Memcache caching, #18
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@120 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
10
Config.php
10
Config.php
@ -26,17 +26,17 @@ class Config extends ArrayObject
|
||||
*/
|
||||
static public function getInstance()
|
||||
{
|
||||
if (! isset(self::$_instance)) {
|
||||
if (!isset(self::$_instance)) {
|
||||
self::$_instance = new Config();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
static public function get($name)
|
||||
static public function get($name, $default = null)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
if (! $instance->offsetExists($name)) {
|
||||
throw new Exception('Configuration variable "' . $name . '" undefined');
|
||||
if (!$instance->offsetExists($name)) {
|
||||
return $default;
|
||||
}
|
||||
return $instance->offsetGet($name);
|
||||
}
|
||||
@ -59,7 +59,7 @@ class ConfigArray extends ArrayObject
|
||||
|
||||
public function offsetGet($index)
|
||||
{
|
||||
if (! $this->offsetExists($index)) {
|
||||
if (!$this->offsetExists($index)) {
|
||||
throw new Exception('Configuration variable "' . $index . '" undefined');
|
||||
}
|
||||
return parent::offsetGet($index);
|
||||
|
86
cache/Cache.php
vendored
Normal file
86
cache/Cache.php
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Cache
|
||||
{
|
||||
|
||||
/**
|
||||
* Add an item to the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function add($key, $value, $expire = 0);
|
||||
|
||||
/**
|
||||
* Decrement item's value
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function decrement($key, $decrement = 1);
|
||||
|
||||
/**
|
||||
* Delete item from the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $value
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function del($key);
|
||||
|
||||
/**
|
||||
* Flush all existing items
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function flush();
|
||||
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get($key);
|
||||
|
||||
/**
|
||||
* Increment item's value
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function increment($key, $increment = 1);
|
||||
|
||||
/**
|
||||
* Replace value of the existing item
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function replace($key, $value, $expire = 0);
|
||||
|
||||
/**
|
||||
* Store data in the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function set($key, $value, $expire = 0);
|
||||
}
|
37
cache/Cacher.php
vendored
Normal file
37
cache/Cacher.php
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Cacher
|
||||
{
|
||||
|
||||
/**
|
||||
* Initialized cachers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static protected $caches = array();
|
||||
|
||||
|
||||
static public function get($cacher, $config = null)
|
||||
{
|
||||
if (!isset(self::$caches[$cacher])) {
|
||||
if (!$config) {
|
||||
$config = Config::get($cacher);
|
||||
}
|
||||
$cache = new $cacher($config);
|
||||
if (!$cache instanceof Cache) {
|
||||
throw new Exception('Cache driver "' . $cacher . '" must extends Cache');
|
||||
}
|
||||
self::$caches[$cacher] = $cache;
|
||||
}
|
||||
return self::$caches[$cacher];
|
||||
}
|
||||
}
|
130
cache/MemcacheCache.php
vendored
Normal file
130
cache/MemcacheCache.php
vendored
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-04
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class MemcacheCache extends Cache
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Memcache
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->connection = new Memcache();
|
||||
|
||||
$required = array('hostname', 'port');
|
||||
foreach ($config as $c) {
|
||||
foreach ($required as $option) {
|
||||
if (!isset($c[$option])) {
|
||||
throw new Exception('Configuration must have a "' . $option . '".');
|
||||
}
|
||||
}
|
||||
$this->connection->addServer($c['hostname'], $c['port']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
public function add($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->add($key, $value, null, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement item's value
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $decrement
|
||||
* @return bool
|
||||
*/
|
||||
public function decrement($key, $decrement = 1)
|
||||
{
|
||||
return $this->connection->decrement($key, $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete item from the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $value
|
||||
* @return bool
|
||||
*/
|
||||
public function del($key)
|
||||
{
|
||||
return $this->connection->delete($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush all existing items
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
return $this->connection->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return $this->connection->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment item's value
|
||||
*
|
||||
* @param string $key
|
||||
* @param int $increment
|
||||
* @return bool
|
||||
*/
|
||||
public function increment($key, $increment = 1)
|
||||
{
|
||||
return $this->connection->increment($key, $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace value of the existing item
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $var
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
public function replace($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->replace($key, $value, null, $expire);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store data in the cache
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $expire
|
||||
* @return bool
|
||||
*/
|
||||
public function set($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->set($key, $value, null, $expire);
|
||||
}
|
||||
}
|
14
model/Db.php
14
model/Db.php
@ -32,18 +32,14 @@ class Db
|
||||
*
|
||||
* @return DbDriver
|
||||
*/
|
||||
static public function connect($name = null, $config = null)
|
||||
static public function connect($name = 'default', $config = null)
|
||||
{
|
||||
if ($name === null) {
|
||||
$name = 'default';
|
||||
}
|
||||
|
||||
if (! isset(self::$connections[$name])) {
|
||||
if (!isset(self::$connections[$name])) {
|
||||
if (!$config) {
|
||||
$config = Config::get('databases')->$name;
|
||||
$config = Config::get(__CLASS__)->$name;
|
||||
}
|
||||
|
||||
if (! is_array($config)) {
|
||||
if (!is_array($config)) {
|
||||
throw new Exception('Connection parameters must be an array');
|
||||
}
|
||||
|
||||
@ -55,7 +51,7 @@ class Db
|
||||
|
||||
$connection = new $driver($config);
|
||||
|
||||
if (! $connection instanceof DbDriver) {
|
||||
if (!$connection instanceof DbDriver) {
|
||||
throw new Exception('Database driver must extends DbDriver');
|
||||
}
|
||||
self::$connections[$name] = $connection;
|
||||
|
@ -22,19 +22,25 @@ abstract class Model
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Cache instance
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
protected $table;
|
||||
|
||||
protected $connection = 'default';
|
||||
|
||||
protected $key = 'id';
|
||||
|
||||
protected $key = 'id';
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = Db::connect($this->connection);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
@ -123,14 +129,25 @@ abstract class Model
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $autoescape
|
||||
* @param bool $autoindent
|
||||
* @return string
|
||||
*/
|
||||
public function table($autoescape = true)
|
||||
protected function table($autoindent = true)
|
||||
{
|
||||
if (!$this->table) {
|
||||
$this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
|
||||
}
|
||||
return $autoescape ? $this->identify($this->table) : $this->table;
|
||||
return $autoindent ? $this->identify($this->table) : $this->table;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
protected function cache()
|
||||
{
|
||||
if (!$this->cache) {
|
||||
$this->cache = Cacher::get(Config::get(__CLASS__, 'MemcacheCache'));
|
||||
}
|
||||
return $this->cache;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user