Skip to content

Commit a2974a9

Browse files
committed
Merge branch '4.4'
* 4.4: [Cache] Improved the example about cache tags
2 parents 24f515c + 60f1409 commit a2974a9

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

cache.rst

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,36 @@ use cache tags. One or more tags could be added to the cache item. All items wit
439439
the same key could be invalidate with one function call::
440440

441441
use Symfony\Contracts\Cache\ItemInterface;
442+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
442443

443-
$value0 = $pool->get('item_0', function (ItemInterface $item) {
444-
$item->tag(['foo', 'bar']);
444+
class SomeClass
445+
{
446+
private $myCachePool;
445447

446-
return 'debug';
447-
});
448+
// using autowiring to inject the cache pool
449+
public function __construct(TagAwareCacheInterface $myCachePool)
450+
{
451+
$this->myCachePool = $myCachePool;
452+
}
448453

449-
$value1 = $pool->get('item_1', function (ItemInterface $item) {
450-
$item->tag('foo');
454+
public function someMethod()
455+
{
456+
$value0 = $this->myCachePool->get('item_0', function (ItemInterface $item) {
457+
$item->tag(['foo', 'bar']);
451458

452-
return 'debug';
453-
});
459+
return 'debug';
460+
});
454461

455-
// Remove all cache keys tagged with "bar"
456-
$pool->invalidateTags(['bar']);
462+
$value1 = $this->myCachePool->get('item_1', function (ItemInterface $item) {
463+
$item->tag('foo');
464+
465+
return 'debug';
466+
});
467+
468+
// Remove all cache keys tagged with "bar"
469+
$this->myCachePool->invalidateTags(['bar']);
470+
}
471+
}
457472

458473
The cache adapter needs to implement :class:`Symfony\\Contracts\\Cache\\TagAwareCacheInterface``
459474
to enable this feature. This could be added by using the following configuration.

0 commit comments

Comments
 (0)