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