0 follower

Final Class Yiisoft\Cache\ArrayCache

InheritanceYiisoft\Cache\ArrayCache
ImplementsPsr\SimpleCache\CacheInterface

ArrayCache provides caching for the current request only by storing the values in an array.

See {@see \Psr\SimpleCache\CacheInterface} for common cache operations that ArrayCache supports.

Constants

Hide inherited constants

Constant Value Description Defined By
EXPIRATION_EXPIRED -1 Yiisoft\Cache\ArrayCache
EXPIRATION_INFINITY 0 Yiisoft\Cache\ArrayCache

Method Details

Hide inherited methods

clear() public method

public boolean clear ( )

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

            
delete() public method

public boolean delete ( string $key )
$key string

                public function delete(string $key): bool
{
    $this->validateKey($key);
    unset($this->cache[$key]);
    return true;
}

            
deleteMultiple() public method

public boolean deleteMultiple ( iterable $keys )
$keys iterable

                public function deleteMultiple(iterable $keys): bool
{
    $keys = $this->iterableToArray($keys);
    /** @psalm-suppress RedundantCondition */
    $this->validateKeys($keys);
    foreach ($keys as $key) {
        $this->delete($key);
    }
    return true;
}

            
get() public method

public mixed get ( string $key, mixed $default null )
$key string
$default mixed

                public function get(string $key, mixed $default = null): mixed
{
    if ($this->has($key)) {
        $value = $this->cache[$key][0];
        if (is_object($value)) {
            return clone $value;
        }
        return $value;
    }
    return $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);
    /** @psalm-suppress RedundantCondition */
    $this->validateKeys($keys);
    $results = [];
    foreach ($keys as $key) {
        $value = $this->get($key, $default);
        $results[$key] = $value;
    }
    return $results;
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    $this->validateKey($key);
    return !$this->isExpired($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);
    $expiration = $this->ttlToExpiration(Ttl::from($ttl));
    if ($expiration < 0) {
        return $this->delete($key);
    }
    if (is_object($value)) {
        $value = clone $value;
    }
    $this->cache[$key] = [$value, $expiration];
    return true;
}

            
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
{
    $values = $this->iterableToArray($values);
    $this->validateKeysOfValues($values);
    foreach ($values as $key => $value) {
        $this->set((string) $key, $value, $ttl);
    }
    return true;
}