CacheManager.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. declare (strict_types = 1);
  12. namespace think;
  13. use Psr\Cache\CacheItemInterface;
  14. use Psr\Cache\CacheItemPoolInterface;
  15. use think\cache\Driver;
  16. use think\Container;
  17. use think\exception\InvalidArgumentException;
  18. /**
  19. * 缓存管理类
  20. * @mixin Driver
  21. */
  22. class CacheManager implements CacheItemPoolInterface
  23. {
  24. /**
  25. * 缓存队列
  26. * @var array
  27. */
  28. protected $data = [];
  29. /**
  30. * 延期保存的缓存队列
  31. * @var array
  32. */
  33. protected $deferred = [];
  34. /**
  35. * 缓存实例
  36. * @var array
  37. */
  38. protected $instance = [];
  39. /**
  40. * 配置参数
  41. * @var array
  42. */
  43. protected $config = [];
  44. /**
  45. * 初始化
  46. * @access public
  47. * @param array $config 配置参数
  48. * @return void
  49. */
  50. public function init(array $config = [])
  51. {
  52. $this->config = $config;
  53. }
  54. /**
  55. * 连接或者切换缓存
  56. * @access public
  57. * @param string $name 连接配置名
  58. * @param bool $force 强制重新连接
  59. * @return Driver
  60. */
  61. public function store(string $name = '', bool $force = false): Driver
  62. {
  63. if ('' == $name) {
  64. $name = $this->config['default'] ?? 'file';
  65. }
  66. if ($force || !isset($this->instance[$name])) {
  67. if (!isset($this->config['stores'][$name])) {
  68. throw new InvalidArgumentException('Undefined cache config:' . $name);
  69. }
  70. $options = $this->config['stores'][$name];
  71. $this->instance[$name] = $this->connect($options);
  72. }
  73. return $this->instance[$name];
  74. }
  75. /**
  76. * 连接缓存
  77. * @access public
  78. * @param array $options 连接参数
  79. * @param string $name 连接配置名
  80. * @return Driver
  81. */
  82. public function connect(array $options, string $name = ''): Driver
  83. {
  84. if ($name && isset($this->instance[$name])) {
  85. return $this->instance[$name];
  86. }
  87. $type = !empty($options['type']) ? $options['type'] : 'File';
  88. $handler = Container::factory($type, '\\think\\cache\\driver\\', $options);
  89. if ($name) {
  90. $this->instance[$name] = $handler;
  91. }
  92. return $handler;
  93. }
  94. /**
  95. * 设置配置
  96. * @access public
  97. * @param array $config 配置参数
  98. * @return void
  99. */
  100. public function config(array $config): void
  101. {
  102. $this->config = array_merge($this->config, $config);
  103. }
  104. /**
  105. * 返回「键」对应的一个缓存项。
  106. * @access public
  107. * @param string $key 缓存标识
  108. * @return CacheItemInterface
  109. * @throws InvalidArgumentException
  110. */
  111. public function getItem($key): CacheItem
  112. {
  113. if (isset($this->data[$key])) {
  114. return $this->data[$key];
  115. }
  116. $cacheItem = new CacheItem($key);
  117. if ($this->has($key)) {
  118. $cacheItem->set($this->get($key));
  119. }
  120. $this->data[$key] = $cacheItem;
  121. return $cacheItem;
  122. }
  123. /**
  124. * 返回一个可供遍历的缓存项集合。
  125. * @access public
  126. * @param array $keys
  127. * @return array|\Traversable
  128. * @throws InvalidArgumentException
  129. */
  130. public function getItems(array $keys = []): array
  131. {
  132. $result = [];
  133. foreach ($keys as $key) {
  134. $result[] = $this->getItem($key);
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 检查缓存系统中是否有「键」对应的缓存项。
  140. * @access public
  141. * @param string $key
  142. * @return bool
  143. * @throws InvalidArgumentException
  144. */
  145. public function hasItem($key): bool
  146. {
  147. return $this->store()->has($key);
  148. }
  149. /**
  150. * 清空缓冲池
  151. * @access public
  152. * @return bool
  153. */
  154. public function clear(): bool
  155. {
  156. return $this->store()->clear();
  157. }
  158. /**
  159. * 从缓冲池里移除某个缓存项
  160. * @access public
  161. * @param string $key
  162. * @return bool
  163. * @throws InvalidArgumentException
  164. */
  165. public function deleteItem($key): bool
  166. {
  167. return $this->store()->delete($key);
  168. }
  169. /**
  170. * 从缓冲池里移除多个缓存项
  171. * @access public
  172. * @param array $keys
  173. * @return bool
  174. * @throws InvalidArgumentException
  175. */
  176. public function deleteItems(array $keys): bool
  177. {
  178. foreach ($keys as $key) {
  179. $this->store()->delete($key);
  180. }
  181. return true;
  182. }
  183. /**
  184. * 立刻为「CacheItemInterface」对象做数据持久化。
  185. * @access public
  186. * @param CacheItemInterface $item
  187. * @return bool
  188. */
  189. public function save(CacheItemInterface $item): bool
  190. {
  191. if ($item->getKey()) {
  192. return $this->store()->set($item->getKey(), $item->get(), $item->getExpire());
  193. }
  194. return false;
  195. }
  196. /**
  197. * 稍后为「CacheItemInterface」对象做数据持久化。
  198. * @access public
  199. * @param CacheItemInterface $item
  200. * @return bool
  201. */
  202. public function saveDeferred(CacheItemInterface $item): bool
  203. {
  204. $this->deferred[$item->getKey()] = $item;
  205. return true;
  206. }
  207. /**
  208. * 提交所有的正在队列里等待的请求到数据持久层,配合 `saveDeferred()` 使用
  209. * @access public
  210. * @return bool
  211. */
  212. public function commit(): bool
  213. {
  214. foreach ($this->deferred as $key => $item) {
  215. $result = $this->save($item);
  216. unset($this->deferred[$key]);
  217. if (false === $result) {
  218. return false;
  219. }
  220. }
  221. return true;
  222. }
  223. public function __call($method, $args)
  224. {
  225. return call_user_func_array([$this->store(), $method], $args);
  226. }
  227. public function __destruct()
  228. {
  229. if (!empty($this->deferred)) {
  230. $this->commit();
  231. }
  232. }
  233. }