Cache.php 4.8 KB

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