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