Cache.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 yiovo\cache;
  13. use think\Manager;
  14. use think\cache\Driver;
  15. use think\cache\TagSet;
  16. use think\helper\Arr;
  17. use think\exception\InvalidArgumentException;
  18. use Psr\SimpleCache\CacheInterface;
  19. /**
  20. * 缓存管理类
  21. * @mixin Driver
  22. * @mixin \yiovo\cache\cache\driver\File
  23. */
  24. class Cache extends Manager implements CacheInterface
  25. {
  26. protected $namespace = '\\yiovo\\cache\\cache\\driver\\';
  27. /**
  28. * 默认驱动
  29. * @return string|null
  30. */
  31. public function getDefaultDriver()
  32. {
  33. return $this->getConfig('default');
  34. }
  35. /**
  36. * 获取缓存配置
  37. * @access public
  38. * @param null|string $name 名称
  39. * @param mixed $default 默认值
  40. * @return mixed
  41. */
  42. public function getConfig(string $name = null, $default = null)
  43. {
  44. if (!is_null($name)) {
  45. return $this->app->config->get('cache.' . $name, $default);
  46. }
  47. return $this->app->config->get('cache');
  48. }
  49. /**
  50. * 获取驱动配置
  51. * @param string $store
  52. * @param string|null $name
  53. * @param null $default
  54. * @return array
  55. */
  56. public function getStoreConfig(string $store, string $name = null, $default = null)
  57. {
  58. if ($config = $this->getConfig("stores.{$store}")) {
  59. return Arr::get($config, $name, $default);
  60. }
  61. throw new \InvalidArgumentException("Store [$store] not found.");
  62. }
  63. protected function resolveType(string $name)
  64. {
  65. return $this->getStoreConfig($name, 'type', 'file');
  66. }
  67. protected function resolveConfig(string $name)
  68. {
  69. return $this->getStoreConfig($name);
  70. }
  71. /**
  72. * 连接或者切换缓存
  73. * @access public
  74. * @param string|null $name 连接配置名
  75. * @return Driver
  76. */
  77. public function store(string $name = null)
  78. {
  79. return $this->driver($name);
  80. }
  81. /**
  82. * 清空缓冲池
  83. * @access public
  84. * @return bool
  85. */
  86. public function clear(): bool
  87. {
  88. return $this->store()->clear();
  89. }
  90. /**
  91. * 读取缓存
  92. * @access public
  93. * @param string $key 缓存变量名
  94. * @param mixed $default 默认值
  95. * @return mixed
  96. */
  97. public function get($key, $default = null)
  98. {
  99. return $this->store()->get($key, $default);
  100. }
  101. /**
  102. * 写入缓存
  103. * @access public
  104. * @param string $key 缓存变量名
  105. * @param mixed $value 存储数据
  106. * @param int|\DateTime $ttl 有效时间 0为永久
  107. * @return bool
  108. */
  109. public function set($key, $value, $ttl = null): bool
  110. {
  111. return $this->store()->set($key, $value, $ttl);
  112. }
  113. /**
  114. * 删除缓存
  115. * @access public
  116. * @param string $key 缓存变量名
  117. * @return bool
  118. */
  119. public function delete($key): bool
  120. {
  121. return $this->store()->delete($key);
  122. }
  123. /**
  124. * 读取缓存
  125. * @access public
  126. * @param iterable $keys 缓存变量名
  127. * @param mixed $default 默认值
  128. * @return iterable
  129. * @throws InvalidArgumentException
  130. */
  131. public function getMultiple($keys, $default = null): iterable
  132. {
  133. return $this->store()->getMultiple($keys, $default);
  134. }
  135. /**
  136. * 写入缓存
  137. * @access public
  138. * @param iterable $values 缓存数据
  139. * @param null|int|\DateInterval $ttl 有效时间 0为永久
  140. * @return bool
  141. */
  142. public function setMultiple($values, $ttl = null): bool
  143. {
  144. return $this->store()->setMultiple($values, $ttl);
  145. }
  146. /**
  147. * 删除缓存
  148. * @access public
  149. * @param iterable $keys 缓存变量名
  150. * @return bool
  151. * @throws InvalidArgumentException
  152. */
  153. public function deleteMultiple($keys): bool
  154. {
  155. return $this->store()->deleteMultiple($keys);
  156. }
  157. /**
  158. * 判断缓存是否存在
  159. * @access public
  160. * @param string $key 缓存变量名
  161. * @return bool
  162. */
  163. public function has($key): bool
  164. {
  165. return $this->store()->has($key);
  166. }
  167. /**
  168. * 缓存标签
  169. * @access public
  170. * @param string|array $name 标签名
  171. * @return TagSet
  172. */
  173. public function tag($name): TagSet
  174. {
  175. return $this->store()->tag($name);
  176. }
  177. }