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:
pzinovkin
2010-03-05 18:41:02 +00:00
parent 00259cfe93
commit 2d7f5391f8
6 changed files with 287 additions and 21 deletions

View File

@ -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;

View File

@ -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;
}
}