0 follower

Final Class Yiisoft\Test\Support\SimpleCache\MemorySimpleCache

InheritanceYiisoft\Test\Support\SimpleCache\MemorySimpleCache
ImplementsPsr\SimpleCache\CacheInterface

Constants

Hide inherited constants

Constant Value Description Defined By
EXPIRATION_EXPIRED -1 Yiisoft\Test\Support\SimpleCache\MemorySimpleCache
EXPIRATION_INFINITY 0 Yiisoft\Test\Support\SimpleCache\MemorySimpleCache

Property Details

Hide inherited properties

$cache protected property
protected array<string, array<integer, mixed>> $cache = []
$returnOnClear public property
public boolean $returnOnClear true
$returnOnDelete public property
public boolean $returnOnDelete true
$returnOnSet public property
public boolean $returnOnSet true

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( array $cacheData = [] )
$cacheData array

                public function __construct(array $cacheData = [])
{
    $this->setMultiple($cacheData);
}

            
clear() public method

public boolean clear ( )

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

            
delete() public method

public boolean delete ( string $key )
$key string

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

            
deleteMultiple() public method

public boolean deleteMultiple ( iterable $keys )
$keys iterable

                public function deleteMultiple(iterable $keys): bool
{
    $keys = $this->iterableToArray($keys);
    $this->validateKeys($keys);
    /** @var string[] $keys */
    foreach ($keys as $key) {
        $this->delete($key);
    }
    return $this->returnOnDelete;
}

            
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);
    if (array_key_exists($key, $this->cache) && !$this->isExpired($key)) {
        $value = $this->cache[$key][0];
        if (is_object($value)) {
            $value = 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);
    $this->validateKeys($keys);
    /** @psalm-var string[] $keys */
    $result = [];
    foreach ($keys as $key) {
        $result[$key] = $this->get($key, $default);
    }
    return $result;
}

            
getValues() public method

Get stored data

public array<array-key, mixed> getValues ( )

                public function getValues(): array
{
    $result = [];
    foreach ($this->cache as $key => $value) {
        $result[$key] = $value[0];
    }
    return $result;
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    $this->validateKey($key);
    /** @psalm-var string $key */
    return isset($this->cache[$key]) && !$this->isExpired($key);
}

            
set() public method

public boolean set ( string $key, mixed $value, null|integer|DateInterval $ttl null )
$key string
$value mixed
$ttl null|integer|DateInterval

                public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
    $this->validateKey($key);
    $expiration = $this->ttlToExpiration($ttl);
    if ($expiration < 0) {
        return $this->delete($key);
    }
    if (is_object($value)) {
        $value = clone $value;
    }
    $this->cache[$key] = [$value, $expiration];
    return $this->returnOnSet;
}

            
setMultiple() public method

public boolean setMultiple ( iterable $values, null|integer|DateInterval $ttl null )
$values iterable
$ttl null|integer|DateInterval

                public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
    $values = $this->iterableToArray($values);
    $this->validateKeysOfValues($values);
    foreach ($values as $key => $value) {
        $this->set((string) $key, $value, $ttl);
    }
    return $this->returnOnSet;
}