Redis implementation, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@149 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
61
RedisManager.php
Normal file
61
RedisManager.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Redis
|
||||
* @since 2010-07-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class RedisManager
|
||||
{
|
||||
|
||||
/**
|
||||
* Redis connections
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static protected $connections = array();
|
||||
|
||||
/**
|
||||
* Connect to redis
|
||||
*
|
||||
* @param string $name connection name. If not set 'default' will be used.
|
||||
* @param array $config Configuration array.
|
||||
*
|
||||
* @return Redis
|
||||
*/
|
||||
static public function connect($name = 'default', $config = null)
|
||||
{
|
||||
if (!isset(self::$connections[$name])) {
|
||||
if (!$config) {
|
||||
$config = Config::get('Redis')->$name;
|
||||
}
|
||||
|
||||
if (!is_array($config)) {
|
||||
throw new Exception('Connection parameters must be an array');
|
||||
}
|
||||
|
||||
$host = isset($config['host']) ? $config['host'] : 'localhost';
|
||||
$port = isset($config['port']) ? $config['port'] : 6379;
|
||||
$database = isset($config['database']) ? $config['database'] : 0;
|
||||
|
||||
/**
|
||||
* @var Redis
|
||||
*/
|
||||
$connection = new Redis();
|
||||
if (!$connection->connect($host, $port)) {
|
||||
throw new Exception('Failed to connect to Redis server at ' . $host . ':' . $port);
|
||||
}
|
||||
if ($database) {
|
||||
if (!$connection->select($database)) {
|
||||
throw new Exception('Failed to select Redis database with index ' . $database);
|
||||
}
|
||||
}
|
||||
self::$connections[$name] = $connection;
|
||||
}
|
||||
return self::$connections[$name];
|
||||
}
|
||||
}
|
36
cache/CacheKey.php
vendored
36
cache/CacheKey.php
vendored
@ -12,31 +12,33 @@
|
||||
class CacheKey
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Cacher
|
||||
*/
|
||||
protected $cacher;
|
||||
|
||||
/**
|
||||
* @var CacheKey
|
||||
*/
|
||||
protected $key;
|
||||
protected $params = '';
|
||||
protected $expire = 0;
|
||||
|
||||
/**
|
||||
* @param Cacher $cacher
|
||||
* @param string $key
|
||||
* @param mixed $params
|
||||
* @param iCacheable $cacheable
|
||||
* @param int $expire
|
||||
* @return CacheKey
|
||||
*/
|
||||
public function __construct($key, $params = array(), $cacheable)
|
||||
public function __construct($cacher, $key, $params = array(), $expire = 0)
|
||||
{
|
||||
$this->cacher = $cacher;
|
||||
$this->key = $key;
|
||||
if (!$cacheable instanceof iCacheable) {
|
||||
throw new GeneralException('CacheKey depends on iCacheable instance');
|
||||
if ($params) {
|
||||
$params = (is_array($params)) ? implode('|', $params) : $params;
|
||||
$this->key = $key . '_' . $params;
|
||||
}
|
||||
$this->cache = $cacheable->getCache();
|
||||
$this->expire = $cacheable->getKeyExpire($this->key);
|
||||
$this->params = (is_array($params)) ? implode('|', $params) : $params;
|
||||
}
|
||||
|
||||
protected function getCacheKey()
|
||||
{
|
||||
$params = ($this->params) ? ('_' . $this->params) : '';
|
||||
return $this->key . $params;
|
||||
$this->expire = $expire;
|
||||
}
|
||||
|
||||
protected function getExpire()
|
||||
@ -54,7 +56,7 @@ class CacheKey
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->cache->get($this->getCacheKey());
|
||||
return $this->cacher->get($this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,11 +64,11 @@ class CacheKey
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
return $this->cache->set($this->getCacheKey(), $value, $this->expire);
|
||||
return $this->cacher->set($this->key, $value, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->getCacheKey());
|
||||
return $this->cacher->del($this->key);
|
||||
}
|
||||
}
|
48
cache/CacheKeySet.php
vendored
48
cache/CacheKeySet.php
vendored
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-10
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CacheKeySet extends CacheKey
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
$item_key = $this->getCacheKey();
|
||||
|
||||
if (!is_array($set) || !array_key_exists($item_key, $set)) {
|
||||
return false;
|
||||
}
|
||||
return $this->cache->get($item_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
if (!is_array($set)) {
|
||||
$set = array();
|
||||
}
|
||||
|
||||
$item_key = $this->getCacheKey();
|
||||
if (!$this->cache->set($item_key, $value, $this->expire)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$set[$item_key] = $this->cache->getExpire($this->expire);
|
||||
return $this->cache->set($this->key, $set, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->key);
|
||||
}
|
||||
}
|
11
cache/MemcacheCache.php
vendored
11
cache/MemcacheCache.php
vendored
@ -26,7 +26,6 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected $expire = 3600;
|
||||
|
||||
protected $keys = array();
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
@ -150,19 +149,11 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
if (!isset($this->keys[$key])) {
|
||||
$this->keys[$key] = md5($this->key_salt . $key);
|
||||
}
|
||||
return $this->keys[$key];
|
||||
return md5($this->key_salt . $key);
|
||||
}
|
||||
|
||||
public function getExpire($expire)
|
||||
{
|
||||
return ($expire > 0) ? $expire : $this->expire;
|
||||
}
|
||||
|
||||
public function cleanKeys()
|
||||
{
|
||||
$this->keys = array();
|
||||
}
|
||||
}
|
24
cache/iCacheable.php
vendored
24
cache/iCacheable.php
vendored
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage cache
|
||||
* @since 2010-03-12
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iCacheable
|
||||
{
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache();
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Expiration time in seconds
|
||||
*/
|
||||
public function getKeyExpire($key);
|
||||
}
|
@ -151,6 +151,9 @@ abstract class DbStatement
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function fetchPairs()
|
||||
{
|
||||
$data = array();
|
||||
@ -179,4 +182,4 @@ abstract class DbStatement
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function driverExecute($sql);
|
||||
}
|
||||
}
|
||||
|
175
model/Model.php
175
model/Model.php
@ -12,7 +12,7 @@
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Model implements iCacheable
|
||||
abstract class Model
|
||||
{
|
||||
|
||||
/**
|
||||
@ -45,13 +45,12 @@ abstract class Model implements iCacheable
|
||||
|
||||
protected $table;
|
||||
|
||||
protected $connection = 'default';
|
||||
protected $key = 'id';
|
||||
|
||||
|
||||
protected $key = 'id';
|
||||
|
||||
public function __construct()
|
||||
public function __construct($connection = 'default')
|
||||
{
|
||||
$this->db = Db::connect($this->connection);
|
||||
$this->db = Db::connect($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -59,7 +58,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function getInsertId()
|
||||
{
|
||||
return $this->db->getInsertId($this->table(false), $this->key);
|
||||
return $this->db->getInsertId($this->table(), $this->key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,7 +85,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
$sql = 'SELECT * FROM ' . $this->table() . ' WHERE ' . $this->identify($this->key) . '=?';
|
||||
$sql = 'SELECT * FROM :table WHERE :pk=?';
|
||||
return $this->fetch($sql, $id);
|
||||
}
|
||||
|
||||
@ -97,7 +96,7 @@ abstract class Model implements iCacheable
|
||||
*/
|
||||
public function insert($data, $on_duplicate = array())
|
||||
{
|
||||
$affected = $this->db->insert($this->table(false), $data, $on_duplicate);
|
||||
$affected = $this->db->insert($this->table(), $data, $on_duplicate);
|
||||
return ($this->getInsertId()) ? $this->getInsertId() : $affected;
|
||||
}
|
||||
|
||||
@ -111,33 +110,28 @@ abstract class Model implements iCacheable
|
||||
if (is_int($where)) {
|
||||
$where = $this->identify($this->key) . '=' . (int) $where;
|
||||
}
|
||||
return $this->db->update($this->table(false), $data, $where);
|
||||
return $this->db->update($this->table(), $data, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|array $where Int or array ids
|
||||
* @param int $id Int id
|
||||
* @return int Number of affected rows
|
||||
*/
|
||||
public function delete($where)
|
||||
public function delete($id)
|
||||
{
|
||||
if (is_array($where)) {
|
||||
$where = $this->identify($this->key) . ' IN (' . $this->quote($where) . ')';
|
||||
} else {
|
||||
$where = $this->identify($this->key) . '=' . (int) $where;
|
||||
}
|
||||
return $this->db->delete($this->table(false), $where);
|
||||
$where = $this->identify($this->key) . '=' . (int) $id;
|
||||
return $this->db->delete($this->table(), $where);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $autoindent
|
||||
* @return string
|
||||
*/
|
||||
protected function table($autoindent = true)
|
||||
protected function table()
|
||||
{
|
||||
if (!$this->table) {
|
||||
$this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
|
||||
}
|
||||
return $autoindent ? $this->identify($this->table) : $this->table;
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,6 +174,75 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
|
||||
/**
|
||||
* This method appends to params table and primary key.
|
||||
* So they can be accessed thru `:table` and `:pk` placeholders.
|
||||
*
|
||||
* @return DbStatement
|
||||
*/
|
||||
protected function query($sql, $params = array())
|
||||
{
|
||||
if (!is_array($params)) {
|
||||
$params = array($params);
|
||||
}
|
||||
$params = array(
|
||||
'table' => new DbExpr($this->identify($this->table())),
|
||||
'pk' => new DbExpr($this->identify($this->key)),
|
||||
) + $params;
|
||||
return $this->db->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param string $field
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetchField($sql, $params = array(), $field, $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetchField($field);
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetch($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetch();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey $cache_key
|
||||
*/
|
||||
protected function fetchAll($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->query($sql, $params)->fetchAll();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/* Cache workaround */
|
||||
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache()
|
||||
@ -191,29 +254,18 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return int
|
||||
*/
|
||||
public function getKeyExpire($key)
|
||||
{
|
||||
return (isset($this->cache_keys[$key])) ? ($this->cache_keys[$key] * 60) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $params
|
||||
* @return CacheKey
|
||||
*/
|
||||
protected function cacheKey($name, $params = array())
|
||||
{
|
||||
if (substr(strtolower($name), -3, 3) == 'set') {
|
||||
return new CacheKeySet($name, $params, $this);
|
||||
}
|
||||
return new CacheKey($name, $params, $this);
|
||||
$expire = (isset($this->cache_keys[$name])) ? ($this->cache_keys[$name] * 60) : 0;
|
||||
return new CacheKey($this->getCache(), $name, $params, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheKey | CacheKeySet $cache
|
||||
* @param CacheKey $cache
|
||||
*/
|
||||
protected function addCleanCache($cache)
|
||||
{
|
||||
@ -228,53 +280,4 @@ abstract class Model implements iCacheable
|
||||
}
|
||||
$this->caches_clean = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param string $field
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetchField($sql, $params = array(), $field, $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetchField($field);
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetch($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetch();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @param CacheKey | CacheKeySet $cache_key
|
||||
*/
|
||||
protected function fetchAll($sql, $params = array(), $cache_key = null)
|
||||
{
|
||||
if (!$cache_key || !$result = $cache_key->get()) {
|
||||
$result = $this->db->query($sql, $params)->fetchAll();
|
||||
if ($cache_key) {
|
||||
$cache_key->set($result);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ class SessionModel extends Model
|
||||
|
||||
public function read($id)
|
||||
{
|
||||
$sql = 'SELECT `data` FROM ' . $this->table() . ' WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()';
|
||||
$sql = 'SELECT `data` FROM :table WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()';
|
||||
return (string) $this->fetchField($sql, $id, 'data');
|
||||
}
|
||||
|
||||
@ -45,21 +45,23 @@ class SessionModel extends Model
|
||||
|
||||
$ip = Env::Server('HTTP_X_FORWARDED_FOR', Env::Server('REMOTE_ADDR'));
|
||||
|
||||
$sql = 'INSERT INTO ' . $this->table() . ' (`id`, `expires`, `ip`, `user_id`, `data`)'
|
||||
. ' VALUES(' . $this->quote($id) . ', UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', INET_ATON(' . $this->quote($ip) . '), ' . $user_id . ', ' . $this->quote($data) . ')'
|
||||
. ' ON DUPLICATE KEY UPDATE `expires`=UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', `ip`=INET_ATON(' . $this->quote($ip) . '), `user_id`=' . $user_id . ', `data`=' . $this->quote($data);
|
||||
$affected = $this->db->query($sql)->affectedRows();
|
||||
return (bool) ($this->getInsertId()) ? $this->getInsertId() : $affected;
|
||||
$update = array(
|
||||
'expires' => new DbExpr('UNIX_TIMESTAMP() + ' . (int) $this->life_time),
|
||||
'ip' => new DbExpr('INET_ATON(' . $this->quote($ip) . ')'),
|
||||
'user_id' => $user_id,
|
||||
'data' => $data
|
||||
);
|
||||
return (bool) $this->insert(array('id' => $id) + $update, $update);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
return (bool) $this->db->delete($this->table(false), array('`id`=?' => (string) $id));
|
||||
return (bool) $this->db->delete($this->table(), array('`id`=?' => (string) $id));
|
||||
}
|
||||
|
||||
public function gc($max_life_time)
|
||||
{
|
||||
return (bool) $this->db->delete($this->table(false), '`expires` < UNIX_TIMESTAMP()');
|
||||
return (bool) $this->db->delete($this->table(), '`expires` < UNIX_TIMESTAMP()');
|
||||
}
|
||||
|
||||
/* End of Session handler methods */
|
||||
@ -67,6 +69,6 @@ class SessionModel extends Model
|
||||
|
||||
public function destroyByUserId($user_id)
|
||||
{
|
||||
return $this->db->delete($this->table(false), array('`user_id`=?' => $user_id));
|
||||
return $this->db->delete($this->table(), array('`user_id`=?' => $user_id));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user