@@ -38,7 +38,11 @@ as the second and third parameters::
3838 // the default lifetime (in seconds) for cache items that do not define their
3939 // own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
4040 // 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
4246 );
4347
4448.. versionadded :: 6.3
@@ -266,6 +270,75 @@ performance when using tag-based invalidation::
266270
267271Read more about this topic in the official `Redis LRU Cache Documentation `_.
268272
273+ Working with Marshaller
274+ -----------------------
275+
276+ TagAwareMarshaller for Tag-Based Caching
277+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278+ Optimizes caching for tag-based retrieval, allowing efficient management of related items::
279+
280+ $marshaller = new TagAwareMarshaller();
281+
282+ $cache = new RedisAdapter($redis, 'tagged_namespace', 3600, $marshaller);
283+
284+ $item = $cache->getItem('tagged_key');
285+ $item->set(['value' => 'some_data', 'tags' => ['tag1', 'tag2']]);
286+ $cache->save($item);
287+
288+ SodiumMarshaller for Encrypted Caching
289+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290+ Encrypts cached data with Sodium for added security::
291+
292+ $encryptionKeys = [sodium_crypto_box_keypair()];
293+ $marshaller = new SodiumMarshaller($encryptionKeys);
294+
295+ $cache = new RedisAdapter($redis, 'secure_namespace', 3600, $marshaller);
296+
297+ $item = $cache->getItem('secure_key');
298+ $item->set('confidential_data');
299+ $cache->save($item);
300+
301+ DefaultMarshaller with igbinary Serialization
302+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303+ Uses igbinary for faster, more efficient serialization when available::
304+
305+ $marshaller = new DefaultMarshaller(true);
306+
307+ $cache = new RedisAdapter($redis, 'optimized_namespace', 3600, $marshaller);
308+
309+ $item = $cache->getItem('optimized_key');
310+ $item->set(['data' => 'optimized_data']);
311+ $cache->save($item);
312+
313+ DefaultMarshaller with Exception on Failure
314+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
315+ Throws an exception if serialization fails, aiding in error handling::
316+
317+ $marshaller = new DefaultMarshaller(false, true);
318+
319+ $cache = new RedisAdapter($redis, 'error_namespace', 3600, $marshaller);
320+
321+ try {
322+ $item = $cache->getItem('error_key');
323+ $item->set('data');
324+ $cache->save($item);
325+ } catch (\ValueError $e) {
326+ echo 'Serialization failed: ' . $e->getMessage();
327+ }
328+
329+ SodiumMarshaller with Key Rotation
330+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331+ Supports key rotation, allowing secure decryption with both old and new keys::
332+
333+ $keys = [sodium_crypto_box_keypair(), sodium_crypto_box_keypair()];
334+ $marshaller = new SodiumMarshaller($keys);
335+
336+ $cache = new RedisAdapter($redis, 'rotated_namespace', 3600, $marshaller);
337+
338+ $item = $cache->getItem('rotated_key');
339+ $item->set('data_to_encrypt');
340+ $cache->save($item);
341+
269342.. _`Data Source Name (DSN)` : https://en.wikipedia.org/wiki/Data_source_name
270343.. _`Redis server` : https://redis.io/
271344.. _`Redis` : https://github.com/phpredis/phpredis
0 commit comments