0 follower

Final Class Yiisoft\Cache\Memcached\Memcached

InheritanceYiisoft\Cache\Memcached\Memcached
ImplementsPsr\SimpleCache\CacheInterface

Memcached implements a cache application component based on memcached PECL extension.

Memcached can be configured with a list of memcached servers passed to the constructor. By default, Memcached assumes there is a memcached server running on localhost at port 11211.

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

Note, there is no security measure to protected data in memcached. All data in memcached can be accessed by any process running in the system.

Psalm Types

Name Value
NewServerType array{0: string, 1: integer, 2?: integer}

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_SERVER_HOST '127.0.0.1' Yiisoft\Cache\Memcached\Memcached
DEFAULT_SERVER_PORT 11211 Yiisoft\Cache\Memcached\Memcached
DEFAULT_SERVER_WEIGHT 1 Yiisoft\Cache\Memcached\Memcached
TTL_EXPIRED -1 Yiisoft\Cache\Memcached\Memcached
TTL_INFINITY 0 Yiisoft\Cache\Memcached\Memcached

Method Details

Hide inherited methods

__construct() public method
public mixed __construct ( string $persistentId '', array $servers = [] )
$persistentId string

The ID that identifies the Memcached instance. By default, the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistentId to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.

$servers array

List of memcached servers that will be added to the server pool.

                public function __construct(string $persistentId = '', array $servers = [])
{
    $this->cache = new \Memcached($persistentId);
    $this->initServers($servers, $persistentId);
}

            
clear() public method

public boolean clear ( )

                public function clear(): bool
{
    return $this->cache->flush();
}

            
delete() public method

public boolean delete ( string $key )
$key string

                public function delete(string $key): bool
{
    $this->validateKey($key);
    return $this->cache->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);
    foreach ($this->cache->deleteMulti($keys) as $result) {
        if ($result === false) {
            return false;
        }
    }
    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
{
    $this->validateKey($key);
    $value = $this->cache->get($key);
    if ($this->cache->getResultCode() === \Memcached::RES_SUCCESS) {
        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);
    /** @var array<string, mixed> $values */
    $values = array_fill_keys($keys, $default);
    $valuesFromCache = $this->cache->getMulti($keys);
    foreach ($values as $key => $value) {
        $values[$key] = $valuesFromCache[$key] ?? $value;
    }
    return $values;
}

            
has() public method

public boolean has ( string $key )
$key string

                public function has(string $key): bool
{
    $this->validateKey($key);
    $this->cache->get($key);
    return $this->cache->getResultCode() === \Memcached::RES_SUCCESS;
}

            
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);
    $ttl = $this->normalizeTtl($ttl);
    if ($ttl <= self::TTL_EXPIRED) {
        return $this->delete($key);
    }
    return $this->cache->set($key, $value, $ttl);
}

            
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);
    return $this->cache->setMulti($values, $this->normalizeTtl($ttl));
}