16
16
/**
17
17
* Memcached storage using memcached extension.
18
18
*/
19
- class MemcachedStorage implements Nette \Caching \Storage, Nette \Caching \BulkReader
19
+ class MemcachedStorage implements Nette \Caching \Storage, Nette \Caching \BulkReader, Nette \ Caching \BulkWriter
20
20
{
21
21
/** @internal cache structure */
22
22
private const
@@ -25,8 +25,6 @@ class MemcachedStorage implements Nette\Caching\Storage, Nette\Caching\BulkReade
25
25
MetaDelta = 'delta ' ;
26
26
27
27
private \Memcached $ memcached ;
28
- private string $ prefix ;
29
- private ?Journal $ journal ;
30
28
31
29
32
30
/**
@@ -41,15 +39,12 @@ public static function isAvailable(): bool
41
39
public function __construct (
42
40
string $ host = 'localhost ' ,
43
41
int $ port = 11211 ,
44
- string $ prefix = '' ,
45
- ?Journal $ journal = null ,
42
+ private string $ prefix = '' ,
43
+ private ?Journal $ journal = null ,
46
44
) {
47
45
if (!static ::isAvailable ()) {
48
46
throw new Nette \NotSupportedException ("PHP extension 'memcached' is not loaded. " );
49
47
}
50
-
51
- $ this ->prefix = $ prefix ;
52
- $ this ->journal = $ journal ;
53
48
$ this ->memcached = new \Memcached ;
54
49
if ($ host ) {
55
50
$ this ->addServer ($ host , $ port );
@@ -168,12 +163,60 @@ public function write(string $key, $data, array $dp): void
168
163
}
169
164
170
165
166
+ public function bulkWrite (array $ items , array $ dp ): bool
167
+ {
168
+ if (isset ($ dp [Cache::Items])) {
169
+ throw new Nette \NotSupportedException ('Dependent items are not supported by MemcachedStorage. ' );
170
+ }
171
+
172
+ $ records = [];
173
+
174
+ $ expire = 0 ;
175
+ if (isset ($ dp [Cache::Expire])) {
176
+ $ expire = (int ) $ dp [Cache::Expire];
177
+ }
178
+
179
+ foreach ($ items as $ key => $ data ) {
180
+ $ key = urlencode ($ this ->prefix . $ key );
181
+ $ meta = [
182
+ self ::MetaData => $ data ,
183
+ ];
184
+
185
+ if (!empty ($ dp [Cache::Sliding])) {
186
+ $ meta [self ::MetaDelta] = $ expire ; // sliding time
187
+ }
188
+
189
+ if (isset ($ dp [Cache::Callbacks])) {
190
+ $ meta [self ::MetaCallbacks] = $ dp [Cache::Callbacks];
191
+ }
192
+
193
+ if (isset ($ dp [Cache::Tags]) || isset ($ dp [Cache::Priority])) {
194
+ if (!$ this ->journal ) {
195
+ throw new Nette \InvalidStateException ('CacheJournal has not been provided. ' );
196
+ }
197
+
198
+ $ this ->journal ->write ($ key , $ dp );
199
+ }
200
+
201
+ $ records [$ key ] = $ meta ;
202
+ }
203
+
204
+ return $ this ->memcached ->setMulti ($ records , $ expire );
205
+ }
206
+
207
+
171
208
public function remove (string $ key ): void
172
209
{
173
210
$ this ->memcached ->delete (urlencode ($ this ->prefix . $ key ), 0 );
174
211
}
175
212
176
213
214
+ public function bulkRemove (array $ keys ): void
215
+ {
216
+ $ this ->memcached ->deleteMulti (array_map (fn ($ key ) => urlencode ($ this ->prefix . $ key ), $ keys ), 0 );
217
+ }
218
+
219
+
177
220
public function clean (array $ conditions ): void
178
221
{
179
222
if (!empty ($ conditions [Cache::All])) {
0 commit comments