Wincache.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * Wincache缓存驱动
  15. * @author liu21st <liu21st@gmail.com>
  16. */
  17. class Wincache 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('wincache_ucache_info')) {
  33. throw new \BadFunctionCallException('not support: WinCache');
  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. $this->readTimes++;
  48. $key = $this->getCacheKey($name);
  49. return wincache_ucache_exists($key);
  50. }
  51. /**
  52. * 读取缓存
  53. * @access public
  54. * @param string $name 缓存变量名
  55. * @param mixed $default 默认值
  56. * @return mixed
  57. */
  58. public function get($name, $default = false)
  59. {
  60. $this->readTimes++;
  61. $key = $this->getCacheKey($name);
  62. return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
  63. }
  64. /**
  65. * 写入缓存
  66. * @access public
  67. * @param string $name 缓存变量名
  68. * @param mixed $value 存储数据
  69. * @param integer|\DateTime $expire 有效时间(秒)
  70. * @return boolean
  71. */
  72. public function set($name, $value, $expire = null)
  73. {
  74. $this->writeTimes++;
  75. if (is_null($expire)) {
  76. $expire = $this->options['expire'];
  77. }
  78. $key = $this->getCacheKey($name);
  79. $expire = $this->getExpireTime($expire);
  80. $value = $this->serialize($value);
  81. if ($this->tag && !$this->has($name)) {
  82. $first = true;
  83. }
  84. if (wincache_ucache_set($key, $value, $expire)) {
  85. isset($first) && $this->setTagItem($key);
  86. return true;
  87. }
  88. return false;
  89. }
  90. /**
  91. * 自增缓存(针对数值缓存)
  92. * @access public
  93. * @param string $name 缓存变量名
  94. * @param int $step 步长
  95. * @return false|int
  96. */
  97. public function inc($name, $step = 1)
  98. {
  99. $this->writeTimes++;
  100. $key = $this->getCacheKey($name);
  101. return wincache_ucache_inc($key, $step);
  102. }
  103. /**
  104. * 自减缓存(针对数值缓存)
  105. * @access public
  106. * @param string $name 缓存变量名
  107. * @param int $step 步长
  108. * @return false|int
  109. */
  110. public function dec($name, $step = 1)
  111. {
  112. $this->writeTimes++;
  113. $key = $this->getCacheKey($name);
  114. return wincache_ucache_dec($key, $step);
  115. }
  116. /**
  117. * 删除缓存
  118. * @access public
  119. * @param string $name 缓存变量名
  120. * @return boolean
  121. */
  122. public function rm($name)
  123. {
  124. $this->writeTimes++;
  125. return wincache_ucache_delete($this->getCacheKey($name));
  126. }
  127. /**
  128. * 清除缓存
  129. * @access public
  130. * @param string $tag 标签名
  131. * @return boolean
  132. */
  133. public function clear($tag = null)
  134. {
  135. if ($tag) {
  136. $keys = $this->getTagItem($tag);
  137. wincache_ucache_delete($keys);
  138. $tagName = $this->getTagkey($tag);
  139. $this->rm($tagName);
  140. return true;
  141. }
  142. $this->writeTimes++;
  143. return wincache_ucache_clear();
  144. }
  145. }