Memcached adapter.
Constructor.
| Type | Description |
|---|---|
| array | Configuration |
NULL
public function __construct(array $config)
{
parent::__construct($config['identifier']);
if(class_exists('\Memcached', false) === false)
{
throw new RuntimeException(vsprintf("%s(): Memcached is not available.", array(__METHOD__)));
}
$this->memcached = new PHP_Memcached();
if($config['compress_data'] !== 1)
{
$this->memcached->setOption(PHP_Memcached::OPT_CONNECT_TIMEOUT, ($config['timeout'] * 1000)); // Multiply by 1000 to convert to ms
}
if($config['compress_data'] === false)
{
$this->memcached->setOption(PHP_Memcached::OPT_COMPRESSION, false);
}
// Add servers to the connection pool
foreach($config['servers'] as $server)
{
$this->memcached->addServer($server['server'], $server['port'], $server['weight']);
}
}
Destructor.
NULL
public function __destruct()
{
$this->memcached = null;
}
Store variable in the cache.
| Type | Description |
|---|---|
| string | Cache key |
| mixed | The variable to store |
| int | (optional) Time to live |
boolean
public function write($key, $value, $ttl = 0)
{
if($ttl !== 0)
{
$ttl += time();
}
if($this->memcached->replace("{$this->identifier}_{$key}", $value, $ttl) === false)
{
return $this->memcached->set("{$this->identifier}_{$key}", $value, $ttl);
}
return true;
}
Fetch variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
mixed
public function read($key)
{
return $this->memcached->get("{$this->identifier}_{$key}");
}
Delete a variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
boolean
public function delete($key)
{
return $this->memcached->delete("{$this->identifier}_{$key}", 0);
}
Clears the user cache.
boolean
public function clear()
{
return $this->memcached->flush();
}
Fetches variable from cache and stores it if it doesn't exist.
| Type | Description |
|---|---|
| string | Cache key |
| closure | Closure (anonymous function) that returns value to store if it doesn't already exist |
| int | (optional) Time to live |
mixed
Magic setter.
| Type | Description |
|---|---|
| string | Cache key |
| mixed | The variable to store |
NULL
Magic getter.
| Type | Description |
|---|---|
| string | Cache key |
mixed
Magic isset.
| Type | Description |
|---|---|
| string | Cache key |
boolean
Magic unsetter.
| Type | Description |
|---|---|
| string | Cache key |
NULL