Added cache purging post-execution for multiple row UPDATEs

This commit is contained in:
Josh Sherman 2012-12-02 13:08:36 -05:00
parent 50664a22ac
commit 3290ee0135
2 changed files with 36 additions and 2 deletions

View file

@ -1085,6 +1085,9 @@ class Model extends Object
// Multiple row query / queries // Multiple row query / queries
if ($this->commit_type == 'queue') if ($this->commit_type == 'queue')
{ {
$update = false;
$cache_keys = array();
/** /**
* @todo I outta loop through twice to determine if it's an INSERT * @todo I outta loop through twice to determine if it's an INSERT
* or an UPDATE. As it stands, you could run into a scenario where * or an UPDATE. As it stands, you could run into a scenario where
@ -1097,6 +1100,8 @@ class Model extends Object
// Performs an UPDATE with multiple queries // Performs an UPDATE with multiple queries
if (array_key_exists($this->columns['id'], $record)) if (array_key_exists($this->columns['id'], $record))
{ {
$update = true;
if (!isset($sql)) if (!isset($sql))
{ {
$sql = ''; $sql = '';
@ -1112,6 +1117,10 @@ class Model extends Object
$update_fields[] = $field . ' = ?'; $update_fields[] = $field . ' = ?';
$input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value); $input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value);
} }
else
{
$cache_keys[] = $this->model . '-' . $value;
}
} }
// @todo Check if the column was passed in // @todo Check if the column was passed in
@ -1204,7 +1213,15 @@ class Model extends Object
} }
} }
return $this->db->execute($sql . ';', $input_parameters); $results = $this->db->execute($sql . ';', $input_parameters);
// Clears the cache
if ($update && $this->use_cache)
{
$this->cache->delete($cache_keys);
}
return $results;
} }
// Single row INSERT or UPDATE // Single row INSERT or UPDATE
elseif (count($this->record) > 0) elseif (count($this->record) > 0)

19
jar.php
View file

@ -5185,6 +5185,9 @@ class Model extends Object
// Multiple row query / queries // Multiple row query / queries
if ($this->commit_type == 'queue') if ($this->commit_type == 'queue')
{ {
$update = false;
$cache_keys = array();
/** /**
* @todo I outta loop through twice to determine if it's an INSERT * @todo I outta loop through twice to determine if it's an INSERT
* or an UPDATE. As it stands, you could run into a scenario where * or an UPDATE. As it stands, you could run into a scenario where
@ -5197,6 +5200,8 @@ class Model extends Object
// Performs an UPDATE with multiple queries // Performs an UPDATE with multiple queries
if (array_key_exists($this->columns['id'], $record)) if (array_key_exists($this->columns['id'], $record))
{ {
$update = true;
if (!isset($sql)) if (!isset($sql))
{ {
$sql = ''; $sql = '';
@ -5212,6 +5217,10 @@ class Model extends Object
$update_fields[] = $field . ' = ?'; $update_fields[] = $field . ' = ?';
$input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value); $input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value);
} }
else
{
$cache_keys[] = $this->model . '-' . $value;
}
} }
// @todo Check if the column was passed in // @todo Check if the column was passed in
@ -5304,7 +5313,15 @@ class Model extends Object
} }
} }
return $this->db->execute($sql . ';', $input_parameters); $results = $this->db->execute($sql . ';', $input_parameters);
// Clears the cache
if ($update && $this->use_cache)
{
$this->cache->delete($cache_keys);
}
return $results;
} }
// Single row INSERT or UPDATE // Single row INSERT or UPDATE
elseif (count($this->record) > 0) elseif (count($this->record) > 0)