From 3290ee0135b53b9d82c6329c74880a366bbb0a41 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 2 Dec 2012 13:08:36 -0500 Subject: [PATCH] Added cache purging post-execution for multiple row UPDATEs --- classes/Model.php | 19 ++++++++++++++++++- jar.php | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/classes/Model.php b/classes/Model.php index af199b4..822bc54 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -1085,6 +1085,9 @@ class Model extends Object // Multiple row query / queries if ($this->commit_type == 'queue') { + $update = false; + $cache_keys = array(); + /** * @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 @@ -1097,6 +1100,8 @@ class Model extends Object // Performs an UPDATE with multiple queries if (array_key_exists($this->columns['id'], $record)) { + $update = true; + if (!isset($sql)) { $sql = ''; @@ -1112,6 +1117,10 @@ class Model extends Object $update_fields[] = $field . ' = ?'; $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 @@ -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 elseif (count($this->record) > 0) diff --git a/jar.php b/jar.php index 7dabfda..74abf96 100755 --- a/jar.php +++ b/jar.php @@ -5185,6 +5185,9 @@ class Model extends Object // Multiple row query / queries if ($this->commit_type == 'queue') { + $update = false; + $cache_keys = array(); + /** * @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 @@ -5197,6 +5200,8 @@ class Model extends Object // Performs an UPDATE with multiple queries if (array_key_exists($this->columns['id'], $record)) { + $update = true; + if (!isset($sql)) { $sql = ''; @@ -5212,6 +5217,10 @@ class Model extends Object $update_fields[] = $field . ' = ?'; $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 @@ -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 elseif (count($this->record) > 0)