From 15aa8a612a6d4660c2aa71f9ea0d66593e277125 Mon Sep 17 00:00:00 2001 From: marcus Date: Mon, 3 Nov 2008 17:24:16 +0000 Subject: Experimental database query caching, please report problems asap. git-svn-id: https://code.elgg.org/elgg/trunk@2381 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/database.php | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/engine/lib/database.php b/engine/lib/database.php index e1f007399..640b1aa5b 100644 --- a/engine/lib/database.php +++ b/engine/lib/database.php @@ -13,6 +13,7 @@ */ $DB_PROFILE = array(); + //$DB_QUERY_CACHE = array(); /** * Connect to the database server and use the Elgg database for a particular database link @@ -151,20 +152,23 @@ function get_data($query, $callback = "") { - global $CONFIG, $dbcalls, $DB_PROFILE; + global $CONFIG, $dbcalls, $DB_PROFILE, $DB_QUERY_CACHE; $dblink = get_db_link('read'); $resultarray = array(); + + if (!$DB_QUERY_CACHE) $DB_QUERY_CACHE = array(); + if (isset($DB_QUERY_CACHE[$query])) { + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log ("$query results returned from cache"); + //return $DB_QUERY_CACHE[$query]; + } + $dbcalls++; if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) - { $DB_PROFILE[] = $query; - - //error_log("--- DB QUERY --- $query"); - //error_log("--- EXPLANATION --- " . print_r(explain_query($query,$dblink), true)); - } if ($result = mysql_query("$query", $dblink)) { while ($row = mysql_fetch_object($result)) { @@ -183,7 +187,13 @@ error_log("WARNING: DB query \"$query\" returned no results."); return false; - } + } + + // Cache result + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log("$query results cached"); + $DB_QUERY_CACHE[$query] = $resultarray; + return $resultarray; } @@ -195,24 +205,32 @@ function get_data_row($query) { - global $CONFIG, $dbcalls, $DB_PROFILE; + global $CONFIG, $dbcalls, $DB_PROFILE, $DB_QUERY_CACHE; $dblink = get_db_link('read'); - + + if (!$DB_QUERY_CACHE) $DB_QUERY_CACHE = array(); + if (isset($DB_QUERY_CACHE[$query])) { + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log ("$query results returned from cache"); + //return $DB_QUERY_CACHE[$query]; + } + $dbcalls++; if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) - { $DB_PROFILE[] = $query; - - //error_log("--- DB QUERY --- $query"); - //error_log("--- EXPLANATION --- " . print_r(explain_query($query,$dblink), true)); - } - if ($result = mysql_query("$query", $dblink)) { - while ($row = mysql_fetch_object($result)) { - return $row; - } + if ($result = mysql_query("$query", $dblink)) { + + $row = mysql_fetch_object($result); + + // Cache result (even if query returned no data + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log("$query results cached"); + $DB_QUERY_CACHE[$query] = $row; + + if ($row) return $row; } if (mysql_errno($dblink)) @@ -233,7 +251,7 @@ function insert_data($query) { - global $CONFIG, $dbcalls, $DB_PROFILE; + global $CONFIG, $dbcalls, $DB_PROFILE, $DB_QUERY_CACHE; $dblink = get_db_link('write'); @@ -244,7 +262,12 @@ $DB_PROFILE[] = $query; //error_log("--- DB QUERY --- $query"); - } + } + + // Invalidate query cache + $DB_QUERY_CACHE = array(); + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log("Query cache invalidated"); if (mysql_query("$query", $dblink)) return mysql_insert_id($dblink); @@ -264,7 +287,7 @@ function update_data($query) { - global $dbcalls, $CONFIG, $DB_PROFILE; + global $dbcalls, $CONFIG, $DB_PROFILE, $DB_QUERY_CACHE; $dblink = get_db_link('write'); @@ -275,7 +298,12 @@ $DB_PROFILE[] = $query; //error_log("--- DB QUERY --- $query"); - } + } + + // Invalidate query cache + $DB_QUERY_CACHE = array(); + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log("Query cache invalidated"); if (mysql_query("$query", $dblink)) return true; //return mysql_affected_rows(); @@ -296,7 +324,7 @@ function delete_data($query) { - global $dbcalls, $CONFIG, $DB_PROFILE; + global $dbcalls, $CONFIG, $DB_PROFILE, $DB_QUERY_CACHE; $dblink = get_db_link('write'); @@ -308,6 +336,11 @@ //error_log("--- DB QUERY --- $query"); } + + // Invalidate query cache + $DB_QUERY_CACHE = array(); + if ((isset($CONFIG->debug)) && ($CONFIG->debug==true)) + error_log("Query cache invalidated"); if (mysql_query("$query", $dblink)) return mysql_affected_rows(); -- cgit v1.2.3