Wincache.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. */
  16. class Wincache extends Driver
  17. {
  18. /**
  19. * 配置参数
  20. * @var array
  21. */
  22. protected $options = [
  23. 'prefix' => '',
  24. 'expire' => 0,
  25. 'tag_prefix' => 'tag:',
  26. 'serialize' => [],
  27. ];
  28. /**
  29. * 架构函数
  30. * @access public
  31. * @param array $options 缓存参数
  32. * @throws \BadFunctionCallException
  33. */
  34. public function __construct(array $options = [])
  35. {
  36. if (!function_exists('wincache_ucache_info')) {
  37. throw new \BadFunctionCallException('not support: WinCache');
  38. }
  39. if (!empty($options)) {
  40. $this->options = array_merge($this->options, $options);
  41. }
  42. }
  43. /**
  44. * 判断缓存
  45. * @access public
  46. * @param string $name 缓存变量名
  47. * @return bool
  48. */
  49. public function has($name): bool
  50. {
  51. $this->readTimes++;
  52. $key = $this->getCacheKey($name);
  53. return wincache_ucache_exists($key);
  54. }
  55. /**
  56. * 读取缓存
  57. * @access public
  58. * @param string $name 缓存变量名
  59. * @param mixed $default 默认值
  60. * @return mixed
  61. */
  62. public function get($name, $default = false)
  63. {
  64. $this->readTimes++;
  65. $key = $this->getCacheKey($name);
  66. return wincache_ucache_exists($key) ? $this->unserialize(wincache_ucache_get($key)) : $default;
  67. }
  68. /**
  69. * 写入缓存
  70. * @access public
  71. * @param string $name 缓存变量名
  72. * @param mixed $value 存储数据
  73. * @param integer|\DateTime $expire 有效时间(秒)
  74. * @return bool
  75. */
  76. public function set($name, $value, $expire = null): bool
  77. {
  78. $this->writeTimes++;
  79. if (is_null($expire)) {
  80. $expire = $this->options['expire'];
  81. }
  82. $key = $this->getCacheKey($name);
  83. $expire = $this->getExpireTime($expire);
  84. $value = $this->serialize($value);
  85. if (wincache_ucache_set($key, $value, $expire)) {
  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(string $name, int $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(string $name, int $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 bool
  121. */
  122. public function delete($name): bool
  123. {
  124. $this->writeTimes++;
  125. return wincache_ucache_delete($this->getCacheKey($name));
  126. }
  127. /**
  128. * 清除缓存
  129. * @access public
  130. * @return bool
  131. */
  132. public function clear(): bool
  133. {
  134. $this->writeTimes++;
  135. return wincache_ucache_clear();
  136. }
  137. /**
  138. * 删除缓存标签
  139. * @access public
  140. * @param array $keys 缓存标识列表
  141. * @return void
  142. */
  143. public function clearTag(array $keys): void
  144. {
  145. wincache_ucache_delete($keys);
  146. }
  147. }