Driver.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 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. declare (strict_types = 1);
  12. namespace think\cache;
  13. use Closure;
  14. use DateInterval;
  15. use DateTime;
  16. use DateTimeInterface;
  17. use Exception;
  18. use Psr\SimpleCache\CacheInterface;
  19. use think\Container;
  20. use think\contract\CacheHandlerInterface;
  21. use think\exception\InvalidArgumentException;
  22. use throwable;
  23. /**
  24. * 缓存基础类
  25. */
  26. abstract class Driver implements CacheInterface, CacheHandlerInterface
  27. {
  28. /**
  29. * 驱动句柄
  30. * @var object
  31. */
  32. protected $handler = null;
  33. /**
  34. * 缓存读取次数
  35. * @var integer
  36. */
  37. protected $readTimes = 0;
  38. /**
  39. * 缓存写入次数
  40. * @var integer
  41. */
  42. protected $writeTimes = 0;
  43. /**
  44. * 缓存参数
  45. * @var array
  46. */
  47. protected $options = [];
  48. /**
  49. * 缓存标签
  50. * @var array
  51. */
  52. protected $tag = [];
  53. /**
  54. * 获取有效期
  55. * @access protected
  56. * @param integer|DateTimeInterface|DateInterval $expire 有效期
  57. * @return int
  58. */
  59. protected function getExpireTime($expire): int
  60. {
  61. if ($expire instanceof DateTimeInterface) {
  62. $expire = $expire->getTimestamp() - time();
  63. } elseif ($expire instanceof DateInterval) {
  64. $expire = DateTime::createFromFormat('U', (string) time())
  65. ->add($expire)
  66. ->format('U') - time();
  67. }
  68. return (int) $expire;
  69. }
  70. /**
  71. * 获取实际的缓存标识
  72. * @access public
  73. * @param string $name 缓存名
  74. * @return string
  75. */
  76. public function getCacheKey(string $name): string
  77. {
  78. return $this->options['prefix'] . $name;
  79. }
  80. /**
  81. * 读取缓存并删除
  82. * @access public
  83. * @param string $name 缓存变量名
  84. * @return mixed
  85. */
  86. public function pull(string $name)
  87. {
  88. $result = $this->get($name, false);
  89. if ($result) {
  90. $this->delete($name);
  91. return $result;
  92. }
  93. }
  94. /**
  95. * 追加(数组)缓存
  96. * @access public
  97. * @param string $name 缓存变量名
  98. * @param mixed $value 存储数据
  99. * @return void
  100. */
  101. public function push(string $name, $value): void
  102. {
  103. $item = $this->get($name, []);
  104. if (!is_array($item)) {
  105. throw new InvalidArgumentException('only array cache can be push');
  106. }
  107. $item[] = $value;
  108. if (count($item) > 1000) {
  109. array_shift($item);
  110. }
  111. $item = array_unique($item);
  112. $this->set($name, $item);
  113. }
  114. /**
  115. * 追加TagSet数据
  116. * @access public
  117. * @param string $name 缓存变量名
  118. * @param mixed $value 存储数据
  119. * @return void
  120. */
  121. public function append(string $name, $value): void
  122. {
  123. $this->push($name, $value);
  124. }
  125. /**
  126. * 如果不存在则写入缓存
  127. * @access public
  128. * @param string $name 缓存变量名
  129. * @param mixed $value 存储数据
  130. * @param int $expire 有效时间 0为永久
  131. * @return mixed
  132. */
  133. public function remember(string $name, $value, $expire = null)
  134. {
  135. if ($this->has($name)) {
  136. return $this->get($name);
  137. }
  138. $time = time();
  139. while ($time + 5 > time() && $this->has($name . '_lock')) {
  140. // 存在锁定则等待
  141. usleep(200000);
  142. }
  143. try {
  144. // 锁定
  145. $this->set($name . '_lock', true);
  146. if ($value instanceof Closure) {
  147. // 获取缓存数据
  148. $value = Container::getInstance()->invokeFunction($value);
  149. }
  150. // 缓存数据
  151. $this->set($name, $value, $expire);
  152. // 解锁
  153. $this->delete($name . '_lock');
  154. } catch (Exception | throwable $e) {
  155. $this->delete($name . '_lock');
  156. throw $e;
  157. }
  158. return $value;
  159. }
  160. /**
  161. * 缓存标签
  162. * @access public
  163. * @param string|array $name 标签名
  164. * @return TagSet
  165. */
  166. public function tag($name): TagSet
  167. {
  168. $name = (array) $name;
  169. $key = implode('-', $name);
  170. if (!isset($this->tag[$key])) {
  171. $this->tag[$key] = new TagSet($name, $this);
  172. }
  173. return $this->tag[$key];
  174. }
  175. /**
  176. * 获取标签包含的缓存标识
  177. * @access public
  178. * @param string $tag 标签标识
  179. * @return array
  180. */
  181. public function getTagItems(string $tag): array
  182. {
  183. $name = $this->getTagKey($tag);
  184. return $this->get($name, []);
  185. }
  186. /**
  187. * 获取实际标签名
  188. * @access public
  189. * @param string $tag 标签名
  190. * @return string
  191. */
  192. public function getTagKey(string $tag): string
  193. {
  194. return $this->options['tag_prefix'] . md5($tag);
  195. }
  196. /**
  197. * 序列化数据
  198. * @access protected
  199. * @param mixed $data 缓存数据
  200. * @return string
  201. */
  202. protected function serialize($data): string
  203. {
  204. if (is_numeric($data)) {
  205. return (string) $data;
  206. }
  207. $serialize = $this->options['serialize'][0] ?? "serialize";
  208. return $serialize($data);
  209. }
  210. /**
  211. * 反序列化数据
  212. * @access protected
  213. * @param string $data 缓存数据
  214. * @return mixed
  215. */
  216. protected function unserialize(string $data)
  217. {
  218. if (is_numeric($data)) {
  219. return $data;
  220. }
  221. $unserialize = $this->options['serialize'][1] ?? "unserialize";
  222. return $unserialize($data);
  223. }
  224. /**
  225. * 返回句柄对象,可执行其它高级方法
  226. *
  227. * @access public
  228. * @return object
  229. */
  230. public function handler()
  231. {
  232. return $this->handler;
  233. }
  234. /**
  235. * 返回缓存读取次数
  236. * @access public
  237. * @return int
  238. */
  239. public function getReadTimes(): int
  240. {
  241. return $this->readTimes;
  242. }
  243. /**
  244. * 返回缓存写入次数
  245. * @access public
  246. * @return int
  247. */
  248. public function getWriteTimes(): int
  249. {
  250. return $this->writeTimes;
  251. }
  252. /**
  253. * 读取缓存
  254. * @access public
  255. * @param iterable $keys 缓存变量名
  256. * @param mixed $default 默认值
  257. * @return iterable
  258. * @throws InvalidArgumentException
  259. */
  260. public function getMultiple($keys, $default = null): iterable
  261. {
  262. $result = [];
  263. foreach ($keys as $key) {
  264. $result[$key] = $this->get($key, $default);
  265. }
  266. return $result;
  267. }
  268. /**
  269. * 写入缓存
  270. * @access public
  271. * @param iterable $values 缓存数据
  272. * @param null|int|\DateInterval $ttl 有效时间 0为永久
  273. * @return bool
  274. */
  275. public function setMultiple($values, $ttl = null): bool
  276. {
  277. foreach ($values as $key => $val) {
  278. $result = $this->set($key, $val, $ttl);
  279. if (false === $result) {
  280. return false;
  281. }
  282. }
  283. return true;
  284. }
  285. /**
  286. * 删除缓存
  287. * @access public
  288. * @param iterable $keys 缓存变量名
  289. * @return bool
  290. * @throws InvalidArgumentException
  291. */
  292. public function deleteMultiple($keys): bool
  293. {
  294. foreach ($keys as $key) {
  295. $result = $this->delete($key);
  296. if (false === $result) {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. public function __call($method, $args)
  303. {
  304. return call_user_func_array([$this->handler, $method], $args);
  305. }
  306. }