Browse Source
Redis implementation, #16
Redis implementation, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@149 4cb57b5f-5bbd-dd11-951b-001d605cbbc5master
pzinovkin
15 years ago
8 changed files with 173 additions and 183 deletions
-
61RedisManager.php
-
36cache/CacheKey.php
-
48cache/CacheKeySet.php
-
11cache/MemcacheCache.php
-
24cache/iCacheable.php
-
3model/DbStatement.php
-
143model/Model.php
-
20session/Session.model.php
@ -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]; |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue