Skip to content

Commit 4f986e3

Browse files
committed
2.0
1 parent 0420d03 commit 4f986e3

15 files changed

Lines changed: 2167 additions & 79 deletions

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
# think-cache
2-
webman think-cache plugin
1+
# webman think-cache
2+
3+
项目来源于 [top-think/think-cache](https://github.com/top-think/think-cache) [bilulanlv/think-cache](https://github.com/bilulanlv/think-cache),在此基础上支持增加了协程和连接池支持,同时支持webman非协程环境。
4+
本缓存只支持redis和文件驱动。

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
"type": "library",
44
"license": "MIT",
55
"require": {
6-
"topthink/think-cache": "^2.0.6"
6+
"workerman/webman-framework": "^2.0 || dev-master",
7+
"topthink/think-container": "^2.0",
8+
"psr/simple-cache": "^1.0|^2.0|^3.0"
79
},
810
"autoload": {
911
"psr-4": {
10-
"Webman\\ThinkCache\\": "src"
12+
"Webman\\ThinkCache\\": "src",
13+
"support\\": "src/support"
1114
}
1215
}
1316
}

src/CacheHandlerInterface.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
19+
/**
20+
* 缓存驱动接口
21+
*/
22+
interface CacheHandlerInterface extends CacheInterface
23+
{
24+
25+
/**
26+
* 自增缓存(针对数值缓存)
27+
* @param string $name 缓存变量名
28+
* @param int $step 步长
29+
* @return false|int
30+
*/
31+
public function inc($name, $step = 1);
32+
33+
/**
34+
* 自减缓存(针对数值缓存)
35+
* @param string $name 缓存变量名
36+
* @param int $step 步长
37+
* @return false|int
38+
*/
39+
public function dec($name, $step = 1);
40+
41+
/**
42+
* 读取缓存并删除
43+
* @param string $name 缓存变量名
44+
* @return mixed
45+
*/
46+
public function pull($name);
47+
48+
/**
49+
* 如果不存在则写入缓存
50+
* @param string $name 缓存变量名
51+
* @param mixed $value 存储数据
52+
* @param int|DateInterval|DateTimeInterface $expire 有效时间 0为永久
53+
* @return mixed
54+
*/
55+
public function remember($name, $value, $expire = null);
56+
57+
/**
58+
* 缓存标签
59+
* @param string|array $name 标签名
60+
* @return TagSet
61+
*/
62+
public function tag($name);
63+
64+
/**
65+
* 删除缓存标签
66+
* @param array $keys 缓存标识列表
67+
* @return void
68+
*/
69+
public function clearTag($keys);
70+
}

src/CacheManager.php

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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

Comments
 (0)