Final Class Yiisoft\Cache\Memcached\Memcached
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
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 \
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.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_SERVER_HOST | '127.0.0.1' | Yiisoft\ |
|
| DEFAULT_SERVER_PORT | 11211 | Yiisoft\ |
|
| DEFAULT_SERVER_WEIGHT | 1 | Yiisoft\ |
|
| TTL_EXPIRED | -1 | Yiisoft\ |
|
| TTL_INFINITY | 0 | Yiisoft\ |
Method Details
See also:
| public mixed __construct ( string $persistentId = '', array $servers = [], array $options = [] ) | ||
| $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. |
| $options | array |
List of memcached options. |
public function __construct(string $persistentId = '', array $servers = [], array $options = [])
{
$this->cache = new \Memcached($persistentId);
if ($options !== [] && @$this->cache->setOptions($options) === false) {
throw new InvalidArgumentException('Unsupported options', 0);
}
$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, integer|DateInterval|null $ttl = null ) | ||
| $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 boolean setMultiple ( iterable $values, integer|DateInterval|null $ttl = null ) | ||
| $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));
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.