@@ -38,7 +38,11 @@ as the second and third parameters::
38
38
// the default lifetime (in seconds) for cache items that do not define their
39
39
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
40
40
// until RedisAdapter::clear() is invoked or the server(s) are purged)
41
- $defaultLifetime = 0
41
+ $defaultLifetime = 0,
42
+ // $marshaller (optional) MarshallerInterface instance to control serialization
43
+ // and deserialization of cache items. By default, it uses native PHP serialization.
44
+ // Useful to compress data, use custom serialization, or optimize the size and performance of cached items.
45
+ ?MarshallerInterface $marshaller = null
42
46
);
43
47
44
48
Configure the Connection
@@ -245,6 +249,75 @@ try to add data when no memory is available. An example setting could look as fo
245
249
maxmemory 100mb
246
250
maxmemory-policy allkeys-lru
247
251
252
+ Working with Marshaller
253
+ -----------------------
254
+
255
+ TagAwareMarshaller for Tag-Based Caching
256
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257
+ Optimizes caching for tag-based retrieval, allowing efficient management of related items::
258
+
259
+ $marshaller = new TagAwareMarshaller();
260
+
261
+ $cache = new RedisAdapter($redis, 'tagged_namespace', 3600, $marshaller);
262
+
263
+ $item = $cache->getItem('tagged_key');
264
+ $item->set(['value' => 'some_data', 'tags' => ['tag1', 'tag2']]);
265
+ $cache->save($item);
266
+
267
+ SodiumMarshaller for Encrypted Caching
268
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269
+ Encrypts cached data with Sodium for added security::
270
+
271
+ $encryptionKeys = [sodium_crypto_box_keypair()];
272
+ $marshaller = new SodiumMarshaller($encryptionKeys);
273
+
274
+ $cache = new RedisAdapter($redis, 'secure_namespace', 3600, $marshaller);
275
+
276
+ $item = $cache->getItem('secure_key');
277
+ $item->set('confidential_data');
278
+ $cache->save($item);
279
+
280
+ DefaultMarshaller with igbinary Serialization
281
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282
+ Uses igbinary for faster, more efficient serialization when available::
283
+
284
+ $marshaller = new DefaultMarshaller(true);
285
+
286
+ $cache = new RedisAdapter($redis, 'optimized_namespace', 3600, $marshaller);
287
+
288
+ $item = $cache->getItem('optimized_key');
289
+ $item->set(['data' => 'optimized_data']);
290
+ $cache->save($item);
291
+
292
+ DefaultMarshaller with Exception on Failure
293
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
294
+ Throws an exception if serialization fails, aiding in error handling::
295
+
296
+ $marshaller = new DefaultMarshaller(false, true);
297
+
298
+ $cache = new RedisAdapter($redis, 'error_namespace', 3600, $marshaller);
299
+
300
+ try {
301
+ $item = $cache->getItem('error_key');
302
+ $item->set('data');
303
+ $cache->save($item);
304
+ } catch (\ValueError $e) {
305
+ echo 'Serialization failed: ' . $e->getMessage();
306
+ }
307
+
308
+ SodiumMarshaller with Key Rotation
309
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310
+ Supports key rotation, allowing secure decryption with both old and new keys::
311
+
312
+ $keys = [sodium_crypto_box_keypair(), sodium_crypto_box_keypair()];
313
+ $marshaller = new SodiumMarshaller($keys);
314
+
315
+ $cache = new RedisAdapter($redis, 'rotated_namespace', 3600, $marshaller);
316
+
317
+ $item = $cache->getItem('rotated_key');
318
+ $item->set('data_to_encrypt');
319
+ $cache->save($item);
320
+
248
321
Read more about this topic in the official `Redis LRU Cache Documentation `_.
249
322
250
323
.. _`Data Source Name (DSN)` : https://en.wikipedia.org/wiki/Data_source_name
0 commit comments