Built a simple wrapper for phpredis

Not every page / page view will need to connect to Redis, set it up so the connect isn't made until a call is made, lazy connect if you will.
This commit is contained in:
Josh Sherman 2013-12-15 16:30:12 -05:00
parent 061d7aee73
commit 7063b7aafe
5 changed files with 109 additions and 5 deletions

31
classes/CustomModule.php Normal file
View file

@ -0,0 +1,31 @@
<?php
class CustomModule extends Module
{
protected $redis = false;
public function __construct()
{
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)
{
}
*/
}
}
?>

30
classes/CustomRedis.php Normal file
View file

@ -0,0 +1,30 @@
<?php
class CustomRedis extends Object
{
protected $redis = false;
public function __call($name, $arguments)
{
if (!$this->redis)
{
$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)
{
exit('There was error connecting to Redis :(');
}
}
return call_user_func_array(array($this->redis, $name), $arguments);
}
}
?>