Xcache.class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Cache\Driver;
  12. use Think\Cache;
  13. /**
  14. * Xcache缓存驱动
  15. */
  16. class Xcache extends Cache
  17. {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options = array())
  24. {
  25. if (!function_exists('xcache_info')) {
  26. E(L('_NOT_SUPPORT_') . ':Xcache');
  27. }
  28. $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
  29. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
  30. $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
  31. }
  32. /**
  33. * 读取缓存
  34. * @access public
  35. * @param string $name 缓存变量名
  36. * @return mixed
  37. */
  38. public function get($name)
  39. {
  40. N('cache_read', 1);
  41. $name = $this->options['prefix'] . $name;
  42. if (xcache_isset($name)) {
  43. return xcache_get($name);
  44. }
  45. return false;
  46. }
  47. /**
  48. * 写入缓存
  49. * @access public
  50. * @param string $name 缓存变量名
  51. * @param mixed $value 存储数据
  52. * @param integer $expire 有效时间(秒)
  53. * @return boolean
  54. */
  55. public function set($name, $value, $expire = null)
  56. {
  57. N('cache_write', 1);
  58. if (is_null($expire)) {
  59. $expire = $this->options['expire'];
  60. }
  61. $name = $this->options['prefix'] . $name;
  62. if (xcache_set($name, $value, $expire)) {
  63. if ($this->options['length'] > 0) {
  64. // 记录缓存队列
  65. $this->queue($name);
  66. }
  67. return true;
  68. }
  69. return false;
  70. }
  71. /**
  72. * 删除缓存
  73. * @access public
  74. * @param string $name 缓存变量名
  75. * @return boolean
  76. */
  77. public function rm($name)
  78. {
  79. return xcache_unset($this->options['prefix'] . $name);
  80. }
  81. /**
  82. * 清除缓存
  83. * @access public
  84. * @return boolean
  85. */
  86. public function clear()
  87. {
  88. return xcache_clear_cache(1, -1);
  89. }
  90. }