8000 [Cache] Improved the example about cache tags · symfony/symfony-docs@2651f92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2651f92

Browse files
committed
[Cache] Improved the example about cache tags
1 parent ce0c625 commit 2651f92

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

cache.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -489,23 +489,33 @@ the same key could be invalidate with one function call::
489489
use Symfony\Contracts\Cache\ItemInterface;
490490
use Symfony\Contracts\Cache\TagAwareCacheInterface;
491491

492-
// using autowiring
493-
public function listProducts(TagAwareCacheInterface $pool)
492+
public function SomeClass
494493
{
495-
$value0 = $pool->get('item_0', function (ItemInterface $item) {
496-
$item->tag(['foo', 'bar']);
494+
private $myCachePool;
497495

498-
return 'debug';
499-
});
496+
// using autowiring to inject the cache pool
497+
public function __construct(TagAwareCacheInterface $myCachePool)
498+
{
499+
$this->myCachePool = $myCachePool;
500+
}
500501

501-
$value1 = $pool->get('item_1', function (ItemInterface $item) {
502-
$item->tag('foo');
502+
public function someMethod()
503+
{
504+
$value0 = $this->myCachePool->get('item_0', function (ItemInterface $item) {
505+
$item->tag(['foo', 'bar']);
503506

504-
return 'debug';
505-
});
507+
return 'debug';
508+
});
506509

507-
// Remove all cache keys tagged with "bar"
508-
$pool->invalidateTags(['bar']);
510+
$value1 = $this->myCachePool->get('item_1', function (ItemInterface $item) {
511+
$item->tag('foo');
512+
513+
return 'debug';
514+
});
515+
516+
// Remove all cache keys tagged with "bar"
517+
$this->myCachePool->invalidateTags(['bar']);
518+
}
509519
}
510520

511521
The cache adapter needs to implement :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface``

0 commit comments

Comments
 (0)
0