5
5
class RedisTaggedCache extends TaggedCache
6
6
{
7
7
/**
8
- * Forever reference key.
8
+ * Store an item in the cache if the key does not exist .
9
9
*
10
- * @var string
10
+ * @param string $key
11
+ * @param mixed $value
12
+ * @param \DateTimeInterface|\DateInterval|int|null $ttl
13
+ * @return bool
11
14
*/
12
- const REFERENCE_KEY_FOREVER = 'forever_ref ' ;
15
+ public function add ($ key , $ value , $ ttl = null )
16
+ {
17
+ $ this ->tags ->addEntry (
18
+ $ this ->itemKey ($ key ),
19
+ ! is_null ($ ttl ) ? $ this ->getSeconds ($ ttl ) : 0
20
+ );
13
21
14
- /**
15
- * Standard reference key.
16
- *
17
- * @var string
18
- */
19
- const REFERENCE_KEY_STANDARD = 'standard_ref ' ;
22
+ return parent ::add ($ key , $ value , $ ttl );
23
+ }
20
24
21
25
/**
22
26
* Store an item in the cache.
@@ -28,11 +32,14 @@ class RedisTaggedCache extends TaggedCache
28
32
*/
29
33
public function put ($ key , $ value , $ ttl = null )
30
34
{
31
- if ($ ttl === null ) {
35
+ if (is_null ( $ ttl) ) {
32
36
return $ this ->forever ($ key , $ value );
33
37
}
34
38
35
- $ this ->pushStandardKeys ($ this ->tags ->getNamespace (), $ key );
39
+ $ this ->tags ->addEntry (
40
+ $ this ->itemKey ($ key ),
41
+ $ this ->getSeconds ($ ttl )
42
+ );
36
43
37
44
return parent ::put ($ key , $ value , $ ttl );
38
45
}
@@ -46,7 +53,7 @@ public function put($key, $value, $ttl = null)
46
53
*/
47
54
public function increment ($ key , $ value = 1 )
48
55
{
49
- $ this ->pushStandardKeys ($ this ->tags -> getNamespace ( ), $ key );
56
+ $ this ->tags -> addEntry ($ this ->itemKey ( $ key ), updateWhen: ' NX ' );
50
57
51
58
return parent ::increment ($ key , $ value );
52
59
}
@@ -60,7 +67,7 @@ public function increment($key, $value = 1)
60
67
*/
61
68
public function decrement ($ key , $ value = 1 )
62
69
{
63
- $ this ->pushStandardKeys ($ this ->tags -> getNamespace ( ), $ key );
70
+ $ this ->tags -> addEntry ($ this ->itemKey ( $ key ), updateWhen: ' NX ' );
64
71
65
72
return parent ::decrement ($ key , $ value );
66
73
}
@@ -74,7 +81,7 @@ public function decrement($key, $value = 1)
74
81
*/
75
82
public function forever ($ key , $ value )
76
83
{
77
- $ this ->pushForeverKeys ($ this ->tags -> getNamespace (), $ key );
84
+ $ this ->tags -> addEntry ($ this ->itemKey ( $ key) );
78
85
79
86
return parent ::forever ($ key , $ value );
80
87
}
@@ -86,129 +93,37 @@ public function forever($key, $value)
86
93
*/
87
94
public function flush ()
88
95
{
89
- $ this ->deleteForeverKeys ();
90
- $ this ->deleteStandardKeys ();
91
-
96
+ $ this ->flushValues ();
92
97
$ this ->tags ->flush ();
93
98
94
99
return true ;
95
100
}
96
101
97
102
/**
98
- * Store standard key references into store.
99
- *
100
- * @param string $namespace
101
- * @param string $key
102
- * @return void
103
- */
104
- protected function pushStandardKeys ($ namespace , $ key )
105
- {
106
- $ this ->pushKeys ($ namespace , $ key , self ::REFERENCE_KEY_STANDARD );
107
- }
108
-
109
- /**
110
- * Store forever key references into store.
111
- *
112
- * @param string $namespace
113
- * @param string $key
114
- * @return void
115
- */
116
- protected function pushForeverKeys ($ namespace , $ key )
117
- {
118
- $ this ->pushKeys ($ namespace , $ key , self ::REFERENCE_KEY_FOREVER );
119
- }
120
-
121
- /**
122
- * Store a reference to the cache key against the reference key.
123
- *
124
- * @param string $namespace
125
- * @param string $key
126
- * @param string $reference
127
- * @return void
128
- */
129
- protected function pushKeys ($ namespace , $ key , $ reference )
130
- {
131
- $ fullKey = $ this ->store ->getPrefix ().sha1 ($ namespace ).': ' .$ key ;
132
-
133
- foreach (explode ('| ' , $ namespace ) as $ segment ) {
134
- $ this ->store ->connection ()->sadd ($ this ->referenceKey ($ segment , $ reference ), $ fullKey );
135
- }
136
- }
137
-
138
- /**
139
- * Delete all of the items that were stored forever.
103
+ * Flush the individual cache entries for the tags.
140
104
*
141
105
* @return void
142
106
*/
143
- protected function deleteForeverKeys ()
107
+ protected function flushValues ()
144
108
{
145
- $ this ->deleteKeysByReference (self ::REFERENCE_KEY_FOREVER );
146
- }
147
-
148
- /**
149
- * Delete all standard items.
150
- *
151
- * @return void
152
- */
153
- protected function deleteStandardKeys ()
154
- {
155
- $ this ->deleteKeysByReference (self ::REFERENCE_KEY_STANDARD );
156
- }
109
+ $ entries = $ this ->tags ->entries ()
110
+ ->map (fn (string $ key ) => $ this ->store ->getPrefix ().$ key )
111
+ ->chunk (1000 );
157
112
158
- /**
159
- * Find and delete all of the items that were stored against a reference.
160
- *
161
- * @param string $reference
162
- * @return void
163
- */
164
- protected function deleteKeysByReference ($ reference )
165
- {
166
- foreach (explode ('| ' , $ this ->tags ->getNamespace ()) as $ segment ) {
167
- $ this ->deleteValues ($ segment = $ this ->referenceKey ($ segment , $ reference ));
168
-
169
- $ this ->store ->connection ()->del ($ segment );
113
+ foreach ($ entries as $ cacheKeys ) {
114
+ $ this ->store ->connection ()->del (...$ cacheKeys );
170
115
}
171
116
}
172
117
173
118
/**
174
- * Delete item keys that have been stored against a reference .
119
+ * Remove all stale reference entries from the tag set .
175
120
*
176
- * @param string $referenceKey
177
- * @return void
121
+ * @return bool
178
122
*/
179
- protected function deleteValues ( $ referenceKey )
123
+ public function flushStale ( )
180
124
{
181
- $ cursor = $ defaultCursorValue = '0 ' ;
182
-
183
- do {
184
- [$ cursor , $ valuesChunk ] = $ this ->store ->connection ()->sscan (
185
- $ referenceKey , $ cursor , ['match ' => '* ' , 'count ' => 1000 ]
186
- );
187
-
188
- // PhpRedis client returns false if set does not exist or empty. Array destruction
189
- // on false stores null in each variable. If valuesChunk is null, it means that
190
- // there were not results from the previously executed "sscan" Redis command.
191
- if (is_null ($ valuesChunk )) {
192
- break ;
193
- }
194
-
195
- $ valuesChunk = array_unique ($ valuesChunk );
196
-
197
- if (count ($ valuesChunk ) > 0 ) {
198
- $ this ->store ->connection ()->del (...$ valuesChunk );
199
- }
200
- } while (((string ) $ cursor ) !== $ defaultCursorValue );
201
- }
125
+ $ this ->tags ->flushStaleEntries ();
202
126
203
- /**
204
- * Get the reference key for the segment.
205
- *
206
- * @param string $segment
207
- * @param string $suffix
208
- * @return string
209
- */
210
- protected function referenceKey ($ segment , $ suffix )
211
- {
212
- return $ this ->store ->getPrefix ().$ segment .': ' .$ suffix ;
127
+ return true ;
213
128
}
214
129
}
0 commit comments