Working on this RedisModel class

Got it working fairly well, just trying to get all of my assumption / opinions baked in. Still need to come up with a decent way to generate session hashes to help avoid session collision . Incidentally Retwis seems to be susceptible to this.
This commit is contained in:
Josh Sherman 2013-12-15 22:59:39 -05:00
parent 7063b7aafe
commit 9fe9eb7acf
5 changed files with 140 additions and 38 deletions

View file

@ -9,22 +9,6 @@ class CustomModule extends Module
parent::__construct();
$this->redis = new CustomRedis();
/*
$datasource = $this->config->datasources['redis'];
try
{
$this->redis = new Redis();
$this->redis->connect($datasource['hostname'], $datasource['port']);
$this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':');
$this->redis->select($datasource['database']);
}
catch (RedisException $e)
{
}
*/
}
}

View file

@ -12,7 +12,7 @@ class CustomRedis extends Object
try
{
$this->redis = new Redis();
$this->redis = parent::getInstance('Redis'); // new Redis();
$this->redis->connect($datasource['hostname'], $datasource['port']);
$this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':');
$this->redis->select($datasource['database']);
@ -23,6 +23,8 @@ class CustomRedis extends Object
}
}
//var_dump($name, $arguments);
return call_user_func_array(array($this->redis, $name), $arguments);
}
}

View file

@ -2,7 +2,90 @@
class RedisModel extends Object
{
protected $redis = false;
protected $prefix = false;
public function __construct()
{
parent::__construct();
$this->redis = new CustomRedis();
}
public function key()
{
$parts = func_get_args();
if ($this->prefix)
{
array_unshift($parts, $this->prefix);
}
return strtolower(implode(':', $parts));
}
public function nextUID()
{
return $this->redis->incr($this->key('uid'));
}
public function getUID($variable, $value)
{
return $this->redis->get($this->key($variable, $value, 'uid'));
}
public function __call($name, $arguments)
{
$name = strtolower($name);
if ($name == 'set')
{
// Grabs our variables
$uid = $arguments[0];
$variables = $arguments[1];
$arguments = array();
// Assembles our new arguments
foreach ($variables as $key => $value)
{
$arguments[$this->key($uid, $key)] = $value;
}
// Sets us up for MSET or just SET
if (count($arguments) > 1)
{
$name = 'mset';
$arguments = array($arguments);
}
else
{
$arguments = array(key($arguments), current($arguments));
}
}
else
{
$base = substr($name, 0, 3);
if (in_array($base, array('set', 'get')))
{
$key = $this->key($arguments[0], substr($name, 3));
$name = $base;
}
switch ($base)
{
case 'set':
$arguments = array($key, $arguments[1]);
break;
case 'get':
$arguments = array($key);
break;
}
}
return call_user_func_array(array($this->redis, $name), $arguments);
}
}
?>