Memcache adapter.
Constructor.
| Type | Description |
|---|---|
| array | Configuration |
NULL
public function __construct(array $config)
{
parent::__construct($config['identifier']);
if(class_exists('\Memcache', false) === false)
{
throw new RuntimeException(vsprintf("%s(): Memcache is not available.", array(__METHOD__)));
}
$this->memcache = new PHP_Memcache();
if($config['compress_data'] !== false)
{
$this->compression = MEMCACHE_COMPRESSED;
}
// Add servers to the connection pool
foreach($config['servers'] as $server)
{
$this->memcache->addServer($server['server'], $server['port'], $server['persistent_connection'], $server['weight'], $config['timeout']);
}
}
Destructor.
NULL
public function __destruct()
{
if($this->memcache !== null)
{
$this->memcache->close();
}
}
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->memcache->replace("{$this->identifier}_{$key}", $value, $this->compression, $ttl) === false)
{
return $this->memcache->set("{$this->identifier}_{$key}", $value, $this->compression, $ttl);
}
return true;
}
Fetch variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
mixed
public function read($key)
{
return $this->memcache->get("{$this->identifier}_{$key}");
}
Delete a variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
boolean
public function delete($key)
{
return $this->memcache->delete("{$this->identifier}_{$key}", 0);
}
Clears the user cache.
boolean
public function clear()
{
return $this->memcache->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