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 \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 __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 |
| $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 delete( string $key ): boolean | ||
| $key | string | |
public function delete(string $key): bool
{
$this->validateKey($key);
return $this->cache->delete($key);
}
| 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;
}
| 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;
}
| 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;
}
| 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;
}
| 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);
}
| 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));
}
Signup or Login in order to comment.