Wincache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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 $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. $key = $this->getCacheKey($name);
  75. if ($this->tag && !$this->has($name)) {
  76. $first = true;
  77. }
  78. if (wincache_ucache_set($key, $value, $expire)) {
  79. isset($first) && $this->setTagItem($key);
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * 自增缓存(针对数值缓存)
  86. * @access public
  87. * @param string $name 缓存变量名
  88. * @param int $step 步长
  89. * @return false|int
  90. */
  91. public function inc($name, $step = 1)
  92. {
  93. $key = $this->getCacheKey($name);
  94. return wincache_ucache_inc($key, $step);
  95. }
  96. /**
  97. * 自减缓存(针对数值缓存)
  98. * @access public
  99. * @param string $name 缓存变量名
  100. * @param int $step 步长
  101. * @return false|int
  102. */
  103. public function dec($name, $step = 1)
  104. {
  105. $key = $this->getCacheKey($name);
  106. return wincache_ucache_dec($key, $step);
  107. }
  108. /**
  109. * 删除缓存
  110. * @access public
  111. * @param string $name 缓存变量名
  112. * @return boolean
  113. */
  114. public function rm($name)
  115. {
  116. return wincache_ucache_delete($this->getCacheKey($name));
  117. }
  118. /**
  119. * 清除缓存
  120. * @access public
  121. * @param string $tag 标签名
  122. * @return boolean
  123. */
  124. public function clear($tag = null)
  125. {
  126. if ($tag) {
  127. $keys = $this->getTagItem($tag);
  128. foreach ($keys as $key) {
  129. wincache_ucache_delete($key);
  130. }
  131. $this->rm('tag_' . md5($tag));
  132. return true;
  133. } else {
  134. return wincache_ucache_clear();
  135. }
  136. }
  137. }