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 __construct( \Psr\SimpleCache\CacheInterface $cache, string $prefix ): mixed
$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 clear( ): boolean

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

            
delete() public method

public delete( string $key ): boolean
$key string

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

            
deleteMultiple() public method

public deleteMultiple( iterable $keys ): boolean
$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 get( string $key, mixed $default null ): mixed
$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 getMultiple( iterable $keys, mixed $default null ): iterable
$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 has( string $key ): boolean
$key string

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

            
set() public method

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

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

            
setMultiple() public method

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

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