Skip to content

Commit

Permalink
Merge branch '7.2' into 7.3
Browse files Browse the repository at this point in the history
* 7.2:
  Minor tweaks
  Add few doc on marshaller in redis adapter
  Fix PHP block in TypeInfo documentation
  • Loading branch information
javiereguiluz committed Dec 12, 2024
2 parents d7f888e + 921dcb5 commit f6edd63
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion components/cache/adapters/redis_adapter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ as the second and third parameters::
// 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,

// $marshaller (optional) An instance of MarshallerInterface to control the serialization
// and deserialization of cache items. By default, native PHP serialization is used.
// This can be useful for compressing data, applying custom serialization logic, or
// optimizing the size and performance of cached items
?MarshallerInterface $marshaller = null
);

Configure the Connection
Expand Down Expand Up @@ -266,6 +272,80 @@ performance when using tag-based invalidation::

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 using Sodium for enhanced 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 and 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, facilitating 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, ensuring 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

0 comments on commit f6edd63

Please sign in to comment.