Skip to content

Commit b111eae

Browse files
Nyholmjaviereguiluz
authored andcommitted
[Cache] Document cache tags
1 parent 931cd65 commit b111eae

File tree

1 file changed

+130
-1
lines changed

1 file changed

+130
-1
lines changed

cache.rst

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ case the value needs to be recalculated.
354354
https://symfony.com/schema/dic/services/services-1.0.xsd">
355355
356356
<framework:config>
357-
<framework:cache default_memcached_provider="memcached://localhost">
357+
<framework:cache>
358358
<framework:pool name="my_cache_pool" adapter="app.my_cache_chain_adapter"/>
359359
<framework:pool name="cache.my_redis" adapter="cache.adapter.redis" provider="redis://user:password@example.com"/>
360360
</framework:cache>
@@ -403,6 +403,135 @@ case the value needs to be recalculated.
403403
adapter in the ``app.my_cache_chain_adapter``
404404

405405

406+
Using Cache Tags
407+
----------------
408+
409+
In applications with many cache keys it could be useful to organize the data stored
410+
to be able to invalidate the cache more efficient. One way to achieve that is to
411+
use cache tags. One or more tags could be added to the cache item. All items with
412+
the same key could be invalidate with one function call::
413+
414+
use Symfony\Contracts\Cache\ItemInterface;
415+
416+
$value0 = $pool->get('item_0', function (ItemInterface $item) {
417+
$item->tag(['foo', 'bar'])
418+
419+
return 'debug';
420+
});
421+
422+
$value1 = $pool->get('item_1', function (ItemInterface $item) {
423+
$item->tag('foo')
424+
425+
return 'debug';
426+
});
427+
428+
// Remove all cache keys tagged with "bar"
429+
$pool->invalidateTags(['bar']);
430+
431+
The cache adapter needs to implement :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface``
432+
to enable this feature. This could be added by using the following configuration.
433+
434+
.. configuration-block::
435+
436+
.. code-block:: yaml
437+
438+
# config/packages/cache.yaml
439+
framework:
440+
cache:
441+
pools:
442+
my_cache_pool:
443+
adapter: cache.adapter.redis
444+
tags: true
445+
446+
.. code-block:: xml
447+
448+
<!-- config/packages/cache.xml -->
449+
<?xml version="1.0" encoding="UTF-8" ?>
450+
<container xmlns="http://symfony.com/schema/dic/services"
451+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
452+
xmlns:framework="http://symfony.com/schema/dic/symfony"
453+
xsi:schemaLocation="http://symfony.com/schema/dic/services
454+
https://symfony.com/schema/dic/services/services-1.0.xsd">
455+
456+
<framework:config>
457+
<framework:cache>
458+
<framework:pool name="my_cache_pool" adapter="cache.adapter.redis" tags="true"/>
459+
</framework:cache>
460+
</framework:config>
461+
</container>
462+
463+
.. code-block:: php
464+
465+
// config/packages/cache.php
466+
$container->loadFromExtension('framework', [
467+
'cache' => [
468+
'pools' => [
469+
'my_cache_pool' => [
470+
'adapter' => 'cache.adapter.redis',
471+
'tags' => true,
472+
],
473+
],
474+
],
475+
]);
476+
477+
Tags are stored in the same pool by default. This is good in most scenarios. But
478+
sometimes it might be better to store the tags in a different pool. That could be
479+
achieved by specifying the adapter.
480+
481+
.. configuration-block::
482+
483+
.. code-block:: yaml
484+
485+
# config/packages/cache.yaml
486+
framework:
487+
cache:
488+
pools:
489+
my_cache_pool:
490+
adapter: cache.adapter.redis
491+
tags: tag_pool
492+
tag_pool:
493+
adapter: cache.adapter.apcu
494+
495+
.. code-block:: xml
496+
497+
<!-- config/packages/cache.xml -->
498+
<?xml version="1.0" encoding="UTF-8" ?>
499+
<container xmlns="http://symfony.com/schema/dic/services"
500+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
501+
xmlns:framework="http://symfony.com/schema/dic/symfony"
502+
xsi:schemaLocation="http://symfony.com/schema/dic/services
503+
https://symfony.com/schema/dic/services/services-1.0.xsd">
504+
505+
<framework:config>
506+
<framework:cache>
507+
<framework:pool name="my_cache_pool" adapter="cache.adapter.redis" tags="tag_pool"/>
508+
<framework:pool name="tag_pool" adapter="cache.adapter.apcu"/>
509+
</framework:cache>
510+
</framework:config>
511+
</container>
512+
513+
.. code-block:: php
514+
515+
// config/packages/cache.php
516+
$container->loadFromExtension('framework', [
517+
'cache' => [
518+
'pools' => [
519+
'my_cache_pool' => [
520+
'adapter' => 'cache.adapter.redis',
521+
'tags' => 'tag_pool',
522+
],
523+
'tag_pool' => [
524+
'adapter' => 'cache.adapter.apcu',
525+
],
526+
],
527+
],
528+
]);
529+
530+
.. note::
531+
532+
The interface :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface`` is
533+
autowired to the ``cache.app`` service.
534+
406535
Clearing the Cache
407536
------------------
408537

0 commit comments

Comments
 (0)