Skip to content

Commit 91095ab

Browse files
committed
Merge branch '6.4' into 7.1
* 6.4: Minor tweaks Add few doc on marshaller in redis adapter
2 parents 924685d + 810f06d commit 91095ab

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

components/cache/adapters/redis_adapter.rst

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ 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+
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
4248
);
4349

4450
Configure the Connection
@@ -266,6 +272,80 @@ performance when using tag-based invalidation::
266272

267273
Read more about this topic in the official `Redis LRU Cache Documentation`_.
268274

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+
269349
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
270350
.. _`Redis server`: https://redis.io/
271351
.. _`Redis`: https://github.com/phpredis/phpredis

0 commit comments

Comments
 (0)