Final Class Yiisoft\Cache\Memcached\Memcached
| Inheritance | Yiisoft\Cache\Memcached\Memcached |
|---|---|
| Implements | Psr\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} |
Public Methods
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
See also:
| 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 |
| $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);
}
| public boolean delete ( string $key ) | ||
| $key | string | |
public function delete(string $key): bool
{
$this->validateKey($key);
return $this->cache->delete($key);
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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);
}
| 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));
}
Signup or Login in order to comment.