Added column caching

If you're pulling data against a single column and returning a single column the UID will be cached out to a key that can easily be recalled the next time the same query is executed. On UPDATE and DELETE the corresponding keys are deleted.
This commit is contained in:
Josh Sherman 2013-09-10 22:43:52 -04:00
parent 72c9e85f5d
commit 9dcf3fa58e

View file

@ -9,7 +9,7 @@
* Redistribution of these files must retain the above copyright notice. * Redistribution of these files must retain the above copyright notice.
* *
* @author Josh Sherman <pickles@joshtronic.com> * @author Josh Sherman <pickles@joshtronic.com>
* @copyright Copyright 2007-2012, Josh Sherman * @copyright Copyright 2007-2013, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html * @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES * @package PICKLES
* @link https://github.com/joshtronic/pickles * @link https://github.com/joshtronic/pickles
@ -121,82 +121,102 @@ class Model extends Object
/** /**
* Field List * Field List
* *
* SQL: SELECT
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $fields = '*'; // SELECT protected $fields = '*';
/** /**
* Table Name * Table Name
* *
* SQL: FROM
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $table = false; // FROM protected $table = false;
/** /**
* Joins * Joins
* *
* SQL: JOIN
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $joins = false; // JOIN protected $joins = false;
/** /**
* [Index] Hints * [Index] Hints
* *
* SQL: USE INDEX
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $hints = false; // USE INDEX protected $hints = false;
/** /**
* Conditions * Conditions
* *
* SQL: WHERE
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $conditions = false; // WHERE protected $conditions = false;
/** /**
* Group * Group
* *
* SQL: GROUP BY
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $group = false; // GROUP BY protected $group = false;
/** /**
* Having * Having
* *
* SQL: HAVING
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $having = false; // HAVING protected $having = false;
/** /**
* Order * Order
* *
* SQL: ORDER BY
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $order = false; // ORDER BY protected $order = false;
/** /**
* Limit * Limit
* *
* SQL: LIMIT
*
* @access protected * @access protected
* @var mixed * @var mixed
*/ */
protected $limit = false; // LIMIT protected $limit = false;
/** /**
* Offset * Offset
* *
* SQL: OFFSET
*
* @access protected * @access protected
* @var mixed (string or array) * @var mixed (string or array)
*/ */
protected $offset = false; // OFFSET protected $offset = false;
/** /**
* Query Results * Query Results
@ -390,6 +410,9 @@ class Model extends Object
$this->$variable = $value; $this->$variable = $value;
} }
// Cache fields
$cache_field = false;
// Builds out the query // Builds out the query
if ($type_or_parameters != null) if ($type_or_parameters != null)
{ {
@ -401,6 +424,25 @@ class Model extends Object
throw new Exception('You cannot pass in 2 query parameter arrays'); throw new Exception('You cannot pass in 2 query parameter arrays');
} }
if ($this->use_cache && isset($type_or_parameters['conditions'])
&& count($type_or_parameters['conditions']) == 1)
{
$cache_key = $this->model . '-' . key($type_or_parameters['conditions'])
. '-' . current($type_or_parameters['conditions']);
$cache_id = $this->cache->get($cache_key);
$cache_field = key($type_or_parameters['conditions']);
if ($cache_id === false)
{
unset($cache_key);
}
else
{
$cache_key = $this->model . '-' . $cache_id;
}
}
if ($this->columns['is_deleted']) if ($this->columns['is_deleted'])
{ {
$type_or_parameters['conditions'][$this->columns['is_deleted']] = '0'; $type_or_parameters['conditions'][$this->columns['is_deleted']] = '0';
@ -487,6 +529,13 @@ class Model extends Object
(count($this->input_parameters) == 0 ? null : $this->input_parameters) (count($this->input_parameters) == 0 ? null : $this->input_parameters)
); );
if ($this->use_cache && $cache_field && $this->count() == 1)
{
$this->cache->set($this->model . '-' . $cache_field . '-' . $this->records[0][$cache_field], $this->records[0][$this->columns['id']]);
$cache_key = $this->model . '-' . $this->records[0][$this->columns['id']];
}
if (isset($cache_key) && $this->use_cache) if (isset($cache_key) && $this->use_cache)
{ {
$this->cache->set($cache_key, $this->records); $this->cache->set($cache_key, $this->records);
@ -1389,7 +1438,24 @@ class Model extends Object
// Clears the cache // Clears the cache
if ($update && $this->use_cache) if ($update && $this->use_cache)
{ {
$this->cache->delete($this->model . '-' . $this->record[$this->columns['id']]); $cache_keys = array($this->model . '-' . $this->record[$this->columns['id']]);
foreach ($this->original as $original)
{
if ($original['id'] == $this->record['id'])
{
foreach ($this->original[0] as $column => $value)
{
if (!String::isEmpty($value) && !in_array($column, $this->columns)
&& $value != $this->record[$column])
{
$cache_keys[] = $this->model . '-' . $column . '-' . $value;
}
}
}
}
$this->cache->delete($cache_keys);
} }
return $results; return $results;
@ -1443,7 +1509,17 @@ class Model extends Object
// Clears the cache // Clears the cache
if ($this->use_cache) if ($this->use_cache)
{ {
$this->cache->delete($this->model . '-' . $this->record[$this->columns['id']]); $cache_keys = $this->model . '-' . $this->record[$this->columns['id']];
foreach ($this->record as $column => $value)
{
if (!in_array($column, $this->columns))
{
$cache_keys[] = $this->model . '-' . $column . '-' . $value;
}
}
$this->cache->delete($cache_keys);
} }
return $results; return $results;