Cache.php 4.9 KB

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