Skip to content

[Cache] Add few doc on marshaller in redis adapter #20399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion components/cache/adapters/redis_adapter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@
$cache = new RedisAdapter(

// the object that stores a valid connection to your Redis system
\Redis $redisConnection,

Check failure on line 33 in components/cache/adapters/redis_adapter.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected T_VARIABLE, expecting ')'

// the string prefixed to the keys of the items stored in this cache
$namespace = '',

Check failure on line 36 in components/cache/adapters/redis_adapter.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected ','

// the default lifetime (in seconds) for cache items that do not define their
// own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
// until RedisAdapter::clear() is invoked or the server(s) are purged)
$defaultLifetime = 0
$defaultLifetime = 0,

Check failure on line 41 in components/cache/adapters/redis_adapter.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[PHP syntax] Syntax error, unexpected ','
// $marshaller (optional) MarshallerInterface instance to control serialization
// and deserialization of cache items. By default, it uses native PHP serialization.
// Useful to compress data, use custom serialization, or optimize the size and performance of cached items.
?MarshallerInterface $marshaller = null
);

.. versionadded:: 6.3
Expand Down Expand Up @@ -266,6 +270,75 @@

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

Working with Marshaller
-----------------------

TagAwareMarshaller for Tag-Based Caching
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Optimizes caching for tag-based retrieval, allowing efficient management of related items::

$marshaller = new TagAwareMarshaller();

$cache = new RedisAdapter($redis, 'tagged_namespace', 3600, $marshaller);

$item = $cache->getItem('tagged_key');
$item->set(['value' => 'some_data', 'tags' => ['tag1', 'tag2']]);
$cache->save($item);

SodiumMarshaller for Encrypted Caching
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Encrypts cached data with Sodium for added security::

$encryptionKeys = [sodium_crypto_box_keypair()];
$marshaller = new SodiumMarshaller($encryptionKeys);

$cache = new RedisAdapter($redis, 'secure_namespace', 3600, $marshaller);

$item = $cache->getItem('secure_key');
$item->set('confidential_data');
$cache->save($item);

DefaultMarshaller with igbinary Serialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Uses igbinary for faster, more efficient serialization when available::

$marshaller = new DefaultMarshaller(true);

$cache = new RedisAdapter($redis, 'optimized_namespace', 3600, $marshaller);

$item = $cache->getItem('optimized_key');
$item->set(['data' => 'optimized_data']);
$cache->save($item);

DefaultMarshaller with Exception on Failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Throws an exception if serialization fails, aiding in error handling::

$marshaller = new DefaultMarshaller(false, true);

$cache = new RedisAdapter($redis, 'error_namespace', 3600, $marshaller);

try {
$item = $cache->getItem('error_key');
$item->set('data');
$cache->save($item);
} catch (\ValueError $e) {
echo 'Serialization failed: ' . $e->getMessage();
}

SodiumMarshaller with Key Rotation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Supports key rotation, allowing secure decryption with both old and new keys::

$keys = [sodium_crypto_box_keypair(), sodium_crypto_box_keypair()];
$marshaller = new SodiumMarshaller($keys);

$cache = new RedisAdapter($redis, 'rotated_namespace', 3600, $marshaller);

$item = $cache->getItem('rotated_key');
$item->set('data_to_encrypt');
$cache->save($item);

.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
.. _`Redis server`: https://redis.io/
.. _`Redis`: https://github.com/phpredis/phpredis
Expand Down
Loading