0 follower

Final Class Yiisoft\Cache\Redis\RedisCache

InheritanceYiisoft\Cache\Redis\RedisCache
ImplementsPsr\SimpleCache\CacheInterface

RedisCache stores cache data in a Redis.

Please refer to {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by RedisCache.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Predis\ClientInterface $client )
$client \Predis\ClientInterface

Predis client instance to use.

                public function __construct(private ClientInterface $client)
{
    $this->connections = $this->client->getConnection();
}

            
clear() public method

public boolean clear ( )

                public function clear(): bool
{
    if ($this->isCluster()) {
        /** @psalm-suppress MixedAssignment, PossibleRawObjectIteration */
        foreach ($this->connections as $connection) {
            /** @var StreamConnection $connection */
            $client = new Client($connection->getParameters());
            $client->flushdb();
        }
        return true;
    }
    return $this->client->flushdb() !== null;
}

            
delete() public method

public boolean delete ( string $key )
$key string

                public function delete(string $key): bool
{
    return !$this->has($key) || $this->client->del($key) === 1;
}

            
deleteMultiple() public method

public boolean deleteMultiple ( iterable $keys )
$keys iterable

                public function deleteMultiple(iterable $keys): bool
{
    $keys = $this->iterableToArray($keys);
    /** @psalm-suppress MixedAssignment, MixedArgument */
    foreach ($keys as $index => $key) {
        if (!$this->has($key)) {
            unset($keys[$index]);
        }
    }
    /** @psalm-suppress MixedArgumentTypeCoercion */
    return empty($keys) || $this->client->del($keys) === count($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);
    /** @var string|null $value */
    $value = $this->client->get($key);
    return $value === null ? $default : unserialize($value);
}

            
getMultiple() public method

public iterable getMultiple ( iterable $keys, mixed $default null )
$keys iterable
$default mixed

                public function getMultiple(iterable $keys, mixed $default = null): iterable
{
    /** @var string[] $keys */
    $keys = $this->iterableToArray($keys);
    $this->validateKeys($keys);
    $values = array_fill_keys($keys, $default);
    if ($this->isCluster()) {
        foreach ($keys as $key) {
            /** @var string|null $value */
            $value = $this->get($key);
            if (null !== $value) {
                /** @psalm-suppress MixedAssignment */
                $values[$key] = unserialize($value);
            }
        }
    } else {
        /** @var null[]|string[] $valuesFromCache */
        $valuesFromCache = $this->client->mget($keys);
        $i = 0;
        /** @psalm-suppress MixedAssignment */
        foreach ($values as $key => $value) {
            $values[$key] = isset($valuesFromCache[$i]) ? unserialize($valuesFromCache[$i]) : $value;
            $i++;
        }
    }
    return $values;
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    $this->validateKey($key);
    /** @var int $ttl */
    $ttl = $this->client->ttl($key);
    /** "-1" - if the key exists but has no associated expire {@see https://redis.io/commands/ttl}. */
    return $ttl > 0 || $ttl === -1;
}

            
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
{
    $ttl = $this->normalizeTtl($ttl);
    if ($this->isExpiredTtl($ttl)) {
        return $this->delete($key);
    }
    $this->validateKey($key);
    /** @var Status|null $result */
    $result = $this->isInfinityTtl($ttl)
        ? $this->client->set($key, serialize($value))
        : $this->client->set($key, serialize($value), 'EX', $ttl);
    return $result !== null;
}

            
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);
    $keys = array_map('\strval', array_keys($values));
    $this->validateKeys($keys);
    $ttl = $this->normalizeTtl($ttl);
    $serializeValues = [];
    if ($this->isExpiredTtl($ttl)) {
        return $this->deleteMultiple($keys);
    }
    foreach ($values as $key => $value) {
        $serializeValues[$key] = serialize($value);
    }
    $results = [];
    if ($this->isCluster()) {
        foreach ($serializeValues as $key => $value) {
            $this->set((string)$key, $value, $this->isInfinityTtl($ttl) ? null : $ttl);
        }
    } else {
        if ($this->isInfinityTtl($ttl)) {
            $this->client->mset($serializeValues);
            return true;
        }
        $this->client->multi();
        $this->client->mset($serializeValues);
        foreach ($keys as $key) {
            $this->client->expire($key, (int)$ttl);
        }
        /** @var array|null $results */
        $results = $this->client->exec();
    }
    return !in_array(null, (array)$results, true);
}