Wincache.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ];
  23. /**
  24. * 构造函数
  25. * @param array $options 缓存参数
  26. * @throws \BadFunctionCallException
  27. * @access public
  28. */
  29. public function __construct($options = [])
  30. {
  31. if (!function_exists('wincache_ucache_info')) {
  32. throw new \BadFunctionCallException('not support: WinCache');
  33. }
  34. if (!empty($options)) {
  35. $this->options = array_merge($this->options, $options);
  36. }
  37. }
  38. /**
  39. * 判断缓存
  40. * @access public
  41. * @param string $name 缓存变量名
  42. * @return bool
  43. */
  44. public function has($name)
  45. {
  46. $key = $this->getCacheKey($name);
  47. return wincache_ucache_exists($key);
  48. }
  49. /**
  50. * 读取缓存
  51. * @access public
  52. * @param string $name 缓存变量名
  53. * @param mixed $default 默认值
  54. * @return mixed
  55. */
  56. public function get($name, $default = false)
  57. {
  58. $key = $this->getCacheKey($name);
  59. return wincache_ucache_exists($key) ? wincache_ucache_get($key) : $default;
  60. }
  61. /**
  62. * 写入缓存
  63. * @access public
  64. * @param string $name 缓存变量名
  65. * @param mixed $value 存储数据
  66. * @param integer|\DateTime $expire 有效时间(秒)
  67. * @return boolean
  68. */
  69. public function set($name, $value, $expire = null)
  70. {
  71. if (is_null($expire)) {
  72. $expire = $this->options['expire'];
  73. }
  74. if ($expire instanceof \DateTime) {
  75. $expire = $expire->getTimestamp() - time();
  76. }
  77. $key = $this->getCacheKey($name);
  78. if ($this->tag && !$this->has($name)) {
  79. $first = true;
  80. }
  81. if (wincache_ucache_set($key, $value, $expire)) {
  82. isset($first) && $this->setTagItem($key);
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. * 自增缓存(针对数值缓存)
  89. * @access public
  90. * @param string $name 缓存变量名
  91. * @param int $step 步长
  92. * @return false|int
  93. */
  94. public function inc($name, $step = 1)
  95. {
  96. $key = $this->getCacheKey($name);
  97. return wincache_ucache_inc($key, $step);
  98. }
  99. /**
  100. * 自减缓存(针对数值缓存)
  101. * @access public
  102. * @param string $name 缓存变量名
  103. * @param int $step 步长
  104. * @return false|int
  105. */
  106. public function dec($name, $step = 1)
  107. {
  108. $key = $this->getCacheKey($name);
  109. return wincache_ucache_dec($key, $step);
  110. }
  111. /**
  112. * 删除缓存
  113. * @access public
  114. * @param string $name 缓存变量名
  115. * @return boolean
  116. */
  117. public function rm($name)
  118. {
  119. return wincache_ucache_delete($this->getCacheKey($name));
  120. }
  121. /**
  122. * 清除缓存
  123. * @access public
  124. * @param string $tag 标签名
  125. * @return boolean
  126. */
  127. public function clear($tag = null)
  128. {
  129. if ($tag) {
  130. $keys = $this->getTagItem($tag);
  131. foreach ($keys as $key) {
  132. wincache_ucache_delete($key);
  133. }
  134. $this->rm('tag_' . md5($tag));
  135. return true;
  136. } else {
  137. return wincache_ucache_clear();
  138. }
  139. }
  140. }