aboutsummaryrefslogtreecommitdiff
path: root/engine/lib
diff options
context:
space:
mode:
authormarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-03 17:24:16 +0000
committermarcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-11-03 17:24:16 +0000
commit15aa8a612a6d4660c2aa71f9ea0d66593e277125 (patch)
tree857cab297b7628cc0a6b35202192c7938758ae7e /engine/lib
parent9539766a79716687877b2b383dd99e36482221bf (diff)
downloadelgg-15aa8a612a6d4660c2aa71f9ea0d66593e277125.tar.gz
elgg-15aa8a612a6d4660c2aa71f9ea0d66593e277125.tar.bz2
Experimental database query caching, please report problems asap.
git-svn-id: https://code.elgg.org/elgg/trunk@2381 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib')
-rw-r--r--engine/lib/database.php79
1 files 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();