diff --git a/RedisManager.php b/RedisManager.php new file mode 100644 index 0000000..230651d --- /dev/null +++ b/RedisManager.php @@ -0,0 +1,61 @@ + + * @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]; + } +} \ No newline at end of file diff --git a/cache/CacheKey.php b/cache/CacheKey.php index cd206e0..572518e 100644 --- a/cache/CacheKey.php +++ b/cache/CacheKey.php @@ -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); } } \ No newline at end of file diff --git a/cache/CacheKeySet.php b/cache/CacheKeySet.php deleted file mode 100644 index c470f78..0000000 --- a/cache/CacheKeySet.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @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); - } -} \ No newline at end of file diff --git a/cache/MemcacheCache.php b/cache/MemcacheCache.php index 7caa6e7..a5f72e2 100644 --- a/cache/MemcacheCache.php +++ b/cache/MemcacheCache.php @@ -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(); - } } \ No newline at end of file diff --git a/cache/iCacheable.php b/cache/iCacheable.php deleted file mode 100644 index 4f6b48b..0000000 --- a/cache/iCacheable.php +++ /dev/null @@ -1,24 +0,0 @@ - - * @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); -} \ No newline at end of file diff --git a/model/DbStatement.php b/model/DbStatement.php index adf4b08..301c574 100644 --- a/model/DbStatement.php +++ b/model/DbStatement.php @@ -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); -} \ No newline at end of file +} diff --git a/model/Model.php b/model/Model.php index dee4e7a..c57ea9b 100644 --- a/model/Model.php +++ b/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,65 +174,33 @@ abstract class Model implements iCacheable } /** - * @return Cache - */ - public function getCache() - { - if (!$this->cache) { - $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache')); - } - return $this->cache; - } - - /** - * @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 + * This method appends to params table and primary key. + * So they can be accessed thru `:table` and `:pk` placeholders. + * + * @return DbStatement */ - protected function cacheKey($name, $params = array()) + protected function query($sql, $params = array()) { - if (substr(strtolower($name), -3, 3) == 'set') { - return new CacheKeySet($name, $params, $this); + if (!is_array($params)) { + $params = array($params); } - return new CacheKey($name, $params, $this); + $params = array( + 'table' => new DbExpr($this->identify($this->table())), + 'pk' => new DbExpr($this->identify($this->key)), + ) + $params; + return $this->db->query($sql, $params); } - - /** - * @param CacheKey | CacheKeySet $cache - */ - protected function addCleanCache($cache) - { - $this->caches_clean[] = $cache; - } - - protected function cleanCaches() - { - // cleaning caches - foreach ($this->caches_clean as $cache) { - $cache->del(); - } - $this->caches_clean = array(); - } - + /** * @param string $sql * @param array $params * @param string $field - * @param CacheKey | CacheKeySet $cache_key + * @param CacheKey $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); + $result = $this->query($sql, $params)->fetchField($field); if ($cache_key) { $cache_key->set($result); } @@ -249,12 +211,12 @@ abstract class Model implements iCacheable /** * @param string $sql * @param array $params - * @param CacheKey | CacheKeySet $cache_key + * @param CacheKey $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(); + $result = $this->query($sql, $params)->fetch(); if ($cache_key) { $cache_key->set($result); } @@ -265,16 +227,57 @@ abstract class Model implements iCacheable /** * @param string $sql * @param array $params - * @param CacheKey | CacheKeySet $cache_key + * @param CacheKey $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(); + $result = $this->query($sql, $params)->fetchAll(); if ($cache_key) { $cache_key->set($result); } } return $result; } -} + + /* Cache workaround */ + + /** + * @return Cache + */ + public function getCache() + { + if (!$this->cache) { + $this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache')); + } + return $this->cache; + } + + /** + * @param string $name + * @param array $params + * @return CacheKey + */ + protected function cacheKey($name, $params = array()) + { + $expire = (isset($this->cache_keys[$name])) ? ($this->cache_keys[$name] * 60) : 0; + return new CacheKey($this->getCache(), $name, $params, $expire); + } + + /** + * @param CacheKey $cache + */ + protected function addCleanCache($cache) + { + $this->caches_clean[] = $cache; + } + + protected function cleanCaches() + { + // cleaning caches + foreach ($this->caches_clean as $cache) { + $cache->del(); + } + $this->caches_clean = array(); + } +} \ No newline at end of file diff --git a/session/Session.model.php b/session/Session.model.php index fd54b84..22d57cd 100644 --- a/session/Session.model.php +++ b/session/Session.model.php @@ -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)); } } \ No newline at end of file