Skip to content

Commit cd6b150

Browse files
committed
minor #12398 [Cache] Improved the example about cache tags (pavelmaca, javiereguiluz)
This PR was merged into the 4.3 branch. Discussion ---------- [Cache] Improved the example about cache tags This continues the work started in #12181. Commits ------- f9d438f Fixed code 2651f92 [Cache] Improved the example about cache tags ce0c625 [Cache] Fix example for using cache tags with TagAwareCacheInterface
2 parents 358b8c5 + f9d438f commit cd6b150

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+
class 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)