0 follower

Final Class Yiisoft\Cache\Apcu\ApcuCache

InheritanceYiisoft\Cache\Apcu\ApcuCache
ImplementsPsr\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 {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ApcCache supports.

Constants

Hide inherited constants

Constant Value Description Defined By
TTL_EXPIRED -1 Yiisoft\Cache\Apcu\ApcuCache
TTL_INFINITY 0 Yiisoft\Cache\Apcu\ApcuCache

Method Details

Hide inherited methods

clear() public method

public boolean clear ( )

                public function clear(): bool
{
    return apcu_clear_cache();
}

            
delete() public method

public boolean delete ( string $key )
$key string

                public function delete(string $key): bool
{
    $this->validateKey($key);
    return apcu_delete($key);
}

            
deleteMultiple() public method

public boolean deleteMultiple ( iterable $keys )
$keys iterable

                public function deleteMultiple(iterable $keys): bool
{
    $keys = $this->iterableToArray($keys);
    $this->validateKeys($keys);
    return apcu_delete($keys) === [];
}

            
get() public method

public mixed get ( string $key, mixed $default null )
$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;
}

            
getMultiple() public method

public iterable getMultiple ( iterable $keys, mixed $default null )
$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;
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    $this->validateKey($key);
    return apcu_exists($key);
}

            
set() public method

public boolean set ( string $key, mixed $value, integer|DateInterval|null $ttl null )
$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);
}

            
setMultiple() public method

public boolean setMultiple ( iterable $values, integer|DateInterval|null $ttl null )
$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;
}