You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.7 KiB

3 years ago
  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. defined('THINK_PATH') or exit();
  14. /**
  15. * Xcache缓存驱动
  16. */
  17. class Xcache extends Cache {
  18. /**
  19. * 架构函数
  20. * @param array $options 缓存参数
  21. * @access public
  22. */
  23. public function __construct($options=array()) {
  24. if ( !function_exists('xcache_info') ) {
  25. E(L('_NOT_SUPPORT_').':Xcache');
  26. }
  27. $this->options['expire'] = isset($options['expire'])?$options['expire']:C('DATA_CACHE_TIME');
  28. $this->options['prefix'] = isset($options['prefix'])?$options['prefix']:C('DATA_CACHE_PREFIX');
  29. $this->options['length'] = isset($options['length'])?$options['length']:0;
  30. }
  31. /**
  32. * 读取缓存
  33. * @access public
  34. * @param string $name 缓存变量名
  35. * @return mixed
  36. */
  37. public function get($name) {
  38. N('cache_read',1);
  39. $name = $this->options['prefix'].$name;
  40. if (xcache_isset($name)) {
  41. return xcache_get($name);
  42. }
  43. return false;
  44. }
  45. /**
  46. * 写入缓存
  47. * @access public
  48. * @param string $name 缓存变量名
  49. * @param mixed $value 存储数据
  50. * @param integer $expire 有效时间(秒)
  51. * @return boolean
  52. */
  53. public function set($name, $value,$expire=null) {
  54. N('cache_write',1);
  55. if(is_null($expire)) {
  56. $expire = $this->options['expire'] ;
  57. }
  58. $name = $this->options['prefix'].$name;
  59. if(xcache_set($name, $value, $expire)) {
  60. if($this->options['length']>0) {
  61. // 记录缓存队列
  62. $this->queue($name);
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * 删除缓存
  70. * @access public
  71. * @param string $name 缓存变量名
  72. * @return boolean
  73. */
  74. public function rm($name) {
  75. return xcache_unset($this->options['prefix'].$name);
  76. }
  77. /**
  78. * 清除缓存
  79. * @access public
  80. * @return boolean
  81. */
  82. public function clear() {
  83. return xcache_clear_cache(1, -1);
  84. }
  85. }