0 follower

Final Class Yiisoft\Cache\PrefixedCache

InheritanceYiisoft\Cache\PrefixedCache
ImplementsPsr\SimpleCache\CacheInterface

PrefixedCache decorates any PSR-16 cache to add global prefix. It is added to every cache key so that it is unique globally in the whole cache storage. It is recommended that you set a unique cache key prefix for each application if the same cache storage is being used by different applications.

$cache = new PrefixedCache(new ArrayCache(), 'my_app_');
$cache->set('answer', 42); // Will set 42 to my_app_answer key.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Psr\SimpleCache\CacheInterface $cache, string $prefix )
$cache \Psr\SimpleCache\CacheInterface

PSR-16 cache to add prefix to.

$prefix string

Prefix to use for all cache keys.

                public function __construct(
    private readonly PsrSimpleCacheInterface $cache,
    private readonly string $prefix
) {
}

            
clear() public method

public boolean clear ( )

                public function clear(): bool
{
    return $this->cache->clear();
}

            
delete() public method

public boolean delete ( string $key )
$key string

                public function delete(string $key): bool
{
    return $this->cache->delete($this->prefix . $key);
}

            
deleteMultiple() public method

public boolean deleteMultiple ( iterable $keys )
$keys iterable

                public function deleteMultiple(iterable $keys): bool
{
    $prefixedKeys = [];
    foreach ($keys as $key) {
        $prefixedKeys[] = $this->prefix . $key;
    }
    return $this->cache->deleteMultiple($prefixedKeys);
}

            
get() public method

public mixed get ( string $key, mixed $default null )
$key string
$default mixed

                public function get(string $key, mixed $default = null): mixed
{
    return $this->cache->get($this->prefix . $key, $default);
}

            
getMultiple() public method

public iterable getMultiple ( iterable $keys, mixed $default null )
$keys iterable
$default mixed

                public function getMultiple(iterable $keys, mixed $default = null): iterable
{
    $prefixedKeys = [];
    foreach ($keys as $key) {
        $prefixedKeys[] = $this->prefix . $key;
    }
    return $this->cache->getMultiple($prefixedKeys, $default);
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    return $this->cache->has($this->prefix . $key);
}

            
set() public method

public boolean set ( string $key, mixed $value, Yiisoft\Cache\Ttl|null|integer|DateInterval $ttl null )
$key string
$value mixed
$ttl Yiisoft\Cache\Ttl|null|integer|DateInterval

                public function set(string $key, mixed $value, Ttl|null|int|DateInterval $ttl = null): bool
{
    return $this->cache->set($this->prefix . $key, $value, $ttl);
}

            
setMultiple() public method

public boolean setMultiple ( iterable $values, Yiisoft\Cache\Ttl|null|integer|DateInterval $ttl null )
$values iterable
$ttl Yiisoft\Cache\Ttl|null|integer|DateInterval

                public function setMultiple(iterable $values, Ttl|null|int|DateInterval $ttl = null): bool
{
    $prefixedValues = [];
    /**
     * @var string $key
     */
    foreach ($values as $key => $value) {
        $prefixedValues[$this->prefix . $key] = $value;
    }
    return $this->cache->setMultiple($prefixedValues, $ttl);
}