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);
}
}
?>

View file

@ -58,7 +58,7 @@ $config = array(
'local' => array( 'local' => array(
'redis' => array( 'redis' => array(
'type' => 'redis', 'type' => 'redis',
'hostname' => 'localhost', 'hostname' => '127.0.0.1',
'port' => 6379, 'port' => 6379,
'database' => '0', 'database' => '0',
'namespace' => 'lb', 'namespace' => 'lb',
@ -67,7 +67,7 @@ $config = array(
'production' => array( 'production' => array(
'redis' => array( 'redis' => array(
'type' => 'redis', 'type' => 'redis',
'hostname' => 'localhost', 'hostname' => '127.0.0.1',
'port' => 6381, 'port' => 6381,
'database' => '0', 'database' => '0',
'namespace' => 'lb', 'namespace' => 'lb',

31
modules/user/create.php Normal file
View file

@ -0,0 +1,31 @@
<?php
class user_create extends CustomModule
{
//protected $ajax = true;
//protected $method = 'POST';
//protected $validate = array(
// 'username' => array(
// 'length:>:30' => 'Usernames may not be more than 30 characters.',
// 'regex:is:/[^a-z0-9-]+/i' => 'Usernames may only contain alphanumeric characters or dashes.',
// 'regex:is:/^(-.+|.+-)$/' => 'Usernames may not start or end with a dash.',
// 'regex:is:/-{2,}/' => 'Usernames may not have two or more dashes in a row.',
// ),
// 'email' => array(
// 'length:>:100' => 'Email addresses may not be more than 100 characters.',
// 'filter:email' => 'Your email address is invalid.',
// ),
// 'password' => array(
// 'length:<:8' => 'Passwords may not be less than 8 characters.',
// ),
//);
public function __default()
{
var_dump($this->redis->set('test', sha1(microtime(true))));
var_dump($this->redis->get('test'));
var_dump($this->redis->info());
}
}
?>

View file

@ -1,4 +1,4 @@
<form role="form" class="row col-xs-12 col-sm-6 col-sm-offset-3" method="post"> <form role="form" class="ajax row col-xs-12 col-sm-6 col-sm-offset-3" method="post" action="/user/create" onsubmit="return false">
<h1>Sign up <small>Fast and free!</small></h1> <h1>Sign up <small>Fast and free!</small></h1>
<?php <?php
echo $this->html->div( echo $this->html->div(
@ -7,7 +7,19 @@
'label' => 'Email Address', 'label' => 'Email Address',
'name' => 'email', 'name' => 'email',
'class' => 'form-control input-lg email required', 'class' => 'form-control input-lg email required',
'placeholder' => 'Enter your email', 'placeholder' => 'Your email address, double check it!',
))
);
echo $this->html->div(
array('class' => 'form-group'),
$this->html->input(array(
'label' => 'Username',
'name' => 'username',
'class' => 'form-control input-lg required',
'minlength' => 4,
'maxlength' => 50,
'placeholder' => 'At least four letters or numbers',
)) ))
); );
@ -18,7 +30,7 @@
'name' => 'password', 'name' => 'password',
'class' => 'form-control input-lg required', 'class' => 'form-control input-lg required',
'minlength' => 8, 'minlength' => 8,
'placeholder' => 'Enter your password', 'placeholder' => 'Use symbols along with letters and numbers',
)) ))
); );
?> ?>