From abb0712c97b88c51197c5275ff7559ca3f25f4a0 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 18 Aug 2012 14:36:17 +0200 Subject: [PATCH] Add no-expiry option to cached query functions, and fix cache invalidation bug --- class.databaserecord.php | 21 ++++++++++++++++++--- include.memcache.php | 9 +++++++-- include.mysql.php | 7 +++++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/class.databaserecord.php b/class.databaserecord.php index 373b8b7..31d6575 100644 --- a/class.databaserecord.php +++ b/class.databaserecord.php @@ -371,11 +371,11 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass /* Temporary implementation to make old style queries play nice with PDO code. */ if(strpos($this->verify_query, ":Id") !== false) { - $this->verify_query = str_replace(":Id", "%d", $this->verify_query); + $this->verify_query = str_replace(":Id", "'%d'", $this->verify_query); } $query = sprintf($this->verify_query, $this->sId); - if($result = mysql_query_cached($query)) + if($result = mysql_query_cached($query, 0)) { $insert_mode = CPHP_INSERTMODE_UPDATE; } @@ -542,9 +542,24 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass public function PurgeCache() { - $query = sprintf($this->fill_query, $this->sId); + if(strpos($this->fill_query, ":Id") !== false) + { + $fill_query = str_replace(":Id", "'%d'", $this->fill_query); + } + else + { + $fill_query = $this->fill_query; + } + + $query = sprintf($fill_query, $this->sId); $key = md5($query) . md5($query . "x"); + + $query_hash = md5($this->fill_query); + $parameter_hash = md5(serialize(array(':Id' => (int) $this->sId))); + $pdo_key = $query_hash . $parameter_hash; + mc_delete($key); + mc_delete($pdo_key); } public function RenderTemplate($template = "") diff --git a/include.memcache.php b/include.memcache.php index c4731de..082807b 100644 --- a/include.memcache.php +++ b/include.memcache.php @@ -39,6 +39,7 @@ function mc_get($key) else { $get_result = $cphp_memcache->get($key); + if($get_result !== false) { return $get_result; @@ -84,7 +85,8 @@ function mc_delete($key) } else { - return $cphp_memcache->delete($key); + $delete_result = $cphp_memcache->delete($key); + return $delete_result; } } @@ -142,7 +144,10 @@ function mysql_query_cached($query, $expiry = 60, $key = "") { if(count($data) > 0) { - mc_set($key, $result, $expiry); + if($expiry != 0) + { + mc_set($key, $result, $expiry); + } $return_object->source = "database"; $return_object->data = $result; diff --git a/include.mysql.php b/include.mysql.php index 2bbddac..44ea944 100644 --- a/include.mysql.php +++ b/include.mysql.php @@ -61,7 +61,7 @@ class CachedPDO extends PDO $query_hash = md5($query); $parameter_hash = md5(serialize($parameters)); $cache_hash = $query_hash . $parameter_hash; - + $return_object = new stdClass; if($result = mc_get($cache_hash)) @@ -96,7 +96,10 @@ class CachedPDO extends PDO { if(count($result) > 0) { - mc_set($cache_hash, $result, $expiry); + if($expiry != 0) + { + mc_set($cache_hash, $result, $expiry); + } $return_object->source = "database"; $return_object->data = $result;