]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #4978 integrates LRU cache for db query cache
authorcash <cash.costello@gmail.com>
Sat, 13 Apr 2013 19:05:40 +0000 (15:05 -0400)
committercash <cash.costello@gmail.com>
Sat, 13 Apr 2013 19:05:40 +0000 (15:05 -0400)
engine/classes/ElggLRUCache.php
engine/lib/database.php

index 90e63bb617d970a1cabdff99cd19935a01d0efcf..f51af2ed7096bd2d0a496d75a871ac71d7c644eb 100644 (file)
@@ -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);
        }
 }
index b41eb4cda51728701585c0a317857fabf8b8e821..3553d787d93b57de99119900b4dab7fb874def6a 100644 (file)
 /**
  * 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');
        }
 }