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
|
|
|
/** |
16
|
|
|
* 标签集合 |
17
|
|
|
*/ |
18
|
|
|
class TagSet |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* 标签的缓存Key |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $tag; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 缓存句柄 |
28
|
|
|
* @var Driver |
29
|
|
|
*/ |
30
|
|
|
protected $handler; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 架构函数 |
34
|
|
|
* @access public |
35
|
|
|
* @param array $tag 缓存标签 |
36
|
|
|
* @param Driver $cache 缓存对象 |
37
|
|
|
*/ |
38
|
3 |
|
public function __construct(array $tag, Driver $cache) |
39
|
|
|
{ |
40
|
3 |
|
$this->tag = $tag; |
41
|
3 |
|
$this->handler = $cache; |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 写入缓存 |
46
|
|
|
* @access public |
47
|
|
|
* @param string $name 缓存变量名 |
48
|
|
|
* @param mixed $value 存储数据 |
49
|
|
|
* @param integer|\DateTime $expire 有效时间(秒) |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
3 |
|
public function set(string $name, $value, $expire = null): bool |
53
|
|
|
{ |
54
|
3 |
|
$this->handler->set($name, $value, $expire); |
|
|
|
|
55
|
|
|
|
56
|
3 |
|
$this->append($name); |
57
|
|
|
|
58
|
3 |
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 追加缓存标识到标签 |
63
|
|
|
* @access public |
64
|
|
|
* @param string $name 缓存变量名 |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
3 |
|
public function append(string $name): void |
68
|
|
|
{ |
69
|
3 |
|
$name = $this->handler->getCacheKey($name); |
70
|
|
|
|
71
|
3 |
|
foreach ($this->tag as $tag) { |
72
|
3 |
|
$key = $this->handler->getTagKey($tag); |
73
|
3 |
|
$this->handler->push($key, $name); |
74
|
|
|
} |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 写入缓存 |
79
|
|
|
* @access public |
80
|
|
|
* @param iterable $values 缓存数据 |
81
|
|
|
* @param null|int|\DateInterval $ttl 有效时间 0为永久 |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function setMultiple($values, $ttl = null): bool |
85
|
|
|
{ |
86
|
|
|
foreach ($values as $key => $val) { |
87
|
|
|
$result = $this->set($key, $val, $ttl); |
|
|
|
|
88
|
|
|
|
89
|
|
|
if (false === $result) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* 如果不存在则写入缓存 |
99
|
|
|
* @access public |
100
|
|
|
* @param string $name 缓存变量名 |
101
|
|
|
* @param mixed $value 存储数据 |
102
|
|
|
* @param int $expire 有效时间 0为永久 |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function remember(string $name, $value, $expire = null) |
106
|
|
|
{ |
107
|
|
|
$result = $this->handler->remember($name, $value, $expire); |
108
|
|
|
|
109
|
|
|
$this->append($name); |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 清除缓存 |
116
|
|
|
* @access public |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
3 |
|
public function clear(): bool |
120
|
|
|
{ |
121
|
|
|
// 指定标签清除 |
122
|
3 |
|
foreach ($this->tag as $tag) { |
123
|
3 |
|
$names = $this->handler->getTagItems($tag); |
124
|
3 |
|
$this->handler->clearTag($names); |
125
|
|
|
|
126
|
3 |
|
$key = $this->handler->getTagKey($tag); |
127
|
3 |
|
$this->handler->delete($key); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
return true; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|