Skip to content

add support for multiple memcache servers and options to EpiSession #70

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
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
48 changes: 42 additions & 6 deletions src/EpiSession_Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ class EpiSession_Memcached implements EpiSessionInterface
{
private static $connected = false;
private $key = null;
private $store= null;
private $store = null;

private $host = null;
private $port = null;
private $compress = null;
private $expiry = null;
private $options = null;

public function __construct($params = array())
{
Expand All @@ -13,10 +19,12 @@ public function __construct($params = array())
setcookie(EpiSession::COOKIE, $cookieVal, time()+1209600, '/');
$_COOKIE[EpiSession::COOKIE] = $cookieVal;
}

$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->port = !empty($params[1]) ? $params[1] : 11211;
$this->compress = !empty($params[2]) ? $params[2] : 0;
$this->expiry = !empty($params[3]) ? $params[3] : 3600;
$this->options = !empty($params[4]) ? $params[4] : array();
}

public function end()
Expand Down Expand Up @@ -51,7 +59,7 @@ public function set($key = null, $value = null)
return false;

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

Expand All @@ -63,10 +71,38 @@ private function connect($params = null)
if(class_exists('Memcached'))
{
$this->memcached = new Memcached;
if($this->memcached->addServer($this->host, $this->port))

if(count($this->host) !== count($this->port))
EpiException::raise(new EpiSessionMemcacheConnectException('Multiple servers require the same nubmber of hosts & ports'));

$servers = array();

// list of both host and port
if(is_array($this->host) && is_array($this->port))
foreach($this->host as $i => $host)
$servers[] = array($host, $this->port[$i]);

// all hosts with the same port
elseif(is_array($this->host) && is_scalar($this->port))
foreach($this->host as $i => $host)
$servers[] = array($host, $this->port);

// one host with multiple ports
elseif(is_scalar($this->host) && is_array($this->port))
foreach($this->port as $i => $port)
$servers[] = array($this->host, $port);

// single server
else
$servers[] = array($this->host, $this->port);

foreach($this->options as $name => $value)
$this->memcached->setOption($name, $value);

if($this->memcached->addServers($servers))
{
self::$connected = true;
$this->key = empty($key) ? $_COOKIE[EpiSession::COOKIE] : $key;
$this->key = $_COOKIE[EpiSession::COOKIE];
$this->store = $this->getAll();
return true;
}
Expand Down