Final Class Yiisoft\Cache\PrefixedCache
| Inheritance | Yiisoft\Cache\PrefixedCache |
|---|---|
| Implements | Psr\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.
Public Methods
Method Details
| 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,
) {}
| public delete( string $key ): boolean | ||
| $key | string | |
public function delete(string $key): bool
{
return $this->cache->delete($this->prefix . $key);
}
| 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);
}
| 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);
}
| 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);
}
| public has( string $key ): boolean | ||
| $key | string | |
public function has(string $key): bool
{
return $this->cache->has($this->prefix . $key);
}
| 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);
}
| 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);
}
Signup or Login in order to comment.