This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sequenzia/Rails/Cache/Store/MemCachedStore.php
2013-10-02 01:12:58 -05:00

71 lines
1.7 KiB
PHP
Executable File

<?php
namespace Rails\Cache\Store;
use Memcached;
use Rails;
class MemCachedStore extends AbstractStore
{
/**
* Instance of Memcached.
*/
private $connection;
public function __construct(array $servers)
{
if (!$servers)
$servers = [['localhost', 11211]];
$this->connection = new Memcached('Rails.Application.' . Rails::application()->name());
$this->connection->addServers($servers);
}
public function connection()
{
return $this->connection;
}
public function read($key, array $params = [])
{
$value = $this->connection->get($key);
if (!$value) {
if ($this->connection->getResultCode() == Memcached::RES_NOTFOUND) {
return null;
} else {
# There was some kind of error.
}
} else {
return unserialize($value);
}
}
public function write($key, $val, array $params)
{
$val = serialize($val);
if (isset($params['expires_in'])) {
if (!ctype_digit((string)$params['expires_in']))
$expires_in = strtotime('+' . $params['expires_in']);
} else
$expires_in = 0;
$resp = $this->connection->add($key, $val, $expires_in);
}
public function delete($key, array $params)
{
$time = !empty($params['time']) ? $params['time'] : 0;
return $this->connection->delete($key, $time);
}
public function exists($key, array $params)
{
if ($this->connection->get($key))
return true;
else {
# An error could have occured.
return false;
}
}
}