Skip to content

Memcached improvements #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 50 additions & 58 deletions src/EpiCache_Memcached.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,64 @@
<?php
class EpiCache_Memcached extends EpiCache
{
private static $connected = false;
private $memcached = null;
private $host = null;
private $port = null;
private $compress = null;
private $expiry = null;
public function __construct($params = array())
{
$this->host = !empty($params[0]) ? $params[0] : 'localhost';
$this->port = !empty($params[1]) ? $params[1] : 11211;;
$this->compress = !empty($params[2]) ? $params[2] : 0;;
$this->expiry = !empty($params[3]) ? $params[3] : 3600;
}

public function delete($key, $timeout = 0)
{
if(!$this->connect() || empty($key))
return false;
class EpiCache_Memcached extends EpiCache {

return $this->memcached->delete($key, $timeout);
}
private static $connected = false;
private $memcached = null;
private $host = null;
private $port = null;
private $compress = null;
private $expiry = null;

public function get($key, $useCache = true)
{
if(!$this->connect() || empty($key))
{
return null;
public function __construct($params = array()) {
$this->host = !empty($params[0]) ? $params[0] : 'localhost';
$this->port = !empty($params[1]) ? $params[1] : 11211;
$this->compress = isset($params[2]) ? $params[2] : 0;
$this->expiry = isset($params[3]) ? $params[3] : 3600;
}
else if($useCache && $getEpiCache = $this->getEpiCache($key))
{
return $getEpiCache;

public function delete($key, $timeout = 0) {
if (!$this->connect() || empty($key))
return false;

return $this->memcached->delete($key, $timeout);
}

public function get($key, $useCache = true) {
if (!$this->connect() || empty($key)) {
return null;
} else if ($useCache && $getEpiCache = $this->getEpiCache($key)) {
return $getEpiCache;
} else {
$value = $this->memcached->get($key);
$this->setEpiCache($key, $value);
return $value;
}
}
else
{
$value = $this->memcached->get($key);
$this->setEpiCache($key, $value);
return $value;

public function set($key = null, $value = null, $ttl = null) {
if (!$this->connect() || empty($key) || $value === null)
return false;

$expiry = $ttl === null ? $this->expiry : $ttl;
$this->memcached->set($key, $value, $expiry);
$this->setEpiCache($key, $value);
return true;
}
}

public function set($key = null, $value = null, $ttl = null)
{
if(!$this->connect() || empty($key) || $value === null)
return false;
private function connect() {
if (self::$connected === true)
return true;

$expiry = $ttl === null ? $this->expiry : $ttl;
$this->memcached->set($key, $value, $expiry);
$this->setEpiCache($key, $value);
return true;
}
if (class_exists('Memcached')) {
$this->memcached = new Memcached;

private function connect()
{
if(self::$connected === true)
return true;
if ($this->memcached->addServer($this->host, $this->port))
return self::$connected = true;
else
EpiException::raise(new EpiCacheMemcacheConnectException('Could not connect to memcache server'));
}

if(class_exists('Memcached'))
{
$this->memcached = new Memcached;

if($this->memcached->addServer($this->host, $this->port))
return self::$connected = true;
else
EpiException::raise(new EpiCacheMemcacheConnectException('Could not connect to memcache server'));
EpiException::raise(new EpiCacheMemcacheClientDneException('No memcache client exists'));
}

EpiException::raise(new EpiCacheMemcacheClientDneException('No memcache client exists'));
}
}
157 changes: 79 additions & 78 deletions src/EpiSession_Memcached.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
<?php
class EpiSession_Memcached implements EpiSessionInterface
{
private static $connected = false;
private $key = null;
private $store= null;

public function __construct($params = array())
{
if(empty($_COOKIE[EpiSession::COOKIE]))
{
$cookieVal = md5(uniqid(rand(), true));
setcookie(EpiSession::COOKIE, $cookieVal, time()+1209600, '/');
$_COOKIE[EpiSession::COOKIE] = $cookieVal;

class EpiSession_Memcached implements EpiSessionInterface {

private static $connected = false;
private $memcached = false;
private $key = null;
private $store = null;
private $host = null;
private $port = null;
private $compress = null;
private $expiry = null;

public function __construct($params = array()) {
if (empty($_COOKIE[EpiSession::COOKIE])) {
$key = md5(uniqid(rand(), true));
setcookie(EpiSession::COOKIE, $key, time() + 1209600, '/');
$_COOKIE[EpiSession::COOKIE] = $key;
}
$this->host = !empty($params[0]) ? $params[0] : 'localhost';
$this->port = !empty($params[1]) ? $params[1] : 11211;
$this->compress = isset($params[2]) ? $params[2] : 0;
$this->expiry = isset($params[3]) ? $params[3] : 3600;
}
$this->host = !empty($params[0]) ? $params[0] : 'localhost';
$this->port = !empty($params[1]) ? $params[1] : 11211;;
$this->compress = !empty($params[2]) ? $params[2] : 0;;
$this->expiry = !empty($params[3]) ? $params[3] : 3600;
}

public function end()
{
if(!$this->connect())
return;

$this->memcached->delete($this->key);
$this->store = null;
setcookie(EpiSession::COOKIE, null, time()-86400);
}

public function get($key = null)
{
if(!$this->connect() || empty($key) || !isset($this->store[$key]))
return false;

return $this->store[$key];
}

public function getAll()
{
if(!$this->connect())
return;

return $this->memcached->get($this->key);
}

public function set($key = null, $value = null)
{
if(!$this->connect() || empty($key))
return false;

$this->store[$key] = $value;
$this->memcached->set($this->key, $this->store);
return $value;
}

private function connect($params = null)
{
if(self::$connected)
return true;

if(class_exists('Memcached'))
{
$this->memcached = new Memcached;
if($this->memcached->addServer($this->host, $this->port))
{
self::$connected = true;
$this->key = empty($key) ? $_COOKIE[EpiSession::COOKIE] : $key;
$this->store = $this->getAll();
return true;
}
else
{
EpiException::raise(new EpiSessionMemcacheConnectException('Could not connect to memcache server'));
}

public function end() {
if (!$this->connect())
return;

$this->memcached->delete($this->key);
$this->store = null;
setcookie(EpiSession::COOKIE, null, time() - 86400);
}

public function get($key = null) {
if (!$this->connect() || empty($key) || !isset($this->store[$key]))
return false;

return $this->store[$key];
}

public function getAll() {
if (!$this->connect())
return;

return $this->memcached->get($this->key);
}
EpiException::raise(new EpiSessionMemcacheClientDneException('Could not connect to memcache server'));
}

public function set($key = null, $value = null) {
if (!$this->connect() || empty($key))
return false;

$this->store[$key] = $value;
$this->memcached->set($this->key, $this->store, $this->expiry);
return $value;
}

private function connect($params = null) {
if (self::$connected)
return true;

if (class_exists('Memcached')) {
$this->memcached = new Memcached;
if ($this->memcached->addServer($this->host, $this->port)) {
self::$connected = true;
$this->key = $_COOKIE[EpiSession::COOKIE];
$this->store = $this->getAll();
return true;
} else {
EpiException::raise(new EpiSessionMemcacheConnectException('Could not connect to memcache server'));
}
}
EpiException::raise(new EpiSessionMemcacheClientDneException('Could not connect to memcache server'));
}

}

class EpiSessionMemcacheConnectException extends EpiException {

}

class EpiSessionMemcacheConnectException extends EpiException {}
class EpiSessionMemcacheClientDneException extends EpiException {}
class EpiSessionMemcacheClientDneException extends EpiException {

}