RedisTaggedCache
Raxos\Cache\Redis\RedisTaggedCache is the tag scoped decorator returned by RedisCache::tags(). It is a readonly class that wraps a RedisCacheInterface and a non empty array of tags, and it invalidates groups of related keys together. See tagged caching for the concept.
readonly class RedisTaggedCache implements RedisTaggedCacheInterfaceConstruction
__construct
public function __construct(
RedisCacheInterface $redis,
array $tags
)Builds the tag scope from the given cache and tags. Throws a RedisCommandFailedException when the tag list is empty. The combined scope is exposed through the public readonly $scope property.
Methods
key
public function key(string $key): stringReturns the given key namespaced with the tag scope hash.
keyRaw
public function keyRaw(string ...$parts): stringBuilds a raw key from the cache prefix and the given parts, joined with colons. Used internally to construct both value keys and tag set keys.
get
public function get(string $key): mixedReads the value of a key within this tag scope.
set
public function set(string $key, mixed $value, int $ttl): boolStores a value, links the key to each tag's member set and extends the tag expiration to at least $ttl seconds.
remember
public function remember(string $key, int $ttl, callable $fn): mixedReturns the cached value when the key exists, otherwise computes it with $fn, stores it with the given time to live and returns it.
exists
public function exists(string $key): boolChecks whether the given key exists within this tag scope.
del
public function del(string ...$keys): boolDeletes the given keys within this tag scope.
flush
public function flush(): voidRemoves every key ever linked to any of the tags, along with the tag sets themselves.
Example
<?php
declare(strict_types=1);
use Raxos\Cache\Redis\RedisCache;
$cache = new RedisCache(prefix: 'app:');
$products = $cache->tags(['products', 'catalog']);
$products->set('product:1', 'Widget', 300);
$products->set('product:2', 'Gadget', 300);
// Later, invalidate every key tagged with either tag.
$products->flush();