Caching of simple queries against primary key.

This commit is contained in:
Josh Sherman 2011-10-30 20:49:23 -04:00
parent 4b96d28bb7
commit e104250597
4 changed files with 120 additions and 18 deletions

View file

@ -130,9 +130,9 @@ class Database extends Object
$instance->setDatabase($datasource['database']);
}
if (isset($datasource['caching']))
if (isset($datasource['cache']))
{
$instance->setCaching($datasource['caching']);
$instance->setCache($datasource['cache']);
}
}

View file

@ -85,7 +85,7 @@ abstract class Database_Common extends Object
* @access protected
* @var boolean
*/
protected $caching = false;
protected $cache = false;
/**
* Connection resource
@ -185,13 +185,13 @@ abstract class Database_Common extends Object
}
/**
* Set Caching
* Set Cache
*
* @param boolean whether or not to use cache
*/
public function setCaching($caching)
public function setCache($cache)
{
return $this->caching = $caching;
return $this->cache = $cache;
}
/**
@ -207,6 +207,18 @@ abstract class Database_Common extends Object
return $this->driver;
}
/**
* Get Cache
*
* Returns the status of caching for this datasource.
*
* @return string whether or not to use the cache
*/
public function getCache()
{
return $this->cache;
}
/**
* Opens database connection
*

View file

@ -42,6 +42,14 @@ class Model extends Object
*/
protected $cache = null;
/**
* Whether or not to use cache
*
* @access protected
* @var boolean
*/
protected $use_cache = false;
/**
* SQL Array
*
@ -249,8 +257,14 @@ class Model extends Object
parent::__construct();
// Gets an instance of the cache and database
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->cache = Cache::getInstance();
// @todo Datasource has no way of being set
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->caching = $this->db->getCache();
if ($this->caching)
{
$this->cache = Cache::getInstance();
}
// Builds out the query
if ($type_or_parameters != null)
@ -272,6 +286,7 @@ class Model extends Object
elseif (ctype_digit((string)$type_or_parameters))
{
$this->loadParameters(array($this->id => $type_or_parameters));
$cache_key = sha1('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $type_or_parameters);
}
elseif (ctype_digit((string)$parameters))
{
@ -310,7 +325,26 @@ class Model extends Object
break;
}
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
$query_database = true;
if (isset($cache_key))
{
$cached = $this->cache->get($cache_key);
}
if (isset($cached) && $cached)
{
$this->records = $cached;
}
else
{
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
if (isset($cache_key))
{
$this->cache->set($cache_key, $this->records);
}
}
}
else
{
@ -919,6 +953,11 @@ class Model extends Object
{
$sql .= ' WHERE ' . $this->id . ' = :' . $this->id . ' LIMIT 1;';
$input_parameters[':' . $this->id] = $this->record[$this->id];
if ($this->caching)
{
$this->cache->delete(sha1('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->id]));
}
}
// Executes the query

69
jar.php
View file

@ -1453,7 +1453,7 @@ abstract class Database_Common extends Object
* @access protected
* @var boolean
*/
protected $caching = false;
protected $cache = false;
/**
* Connection resource
@ -1553,13 +1553,13 @@ abstract class Database_Common extends Object
}
/**
* Set Caching
* Set Cache
*
* @param boolean whether or not to use cache
*/
public function setCaching($caching)
public function setCache($cache)
{
return $this->caching = $caching;
return $this->cache = $cache;
}
/**
@ -1575,6 +1575,18 @@ abstract class Database_Common extends Object
return $this->driver;
}
/**
* Get Cache
*
* Returns the status of caching for this datasource.
*
* @return string whether or not to use the cache
*/
public function getCache()
{
return $this->cache;
}
/**
* Opens database connection
*
@ -2205,9 +2217,9 @@ class Database extends Object
$instance->setDatabase($datasource['database']);
}
if (isset($datasource['caching']))
if (isset($datasource['cache']))
{
$instance->setCaching($datasource['caching']);
$instance->setCache($datasource['cache']);
}
}
@ -4059,6 +4071,14 @@ class Model extends Object
*/
protected $cache = null;
/**
* Whether or not to use cache
*
* @access protected
* @var boolean
*/
protected $use_cache = false;
/**
* SQL Array
*
@ -4266,8 +4286,14 @@ class Model extends Object
parent::__construct();
// Gets an instance of the cache and database
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->cache = Cache::getInstance();
// @todo Datasource has no way of being set
$this->db = Database::getInstance($this->datasource != '' ? $this->datasource : null);
$this->caching = $this->db->getCache();
if ($this->caching)
{
$this->cache = Cache::getInstance();
}
// Builds out the query
if ($type_or_parameters != null)
@ -4289,6 +4315,7 @@ class Model extends Object
elseif (ctype_digit((string)$type_or_parameters))
{
$this->loadParameters(array($this->id => $type_or_parameters));
$cache_key = sha1('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $type_or_parameters);
}
elseif (ctype_digit((string)$parameters))
{
@ -4327,7 +4354,26 @@ class Model extends Object
break;
}
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
$query_database = true;
if (isset($cache_key))
{
$cached = $this->cache->get($cache_key);
}
if (isset($cached) && $cached)
{
$this->records = $cached;
}
else
{
$this->records = $this->db->fetch(implode(' ', $this->sql), (count($this->input_parameters) == 0 ? null : $this->input_parameters));
if (isset($cache_key))
{
$this->cache->set($cache_key, $this->records);
}
}
}
else
{
@ -4936,6 +4982,11 @@ class Model extends Object
{
$sql .= ' WHERE ' . $this->id . ' = :' . $this->id . ' LIMIT 1;';
$input_parameters[':' . $this->id] = $this->record[$this->id];
if ($this->caching)
{
$this->cache->delete(sha1('PICKLES-' . $this->datasource . '-' . $this->table . '-' . $this->record[$this->id]));
}
}
// Executes the query