aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcash <cash.costello@gmail.com>2013-04-13 15:05:40 -0400
committercash <cash.costello@gmail.com>2013-04-13 15:05:40 -0400
commit21c92cf39d719a913cd9e29474c04781e9cd72ef (patch)
tree5c08d5742defdd8f3a1577453c9f824930f7e92e
parentfc9a1e985b0b20c74a913f22ec9ea7e0c16848a7 (diff)
downloadelgg-21c92cf39d719a913cd9e29474c04781e9cd72ef.tar.gz
elgg-21c92cf39d719a913cd9e29474c04781e9cd72ef.tar.bz2
Fixes #4978 integrates LRU cache for db query cache
-rw-r--r--engine/classes/ElggLRUCache.php8
-rw-r--r--engine/lib/database.php29
2 files changed, 19 insertions, 18 deletions
diff --git a/engine/classes/ElggLRUCache.php b/engine/classes/ElggLRUCache.php
index 90e63bb61..f51af2ed7 100644
--- a/engine/classes/ElggLRUCache.php
+++ b/engine/classes/ElggLRUCache.php
@@ -139,7 +139,7 @@ class ElggLRUCache implements ArrayAccess {
* @param mixed $value The value to set.
* @return void
*/
- function offsetSet($key, $value) {
+ public function offsetSet($key, $value) {
$this->set($key, $value);
}
@@ -151,7 +151,7 @@ class ElggLRUCache implements ArrayAccess {
* @param int|string $key The key to retrieve.
* @return mixed
*/
- function offsetGet($key) {
+ public function offsetGet($key) {
return $this->get($key);
}
@@ -163,7 +163,7 @@ class ElggLRUCache implements ArrayAccess {
* @param int|string $key The key to unset.
* @return void
*/
- function offsetUnset($key) {
+ public function offsetUnset($key) {
$this->remove($key);
}
@@ -175,7 +175,7 @@ class ElggLRUCache implements ArrayAccess {
* @param int|string $key A key to check for.
* @return boolean
*/
- function offsetExists($key) {
+ public function offsetExists($key) {
return $this->containsKey($key);
}
}
diff --git a/engine/lib/database.php b/engine/lib/database.php
index b41eb4cda..3553d787d 100644
--- a/engine/lib/database.php
+++ b/engine/lib/database.php
@@ -12,18 +12,19 @@
/**
* Query cache for all queries.
*
- * Each query and its results are stored in this array as:
+ * Each query and its results are stored in this cache as:
* <code>
- * $DB_QUERY_CACHE[$query] => array(result1, result2, ... resultN)
+ * $DB_QUERY_CACHE[query hash] => array(result1, result2, ... resultN)
* </code>
+ * @see elgg_query_runner() for details on the hash.
*
- * @warning be array this var may be an array or ElggStaticVariableCache depending on when called :(
+ * @warning Elgg used to set this as an empty array to turn off the cache
*
- * @global ElggStaticVariableCache|array $DB_QUERY_CACHE
+ * @global ElggLRUCache|null $DB_QUERY_CACHE
* @access private
*/
global $DB_QUERY_CACHE;
-$DB_QUERY_CACHE = array();
+$DB_QUERY_CACHE = null;
/**
* Queries to be executed upon shutdown.
@@ -127,9 +128,8 @@ function establish_db_link($dblinkname = "readwrite") {
// Set up cache if global not initialized and query cache not turned off
if ((!$DB_QUERY_CACHE) && (!$db_cache_off)) {
- // @todo everywhere else this is assigned to array(), making it dangerous to call
- // object methods on this. We should consider making this an plain array
- $DB_QUERY_CACHE = new ElggStaticVariableCache('db_query_cache');
+ // @todo if we keep this cache in 1.9, expose the size as a config parameter
+ $DB_QUERY_CACHE = new ElggLRUCache(200);
}
}
@@ -404,11 +404,9 @@ function elgg_query_runner($query, $callback = null, $single = false) {
// Is cached?
if ($DB_QUERY_CACHE) {
- $cached_query = $DB_QUERY_CACHE[$hash];
-
- if ($cached_query !== FALSE) {
+ if (isset($DB_QUERY_CACHE[$hash])) {
elgg_log("DB query $query results returned from cache (hash: $hash)", 'NOTICE');
- return $cached_query;
+ return $DB_QUERY_CACHE[$hash];
}
}
@@ -531,10 +529,13 @@ function delete_data($query) {
*/
function _elgg_invalidate_query_cache() {
global $DB_QUERY_CACHE;
- if ($DB_QUERY_CACHE) {
- /* @var ElggStaticVariableCache $DB_QUERY_CACHE */
+ if ($DB_QUERY_CACHE instanceof ElggLRUCache) {
$DB_QUERY_CACHE->clear();
elgg_log("Query cache invalidated", 'NOTICE');
+ } elseif ($DB_QUERY_CACHE) {
+ // In case someone sets the cache to an array and primes it with data
+ $DB_QUERY_CACHE = array();
+ elgg_log("Query cache invalidated", 'NOTICE');
}
}