Driver.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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;
  12. /**
  13. * 缓存基础类
  14. */
  15. abstract class Driver
  16. {
  17. protected $handler = null;
  18. protected $options = [];
  19. protected $tag;
  20. /**
  21. * 判断缓存是否存在
  22. * @access public
  23. * @param string $name 缓存变量名
  24. * @return bool
  25. */
  26. abstract public function has($name);
  27. /**
  28. * 读取缓存
  29. * @access public
  30. * @param string $name 缓存变量名
  31. * @param mixed $default 默认值
  32. * @return mixed
  33. */
  34. abstract public function get($name, $default = false);
  35. /**
  36. * 写入缓存
  37. * @access public
  38. * @param string $name 缓存变量名
  39. * @param mixed $value 存储数据
  40. * @param int $expire 有效时间 0为永久
  41. * @return boolean
  42. */
  43. abstract public function set($name, $value, $expire = null);
  44. /**
  45. * 自增缓存(针对数值缓存)
  46. * @access public
  47. * @param string $name 缓存变量名
  48. * @param int $step 步长
  49. * @return false|int
  50. */
  51. abstract public function inc($name, $step = 1);
  52. /**
  53. * 自减缓存(针对数值缓存)
  54. * @access public
  55. * @param string $name 缓存变量名
  56. * @param int $step 步长
  57. * @return false|int
  58. */
  59. abstract public function dec($name, $step = 1);
  60. /**
  61. * 删除缓存
  62. * @access public
  63. * @param string $name 缓存变量名
  64. * @return boolean
  65. */
  66. abstract public function rm($name);
  67. /**
  68. * 清除缓存
  69. * @access public
  70. * @param string $tag 标签名
  71. * @return boolean
  72. */
  73. abstract public function clear($tag = null);
  74. /**
  75. * 获取实际的缓存标识
  76. * @access public
  77. * @param string $name 缓存名
  78. * @return string
  79. */
  80. protected function getCacheKey($name)
  81. {
  82. return $this->options['prefix'] . $name;
  83. }
  84. /**
  85. * 读取缓存并删除
  86. * @access public
  87. * @param string $name 缓存变量名
  88. * @return mixed
  89. */
  90. public function pull($name)
  91. {
  92. $result = $this->get($name, false);
  93. if ($result) {
  94. $this->rm($name);
  95. return $result;
  96. } else {
  97. return;
  98. }
  99. }
  100. /**
  101. * 如果不存在则写入缓存
  102. * @access public
  103. * @param string $name 缓存变量名
  104. * @param mixed $value 存储数据
  105. * @param int $expire 有效时间 0为永久
  106. * @return mixed
  107. */
  108. public function remember($name, $value, $expire = null)
  109. {
  110. if (!$this->has($name)) {
  111. while ($this->has($name . '_lock')) {
  112. // 存在锁定则等待
  113. }
  114. try {
  115. // 锁定
  116. $this->set($name . '_lock', true);
  117. if ($value instanceof \Closure) {
  118. $value = call_user_func($value);
  119. }
  120. $this->set($name, $value, $expire);
  121. // 解锁
  122. $this->rm($name . '_lock');
  123. } catch (\Exception $e) {
  124. // 解锁
  125. $this->rm($name . '_lock');
  126. }
  127. } else {
  128. $value = $this->get($name);
  129. }
  130. return $value;
  131. }
  132. /**
  133. * 缓存标签
  134. * @access public
  135. * @param string $name 标签名
  136. * @param string|array $keys 缓存标识
  137. * @param bool $overlay 是否覆盖
  138. * @return $this
  139. */
  140. public function tag($name, $keys = null, $overlay = false)
  141. {
  142. if (is_null($name)) {
  143. } elseif (is_null($keys)) {
  144. $this->tag = $name;
  145. } else {
  146. $key = 'tag_' . md5($name);
  147. if (is_string($keys)) {
  148. $keys = explode(',', $keys);
  149. }
  150. $keys = array_map([$this, 'getCacheKey'], $keys);
  151. if ($overlay) {
  152. $value = $keys;
  153. } else {
  154. $value = array_unique(array_merge($this->getTagItem($name), $keys));
  155. }
  156. $this->set($key, implode(',', $value), 0);
  157. }
  158. return $this;
  159. }
  160. /**
  161. * 更新标签
  162. * @access public
  163. * @param string $name 缓存标识
  164. * @return void
  165. */
  166. protected function setTagItem($name)
  167. {
  168. if ($this->tag) {
  169. $key = 'tag_' . md5($this->tag);
  170. $this->tag = null;
  171. if ($this->has($key)) {
  172. $value = explode(',', $this->get($key));
  173. $value[] = $name;
  174. $value = implode(',', array_unique($value));
  175. } else {
  176. $value = $name;
  177. }
  178. $this->set($key, $value, 0);
  179. }
  180. }
  181. /**
  182. * 获取标签包含的缓存标识
  183. * @access public
  184. * @param string $tag 缓存标签
  185. * @return array
  186. */
  187. protected function getTagItem($tag)
  188. {
  189. $key = 'tag_' . md5($tag);
  190. $value = $this->get($key);
  191. if ($value) {
  192. return array_filter(explode(',', $value));
  193. } else {
  194. return [];
  195. }
  196. }
  197. /**
  198. * 返回句柄对象,可执行其它高级方法
  199. *
  200. * @access public
  201. * @return object
  202. */
  203. public function handler()
  204. {
  205. return $this->handler;
  206. }
  207. }