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(); parent::__construct();
$this->redis = new CustomRedis(); $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 try
{ {
$this->redis = new Redis(); $this->redis = parent::getInstance('Redis'); // new Redis();
$this->redis->connect($datasource['hostname'], $datasource['port']); $this->redis->connect($datasource['hostname'], $datasource['port']);
$this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':'); $this->redis->setOption(Redis::OPT_PREFIX, $datasource['namespace'] . ':');
$this->redis->select($datasource['database']); $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); return call_user_func_array(array($this->redis, $name), $arguments);
} }
} }

View file

@ -2,7 +2,90 @@
class RedisModel extends Object 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);
}
} }
?> ?>

View file

@ -2,7 +2,7 @@
class User extends RedisModel class User extends RedisModel
{ {
protected $prefix = 'user';
} }
?> ?>

View file

@ -5,16 +5,15 @@ class user_create extends CustomModule
// protected $ajax = true; // protected $ajax = true;
// protected $method = 'POST'; // protected $method = 'POST';
// protected $validate = array( // 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( // 'email' => array(
// 'length:>:100' => 'Email addresses may not be more than 100 characters.', // 'length:>:100' => 'Email addresses may not be more than 100 characters.',
// 'filter:email' => 'Your email address is invalid.', // 'filter:email' => 'Your email address is invalid.',
// ), // ),
// 'username' => array(
// 'length:<:4' => 'Usernames may not be less than 4 characters.',
// 'length:>:30' => 'Usernames may not be more than 50 characters.',
// 'regex:is:/[^a-z0-9]+/i' => 'Usernames may only contain letters and numbers.',
// ),
// 'password' => array( // 'password' => array(
// 'length:<:8' => 'Passwords may not be less than 8 characters.', // 'length:<:8' => 'Passwords may not be less than 8 characters.',
// ), // ),
@ -22,9 +21,43 @@ class user_create extends CustomModule
public function __default() public function __default()
{ {
var_dump($this->redis->set('test', sha1(microtime(true)))); try
var_dump($this->redis->get('test')); {
var_dump($this->redis->info()); $_POST = array(
'email' => 'foo@bar.com',
'username' => 'fubar',
'password' => 'insecure123',
);
$user = new User();
// Checks if the email or username is already in use
foreach (array('email', 'username') as $field)
{
if ($user->getUID($field, $_POST[$field]))
{
return array('error' => 'The ' . $field . ' is already in use.');
}
}
// Grabs the next UID
$uid = $user->nextUID();
// Writes the user data
$user->set('1001', array(
'username' => $_POST['username'],
'email' => $_POST['email'],
'password' => crypt($_POST['password'], '$2y$11$' . String::random(22) . '$'),
));
// Sets up the authentication cookie
//$user->setAuth()
}
catch (RedisException $e)
{
return array('error' => $e->getMessage());
}
} }
} }