@@ -15,7 +15,7 @@ Unlike the :doc:`APCu adapter </components/cache/adapters/apcu_adapter>`, and si
15
15
shared memory; you can store contents independent of your PHP environment. The ability
16
16
to utilize a cluster of servers to provide redundancy and/or fail-over is also available.
17
17
18
- .. caution ::
18
+ .. warning ::
19
19
20
20
**Requirements: ** At least one `Redis server `_ must be installed and running to use this
21
21
adapter. Additionally, this adapter requires a compatible extension or library that implements
@@ -38,7 +38,13 @@ 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
+
43
+ // $marshaller (optional) An instance of MarshallerInterface to control the serialization
44
+ // and deserialization of cache items. By default, native PHP serialization is used.
45
+ // This can be useful for compressing data, applying custom serialization logic, or
46
+ // optimizing the size and performance of cached items
47
+ ?MarshallerInterface $marshaller = null
42
48
);
43
49
44
50
Configure the Connection
@@ -266,6 +272,80 @@ performance when using tag-based invalidation::
266
272
267
273
Read more about this topic in the official `Redis LRU Cache Documentation `_.
268
274
275
+ Working with Marshaller
276
+ -----------------------
277
+
278
+ TagAwareMarshaller for Tag-Based Caching
279
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280
+
281
+ Optimizes caching for tag-based retrieval, allowing efficient management of related items::
282
+
283
+ $marshaller = new TagAwareMarshaller();
284
+
285
+ $cache = new RedisAdapter($redis, 'tagged_namespace', 3600, $marshaller);
286
+
287
+ $item = $cache->getItem('tagged_key');
288
+ $item->set(['value' => 'some_data', 'tags' => ['tag1', 'tag2']]);
289
+ $cache->save($item);
290
+
291
+ SodiumMarshaller for Encrypted Caching
292
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
293
+
294
+ Encrypts cached data using Sodium for enhanced security::
295
+
296
+ $encryptionKeys = [sodium_crypto_box_keypair()];
297
+ $marshaller = new SodiumMarshaller($encryptionKeys);
298
+
299
+ $cache = new RedisAdapter($redis, 'secure_namespace', 3600, $marshaller);
300
+
301
+ $item = $cache->getItem('secure_key');
302
+ $item->set('confidential_data');
303
+ $cache->save($item);
304
+
305
+ DefaultMarshaller with igbinary Serialization
306
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307
+
308
+ Uses ``igbinary` for faster and more efficient serialization when available ::
309
+
310
+ $marshaller = new DefaultMarshaller(true);
311
+
312
+ $cache = new RedisAdapter($redis, 'optimized_namespace', 3600, $marshaller);
313
+
314
+ $item = $cache->getItem('optimized_key');
315
+ $item->set(['data' => 'optimized_data']);
316
+ $cache->save($item);
317
+
318
+ DefaultMarshaller with Exception on Failure
319
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320
+
321
+ Throws an exception if serialization fails, facilitating error handling::
322
+
323
+ $marshaller = new DefaultMarshaller(false, true);
324
+
325
+ $cache = new RedisAdapter($redis, 'error_namespace', 3600, $marshaller);
326
+
327
+ try {
328
+ $item = $cache->getItem('error_key');
329
+ $item->set('data');
330
+ $cache->save($item);
331
+ } catch (\ValueError $e) {
332
+ echo 'Serialization failed: '.$e->getMessage();
333
+ }
334
+
335
+ SodiumMarshaller with Key Rotation
336
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
337
+
338
+ Supports key rotation, ensuring secure decryption with both old and new keys::
339
+
340
+ $keys = [sodium_crypto_box_keypair(), sodium_crypto_box_keypair()];
341
+ $marshaller = new SodiumMarshaller($keys);
342
+
343
+ $cache = new RedisAdapter($redis, 'rotated_namespace', 3600, $marshaller);
344
+
345
+ $item = $cache->getItem('rotated_key');
346
+ $item->set('data_to_encrypt');
347
+ $cache->save($item);
348
+
269
349
.. _`Data Source Name (DSN)` : https://en.wikipedia.org/wiki/Data_source_name
270
350
.. _`Redis server` : https://redis.io/
271
351
.. _`Redis` : https://github.com/phpredis/phpredis
0 commit comments