Added arbitrary cache usage by key.

Pass a Model an optional second or third parameter to force the Model to check the cache before running the query and stashing the results in the key for future queries.
This commit is contained in:
Josh Sherman 2013-09-15 17:15:22 -04:00
parent 6b41edf659
commit 919ad37717

View file

@ -400,9 +400,10 @@ class Model extends Object
* Potentially populates the record set from the passed arguments. * Potentially populates the record set from the passed arguments.
* *
* @param mixed $type_or_parameters optional type of query or parameters * @param mixed $type_or_parameters optional type of query or parameters
* @param array $parameters optional data to create a query from * @param mixed $parameter_or_key optional data to create query or cache key
* @param string $passed_key optional key to use for caching
*/ */
public function execute($type_or_parameters = null, $parameters = null) public function execute($type_or_parameters = null, $parameters_or_key = null, $passed_key = null)
{ {
// Resets internal properties // Resets internal properties
foreach ($this->snapshot as $variable => $value) foreach ($this->snapshot as $variable => $value)
@ -416,7 +417,7 @@ class Model extends Object
// Loads the parameters into the object // Loads the parameters into the object
if (is_array($type_or_parameters)) if (is_array($type_or_parameters))
{ {
if (is_array($parameters)) if (is_array($parameters_or_key))
{ {
throw new Exception('You cannot pass in 2 query parameter arrays'); throw new Exception('You cannot pass in 2 query parameter arrays');
} }
@ -472,19 +473,19 @@ class Model extends Object
$this->loadParameters($type_or_parameters); $this->loadParameters($type_or_parameters);
} }
elseif (is_array($parameters)) elseif (is_array($parameters_or_key))
{ {
$this->prepareParameters($parameters); $this->prepareParameters($parameters_or_key);
if ($this->use_cache if ($this->use_cache
&& isset($parameters['conditions'][$this->columns['id']]) && isset($parameters_or_key['conditions'][$this->columns['id']])
&& count($parameters) == 1 && count($parameters_or_key) == 1
&& count($parameters['conditions']) == 1) && count($parameters_or_key['conditions']) == 1)
{ {
$cache_keys = array(); $cache_keys = array();
$sorted_records = array(); $sorted_records = array();
foreach ($parameters['conditions'][$this->columns['id']] as $id) foreach ($parameters_or_key['conditions'][$this->columns['id']] as $id)
{ {
$cache_keys[] = strtoupper($this->model) . '-' . $id; $cache_keys[] = strtoupper($this->model) . '-' . $id;
$sorted_records[$id] = true; $sorted_records[$id] = true;
@ -503,15 +504,15 @@ class Model extends Object
unset($cached); unset($cached);
foreach ($parameters['conditions'][$this->columns['id']] as $key => $id) foreach ($parameters_or_key['conditions'][$this->columns['id']] as $key => $id)
{ {
if (isset($partial_cache[$id])) if (isset($partial_cache[$id]))
{ {
unset($parameters['conditions'][$this->columns['id']][$key]); unset($parameters_or_key['conditions'][$this->columns['id']][$key]);
} }
} }
if (count($parameters['conditions'][$this->columns['id']]) == 0) if (count($parameters_or_key['conditions'][$this->columns['id']]) == 0)
{ {
$cache_key = true; $cache_key = true;
$cached = array_values($partial_cache); $cached = array_values($partial_cache);
@ -520,40 +521,50 @@ class Model extends Object
if ($this->columns['is_deleted']) if ($this->columns['is_deleted'])
{ {
$parameters['conditions'][$this->columns['is_deleted']] = '0'; $parameters_or_key['conditions'][$this->columns['is_deleted']] = '0';
} }
$this->loadParameters($parameters); $this->loadParameters($parameters_or_key);
} }
elseif (ctype_digit((string)$type_or_parameters)) elseif (ctype_digit((string)$type_or_parameters))
{ {
$cache_key = strtoupper($this->model) . '-' . $type_or_parameters; $cache_key = strtoupper($this->model) . '-' . $type_or_parameters;
$parameters = array($this->columns['id'] => $type_or_parameters); $parameters_or_key = array($this->columns['id'] => $type_or_parameters);
if ($this->columns['is_deleted']) if ($this->columns['is_deleted'])
{ {
$parameters[$this->columns['is_deleted']] = '0'; $parameters_or_key[$this->columns['is_deleted']] = '0';
} }
$this->loadParameters($parameters); $this->loadParameters($parameters_or_key);
} }
elseif (ctype_digit((string)$parameters)) elseif (ctype_digit((string)$parameters_or_key))
{ {
$cache_key = strtoupper($this->model) . '-' . $parameters; $cache_key = strtoupper($this->model) . '-' . $parameters_or_key;
$parameters = array($this->columns['id'] => $parameters); $parameters_or_key = array($this->columns['id'] => $parameters_or_key);
if ($this->columns['is_deleted']) if ($this->columns['is_deleted'])
{ {
$parameters[$this->columns['is_deleted']] = '0'; $parameters_or_key[$this->columns['is_deleted']] = '0';
} }
$this->loadParameters($parameters); $this->loadParameters($parameters_or_key);
} }
elseif ($this->columns['is_deleted']) elseif ($this->columns['is_deleted'])
{ {
$this->loadParameters(array($this->columns['is_deleted'] => '0')); $this->loadParameters(array($this->columns['is_deleted'] => '0'));
} }
if (is_string($parameters_or_key))
{
$passed_key = $parameters_or_key;
}
if (is_string($passed_key))
{
$cache_key = $passed_key;
}
// Starts with a basic SELECT ... FROM // Starts with a basic SELECT ... FROM
$this->sql = array( $this->sql = array(
'SELECT ' . (is_array($this->fields) ? implode(', ', $this->fields) : $this->fields), 'SELECT ' . (is_array($this->fields) ? implode(', ', $this->fields) : $this->fields),
@ -617,7 +628,7 @@ class Model extends Object
{ {
if (isset($cache_key)) if (isset($cache_key))
{ {
$this->cache->set($cache_key, $this->records[0]); $this->cache->set($cache_key, $passed_key ? $this->records : $this->records[0]);
} }
elseif (isset($cache_keys)) elseif (isset($cache_keys))
{ {
@ -1257,8 +1268,6 @@ class Model extends Object
{ {
$cache_keys[] = strtoupper($this->model) . '-' . $value; $cache_keys[] = strtoupper($this->model) . '-' . $value;
} }
var_dump($cache_keys);exit;
} }
// @todo Check if the column was passed in // @todo Check if the column was passed in