Final Class Yiisoft\Cache\Apcu\ApcuCache
| Inheritance | Yiisoft\Cache\Apcu\ApcuCache |
|---|---|
| Implements | Psr\SimpleCache\CacheInterface |
ApcuCache provides APCu caching in terms of an application component.
To use this application component, the APCu PHP extension must be loaded. In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
See \Psr\SimpleCache\CacheInterface for common cache operations that ApcCache supports.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| TTL_EXPIRED | -1 | Yiisoft\Cache\Apcu\ApcuCache | |
| TTL_INFINITY | 0 | Yiisoft\Cache\Apcu\ApcuCache |
Method Details
| public delete( string $key ): boolean | ||
| $key | string | |
public function delete(string $key): bool
{
$this->validateKey($key);
return apcu_delete($key);
}
| public deleteMultiple( iterable $keys ): boolean | ||
| $keys | iterable | |
public function deleteMultiple(iterable $keys): bool
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
return apcu_delete($keys) === [];
}
| public get( string $key, mixed $default = null ): mixed | ||
| $key | string | |
| $default | mixed | |
public function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
$value = apcu_fetch($key, $success);
return $success ? $value : $default;
}
| public getMultiple( iterable $keys, mixed $default = null ): iterable | ||
| $keys | iterable | |
| $default | mixed | |
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
$values = array_fill_keys($keys, $default);
if (($valuesFromCache = apcu_fetch($keys)) === false) {
/** @var array<string, mixed> $values */
return $values;
}
$valuesFromCache = $this->normalizeAPCuOutput($valuesFromCache);
foreach ($values as $key => $value) {
$values[$key] = $valuesFromCache[(string) $key] ?? $value;
}
/** @var array<string, mixed> $values */
return $values;
}
| public has( string $key ): boolean | ||
| $key | string | |
public function has(string $key): bool
{
$this->validateKey($key);
return apcu_exists($key);
}
| public set( string $key, mixed $value, integer|DateInterval|null $ttl = null ): boolean | ||
| $key | string | |
| $value | mixed | |
| $ttl | integer|DateInterval|null | |
public function set(string $key, mixed $value, int|DateInterval|null $ttl = null): bool
{
$this->validateKey($key);
$ttl = $this->normalizeTtl($ttl);
if ($ttl <= self::TTL_EXPIRED) {
return $this->delete($key);
}
return apcu_store($key, $value, $ttl);
}
| public setMultiple( iterable $values, integer|DateInterval|null $ttl = null ): boolean | ||
| $values | iterable | |
| $ttl | integer|DateInterval|null | |
public function setMultiple(iterable $values, int|DateInterval|null $ttl = null): bool
{
$ttl = $this->normalizeTtl($ttl);
$values = $this->iterableToArray($values);
$this->validateKeysOfValues($values);
[$valuesWithStringKeys, $valuesWithIntegerKeys] = $this->splitValuesByKeyType($values);
/** @psalm-suppress RedundantCondition */
if (apcu_store($valuesWithStringKeys, null, $ttl) !== []) {
return false;
}
foreach ($valuesWithIntegerKeys as $key => $value) {
if (!apcu_store((string) $key, $value, $ttl)) {
return false;
}
}
return true;
}
Signup or Login in order to comment.