Skip to content

[Cache] Fix example for using cache tags with TagAwareCacheInterface #12181

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

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 15 additions & 10 deletions cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,26 @@ 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'])
// using autowiring
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// using autowiring

public function listProducts(TagAwareCacheInterface $pool)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update $pool to $myCachePool to match the config below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a more generic name than listProducts. Maybe foo?

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

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

$value1 = $pool->get('item_1', function (ItemInterface $item) {
$item->tag('foo')
$value1 = $pool->get('item_1', function (ItemInterface $item) {
$item->tag('foo');

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

// Remove all cache keys tagged with "bar"
$pool->invalidateTags(['bar']);
// Remove all cache keys tagged with "bar"
$pool->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