-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
RedisTaggedCache.php
198 lines (174 loc) · 4.64 KB
/
RedisTaggedCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Illuminate\Cache;
class RedisTaggedCache extends TaggedCache
{
/**
* Forever reference key.
*
* @var string
*/
const REFERENCE_KEY_FOREVER = 'forever_ref';
/**
* Standard reference key.
*
* @var string
*/
const REFERENCE_KEY_STANDARD = 'standard_ref';
/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @return bool
*/
public function put($key, $value, $ttl = null)
{
if ($ttl === null) {
return $this->forever($key, $value);
}
$this->pushStandardKeys($this->tags->getNamespace(), $key);
return parent::put($key, $value, $ttl);
}
/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function increment($key, $value = 1)
{
$this->pushStandardKeys($this->tags->getNamespace(), $key);
parent::increment($key, $value);
}
/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function decrement($key, $value = 1)
{
$this->pushStandardKeys($this->tags->getNamespace(), $key);
parent::decrement($key, $value);
}
/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return bool
*/
public function forever($key, $value)
{
$this->pushForeverKeys($this->tags->getNamespace(), $key);
return parent::forever($key, $value);
}
/**
* Remove all items from the cache.
*
* @return bool
*/
public function flush()
{
$this->deleteForeverKeys();
$this->deleteStandardKeys();
return parent::flush();
}
/**
* Store standard key references into store.
*
* @param string $namespace
* @param string $key
* @return void
*/
protected function pushStandardKeys($namespace, $key)
{
$this->pushKeys($namespace, $key, self::REFERENCE_KEY_STANDARD);
}
/**
* Store forever key references into store.
*
* @param string $namespace
* @param string $key
* @return void
*/
protected function pushForeverKeys($namespace, $key)
{
$this->pushKeys($namespace, $key, self::REFERENCE_KEY_FOREVER);
}
/**
* Store a reference to the cache key against the reference key.
*
* @param string $namespace
* @param string $key
* @param string $reference
* @return void
*/
protected function pushKeys($namespace, $key, $reference)
{
$fullKey = $this->store->getPrefix().sha1($namespace).':'.$key;
foreach (explode('|', $namespace) as $segment) {
$this->store->connection()->sadd($this->referenceKey($segment, $reference), $fullKey);
}
}
/**
* Delete all of the items that were stored forever.
*
* @return void
*/
protected function deleteForeverKeys()
{
$this->deleteKeysByReference(self::REFERENCE_KEY_FOREVER);
}
/**
* Delete all standard items.
*
* @return void
*/
protected function deleteStandardKeys()
{
$this->deleteKeysByReference(self::REFERENCE_KEY_STANDARD);
}
/**
* Find and delete all of the items that were stored against a reference.
*
* @param string $reference
* @return void
*/
protected function deleteKeysByReference($reference)
{
foreach (explode('|', $this->tags->getNamespace()) as $segment) {
$this->deleteValues($segment = $this->referenceKey($segment, $reference));
$this->store->connection()->del($segment);
}
}
/**
* Delete item keys that have been stored against a reference.
*
* @param string $referenceKey
* @return void
*/
protected function deleteValues($referenceKey)
{
$values = array_unique($this->store->connection()->smembers($referenceKey));
if (count($values) > 0) {
foreach (array_chunk($values, 1000) as $valuesChunk) {
$this->store->connection()->del(...$valuesChunk);
}
}
}
/**
* Get the reference key for the segment.
*
* @param string $segment
* @param string $suffix
* @return string
*/
protected function referenceKey($segment, $suffix)
{
return $this->store->getPrefix().$segment.':'.$suffix;
}
}