Xcache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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\Driver;
  13. /**
  14. * Xcache缓存驱动
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. class Xcache extends Driver
  18. {
  19. protected $options = [
  20. 'prefix' => '',
  21. 'expire' => 0,
  22. 'serialize' => true,
  23. ];
  24. /**
  25. * 架构函数
  26. * @access public
  27. * @param array $options 缓存参数
  28. * @throws \BadFunctionCallException
  29. */
  30. public function __construct($options = [])
  31. {
  32. if (!function_exists('xcache_info')) {
  33. throw new \BadFunctionCallException('not support: Xcache');
  34. }
  35. if (!empty($options)) {
  36. $this->options = array_merge($this->options, $options);
  37. }
  38. }
  39. /**
  40. * 判断缓存
  41. * @access public
  42. * @param string $name 缓存变量名
  43. * @return bool
  44. */
  45. public function has($name)
  46. {
  47. $key = $this->getCacheKey($name);
  48. return xcache_isset($key);
  49. }
  50. /**
  51. * 读取缓存
  52. * @access public
  53. * @param string $name 缓存变量名
  54. * @param mixed $default 默认值
  55. * @return mixed
  56. */
  57. public function get($name, $default = false)
  58. {
  59. $this->readTimes++;
  60. $key = $this->getCacheKey($name);
  61. return xcache_isset($key) ? $this->unserialize(xcache_get($key)) : $default;
  62. }
  63. /**
  64. * 写入缓存
  65. * @access public
  66. * @param string $name 缓存变量名
  67. * @param mixed $value 存储数据
  68. * @param integer|\DateTime $expire 有效时间(秒)
  69. * @return boolean
  70. */
  71. public function set($name, $value, $expire = null)
  72. {
  73. $this->writeTimes++;
  74. if (is_null($expire)) {
  75. $expire = $this->options['expire'];
  76. }
  77. if ($this->tag && !$this->has($name)) {
  78. $first = true;
  79. }
  80. $key = $this->getCacheKey($name);
  81. $expire = $this->getExpireTime($expire);
  82. $value = $this->serialize($value);
  83. if (xcache_set($key, $value, $expire)) {
  84. isset($first) && $this->setTagItem($key);
  85. return true;
  86. }
  87. return false;
  88. }
  89. /**
  90. * 自增缓存(针对数值缓存)
  91. * @access public
  92. * @param string $name 缓存变量名
  93. * @param int $step 步长
  94. * @return false|int
  95. */
  96. public function inc($name, $step = 1)
  97. {
  98. $this->writeTimes++;
  99. $key = $this->getCacheKey($name);
  100. return xcache_inc($key, $step);
  101. }
  102. /**
  103. * 自减缓存(针对数值缓存)
  104. * @access public
  105. * @param string $name 缓存变量名
  106. * @param int $step 步长
  107. * @return false|int
  108. */
  109. public function dec($name, $step = 1)
  110. {
  111. $this->writeTimes++;
  112. $key = $this->getCacheKey($name);
  113. return xcache_dec($key, $step);
  114. }
  115. /**
  116. * 删除缓存
  117. * @access public
  118. * @param string $name 缓存变量名
  119. * @return boolean
  120. */
  121. public function rm($name)
  122. {
  123. $this->writeTimes++;
  124. return xcache_unset($this->getCacheKey($name));
  125. }
  126. /**
  127. * 清除缓存
  128. * @access public
  129. * @param string $tag 标签名
  130. * @return boolean
  131. */
  132. public function clear($tag = null)
  133. {
  134. if ($tag) {
  135. // 指定标签清除
  136. $keys = $this->getTagItem($tag);
  137. foreach ($keys as $key) {
  138. xcache_unset($key);
  139. }
  140. $this->rm($this->getTagKey($tag));
  141. return true;
  142. }
  143. $this->writeTimes++;
  144. if (function_exists('xcache_unset_by_prefix')) {
  145. return xcache_unset_by_prefix($this->options['prefix']);
  146. } else {
  147. return false;
  148. }
  149. }
  150. }