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 === false) { 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; } return $this->connection->set($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; } } }