Driver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if ($value instanceof \Closure) {
  112. $value = call_user_func($value);
  113. }
  114. $this->set($name, $value, $expire);
  115. } else {
  116. $value = $this->get($name);
  117. }
  118. return $value;
  119. }
  120. /**
  121. * 缓存标签
  122. * @access public
  123. * @param string $name 标签名
  124. * @param string|array $keys 缓存标识
  125. * @param bool $overlay 是否覆盖
  126. * @return $this
  127. */
  128. public function tag($name, $keys = null, $overlay = false)
  129. {
  130. if (is_null($keys)) {
  131. $this->tag = $name;
  132. } else {
  133. $key = 'tag_' . md5($name);
  134. if (is_string($keys)) {
  135. $keys = explode(',', $keys);
  136. }
  137. $keys = array_map([$this, 'getCacheKey'], $keys);
  138. if ($overlay) {
  139. $value = $keys;
  140. } else {
  141. $value = array_unique(array_merge($this->getTagItem($name), $keys));
  142. }
  143. $this->set($key, implode(',', $value));
  144. }
  145. return $this;
  146. }
  147. /**
  148. * 更新标签
  149. * @access public
  150. * @param string $name 缓存标识
  151. * @return void
  152. */
  153. protected function setTagItem($name)
  154. {
  155. if ($this->tag) {
  156. $key = 'tag_' . md5($this->tag);
  157. $this->tag = null;
  158. if ($this->has($key)) {
  159. $value = explode(',', $this->get($key));
  160. $value[] = $name;
  161. $value = implode(',', array_unique($value));
  162. } else {
  163. $value = $name;
  164. }
  165. $this->set($key, $value);
  166. }
  167. }
  168. /**
  169. * 获取标签包含的缓存标识
  170. * @access public
  171. * @param string $tag 缓存标签
  172. * @return array
  173. */
  174. protected function getTagItem($tag)
  175. {
  176. $key = 'tag_' . md5($tag);
  177. $value = $this->get($key);
  178. if ($value) {
  179. return explode(',', $value);
  180. } else {
  181. return [];
  182. }
  183. }
  184. /**
  185. * 返回句柄对象,可执行其它高级方法
  186. *
  187. * @access public
  188. * @return object
  189. */
  190. public function handler()
  191. {
  192. return $this->handler;
  193. }
  194. }