8000 [Cache] Doc context aware adapters, aka hierarchical invalidation by nicolas-grekas · Pull Request #6858 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Doc context aware adapters, aka hierarchical invalidation #6858

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

Closed
Closed
Changes from 1 commit
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
10000
Diff view
Diff view
Prev Previous commit
[Cache] Doc context aware adapters, aka hierarchical invalidation
  • Loading branch information
nicolas-grekas committed Sep 15, 2016
commit dd864d8241bf8aebbaff714dfc6b0d8412196089
48 changes: 46 additions & 2 deletions components/cache/cache_invalidation.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.. index::
single: Cache; Invalidation
single: Cache; Tags
single: Cache; Hierarchical

Cache Invalidation
==================
Expand All @@ -10,13 +11,14 @@ change in the state of your model. The most basic kind of invalidation is direct
items deletion. But when the state of a primary resource has spread accross
several cached items, keeping them in sync can be difficult.

The Symfony Cache component provides two mechanisms to help solve this problem:
The Symfony Cache component provides three mechanisms to help solve this problem:

* Tags based invalidation for managing data dependencies;
* Hierarchical invalidation for context dependent data;
* Expiration based invalidation for time related dependencies.

.. versionadded:: 3.2
Tags based invalidation was introduced in Symfony 3.2.
Tags based and hierarchical invalidations were introduced in Symfony 3.2.

Using Cache Tags
----------------
Expand Down Expand Up @@ -81,6 +83,48 @@ your fronts and have very fast invalidation checks::
new RedisAdapter('redis://localhost')
);

Using Cache Hierarchies
-----------------------

By using adapters that implement
:class:`Symfony\\Component\\Cache\\Adapter\\ContextAwareAdapterInterface`,
you can create nested cache pools from existing ones. Each nested cache pool
deals with its own set of cached items, but clearing their parents clears them
recursively.

Nested cache pools are derivated from their parents by calling
:method:`Symfony\\Component\\Cache\\Adapter\\ContextAwareAdapterInterface::withContext`.
The method takes a single ``$context`` argument, which is a string that
identifies and isolates their cached items subsets. Appart from this, derivated
pools share everything with their parents, esp. any database connection they might
manage.

You can use such contextualized hierarchies everywhere you would otherwise
directly prefix your cache keys, this prefix being the "context" of the cached
items that use it in their keys.

For example, you could use hierachical pools to cache language dependent
variations of your content::

$databaseCachePool = new PdoAdapter($pdoConnection);

$lang = 'en';
$enCachePool = $databaseCachePool->withContext($lang);

$lang = 'fr';
$frCachePool = $databaseCachePool->withContext($lang);

// Get the same "front-page" item but from different lang context
$enFrontPage = $enCachePool->getItem('front-page');
$frFrontPage = $frCachePool->getItem('front-page');

// This clears also $enCachePool and $frCachePool
$databaseCachePool->clear();

.. note::

Invalidating by tags affects both parents and children pools.

Using Cache Expiration
----------------------

Expand Down
0