Skip to content

[Cache] Improved the example about cache tags #12398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,36 @@ use cache tags. One or more tags could be added to the cache item. All items wit
the same key could be invalidate with one function call::

use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

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

return 'debug';
});
// using autowiring to inject the cache pool
public function __construct(TagAwareCacheInterface $myCachePool)
{
$this->myCachePool = $myCachePool;
}

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

return 'debug';
});
return 'debug';
});

// Remove all cache keys tagged with "bar"
$pool->invalidateTags(['bar']);
$value1 = $this->myCachePool->get('item_1', function (ItemInterface $item) {
$item->tag('foo');

return 'debug';
});

// Remove all cache keys tagged with "bar"
$this->myCachePool->invalidateTags(['bar']);
}
}

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