Skip to content

Commit 3cdd1dc

Browse files
author
lacatoire
committed
Add few doc on marshaller in redis adapter
1 parent 2cbbb39 commit 3cdd1dc

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

components/cache/adapters/redis_adapter.rst

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
Configure the Connection
@@ -245,6 +249,75 @@ try to add data when no memory is available. An example setting could look as fo
245249
maxmemory 100mb
246250
maxmemory-policy allkeys-lru
247251
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+
248321
Read more about this topic in the official `Redis LRU Cache Documentation`_.
249322

250323
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name

0 commit comments

Comments
 (0)