|
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 <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\cache; |
|
14
|
|
|
|
|
15
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
16
|
|
|
use think\exception\InvalidArgumentException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* SimpleCache接口 |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
abstract class SimpleCache implements CacheInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* 判断缓存是否存在 |
|
25
|
|
|
* @access public |
|
26
|
|
|
* @param string $name 缓存变量名 |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public function has($name): bool; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* 读取缓存 |
|
33
|
|
|
* @access public |
|
34
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
35
|
|
|
* @param mixed $default 默认值 |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract public function get($name, $default = false); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 写入缓存 |
|
42
|
|
|
* @access public |
|
43
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
44
|
|
|
* @param mixed $value 存储数据 |
|
45
|
|
|
* @param null|int|\DateInterval $expire 有效时间 0为永久 |
|
|
|
|
|
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract public function set($name, $value, $expire = null): bool; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 自增缓存(针对数值缓存) |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @param string $name 缓存变量名 |
|
54
|
|
|
* @param int $step 步长 |
|
55
|
|
|
* @return false|int |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract public function inc(string $name, int $step = 1); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* 自减缓存(针对数值缓存) |
|
61
|
|
|
* @access public |
|
62
|
|
|
* @param string $name 缓存变量名 |
|
63
|
|
|
* @param int $step 步长 |
|
64
|
|
|
* @return false|int |
|
65
|
|
|
*/ |
|
66
|
|
|
abstract public function dec(string $name, int $step = 1); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* 删除缓存 |
|
70
|
|
|
* @access public |
|
71
|
|
|
* @param string $name 缓存变量名 |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract public function delete($name): bool; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* 清除缓存 |
|
78
|
|
|
* @access public |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
abstract public function clear(): bool; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 读取缓存 |
|
85
|
|
|
* @access public |
|
86
|
|
|
* @param iterable $keys 缓存变量名 |
|
|
|
|
|
|
87
|
|
|
* @param mixed $default 默认值 |
|
88
|
|
|
* @return iterable |
|
89
|
|
|
* @throws InvalidArgumentException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getMultiple($keys, $default = null): iterable |
|
92
|
|
|
{ |
|
93
|
|
|
$result = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($keys as $key) { |
|
96
|
|
|
$result[$key] = $this->get($key, $default); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $result; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* 写入缓存 |
|
104
|
|
|
* @access public |
|
105
|
|
|
* @param iterable $values 缓存数据 |
|
106
|
|
|
* @param null|int|\DateInterval $ttl 有效时间 0为永久 |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setMultiple($values, $ttl = null): bool |
|
110
|
|
|
{ |
|
111
|
|
|
foreach ($values as $key => $val) { |
|
112
|
|
|
$result = $this->set($key, $val, $ttl); |
|
113
|
|
|
|
|
114
|
|
|
if (false === $result) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* 删除缓存 |
|
124
|
|
|
* @access public |
|
125
|
|
|
* @param iterable $keys 缓存变量名 |
|
|
|
|
|
|
126
|
|
|
* @return bool |
|
127
|
|
|
* @throws InvalidArgumentException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function deleteMultiple($keys): bool |
|
130
|
|
|
{ |
|
131
|
|
|
foreach ($keys as $key) { |
|
132
|
|
|
$result = $this->delete($key); |
|
133
|
|
|
|
|
134
|
|
|
if (false === $result) { |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|