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 \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 __construct( string $persistentId '', array $servers = [] ): mixed
$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 clear( ): boolean

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

            
delete() public method

public delete( string $key ): boolean
$key string

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

            
deleteMultiple() public method

public deleteMultiple( iterable $keys ): boolean
$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 get( string $key, mixed $default null ): mixed
$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 getMultiple( iterable $keys, mixed $default null ): iterable
$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 has( string $key ): boolean
$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 set( string $key, mixed $value, integer|DateInterval|null $ttl null ): boolean
$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 $this->cache->set($key, $value, $ttl);
}

            
setMultiple() public method

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

                public function setMultiple(iterable $values, int|DateInterval|null $ttl = null): bool
{
    $values = $this->iterableToArray($values);
    $this->validateKeysOfValues($values);
    return $this->cache->setMulti($values, $this->normalizeTtl($ttl));
}