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 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
) {
}
| public boolean delete ( string $key ) | ||
| $key | string | |
public function delete(string $key): bool
{
return $this->cache->delete($this->prefix . $key);
}
| 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);
}
| 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);
}
| 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);
}
| public boolean has ( string $key ) | ||
| $key | string | |
public function has(string $key): bool
{
return $this->cache->has($this->prefix . $key);
}
| 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);
}
| 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);
}
Signup or Login in order to comment.