]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Added caching of all subtypes with single query
authorPaweł Sroka <srokap@gmail.com>
Sun, 4 Nov 2012 10:25:42 +0000 (11:25 +0100)
committerSteve Clay <steve@mrclay.org>
Wed, 28 Nov 2012 04:22:09 +0000 (23:22 -0500)
engine/lib/entities.php

index a14160e14c18bb73371d448867829be28d0111e0..3642856bf2c172076da6e16659fa5c350f9c2543 100644 (file)
@@ -17,7 +17,7 @@ global $ENTITY_CACHE;
 $ENTITY_CACHE = array();
 
 /**
- * Cache subtypes and related class names once loaded.
+ * Cache subtypes and related class names.
  *
  * @global array $SUBTYPE_CACHE
  * @access private
@@ -156,18 +156,15 @@ function get_subtype_id($type, $subtype) {
        if ($subtype == "") {
                return FALSE;
        }
+       
+       if (!$SUBTYPE_CACHE) {\r
+               populate_subtype_cache();\r
+       }
 
-       // @todo use the cache before hitting database
-       $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
-               where type='$type' and subtype='$subtype'");
-
-       if ($result) {
-               if (!$SUBTYPE_CACHE) {
-                       $SUBTYPE_CACHE = array();
-               }
-
-               $SUBTYPE_CACHE[$result->id] = $result;
-               return $result->id;
+       // use the cache before hitting database\r
+       $result = retrieve_cached_subtype($type, $subtype);\r
+       if ($result!==null) {\r
+               return $result->id;\r
        }
 
        return FALSE;
@@ -192,21 +189,47 @@ function get_subtype_from_id($subtype_id) {
                return false;
        }
 
+       if (!$SUBTYPE_CACHE) {\r
+               populate_subtype_cache();\r
+       }\r
+       
        if (isset($SUBTYPE_CACHE[$subtype_id])) {
                return $SUBTYPE_CACHE[$subtype_id]->subtype;
        }
 
-       $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
-       if ($result) {
-               if (!$SUBTYPE_CACHE) {
-                       $SUBTYPE_CACHE = array();
-               }
+       return false;
+}
 
-               $SUBTYPE_CACHE[$subtype_id] = $result;
-               return $result->subtype;
+/**\r
+ * Retrieve subtype from the cache.\r
+ *
+ * @return stdClass|null\r
+ * @access private\r
+ */\r
+function retrieve_cached_subtype($type, $subtype) {\r
+       global $SUBTYPE_CACHE;\r
+\r
+       foreach ($SUBTYPE_CACHE as $id => $obj) {
+               if ($obj->type==$type && $obj->subtype==$subtype) {
+                       return $obj;
+               }
        }
+       return null;\r
+}
 
-       return false;
+/**
+ * Fetch all suptypes from DB to local cache.
+ * @access private
+ */
+function populate_subtype_cache() {
+       global $CONFIG, $SUBTYPE_CACHE;
+       
+       $results = get_data("SELECT * from {$CONFIG->dbprefix}entity_subtypes");
+       
+       $SUBTYPE_CACHE = array();
+       foreach ($results as $result) {
+               $SUBTYPE_CACHE[$result->id] = $result;
+       }
 }
 
 /**
@@ -230,16 +253,13 @@ function get_subtype_class($type, $subtype) {
        $type = sanitise_string($type);
        $subtype = sanitise_string($subtype);
 
-       // @todo use the cache before going to the database
-       $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
-               where type='$type' and subtype='$subtype'");
-
-       if ($result) {
-               if (!$SUBTYPE_CACHE) {
-                       $SUBTYPE_CACHE = array();
-               }
-
-               $SUBTYPE_CACHE[$result->id] = $result;
+       if (!$SUBTYPE_CACHE) {\r
+               populate_subtype_cache();\r
+       }\r
+       
+       // use the cache before going to the database
+       $result = retrieve_cached_subtype($type, $subtype);
+       if ($result!==null) {
                return $result->class;
        }
 
@@ -265,20 +285,14 @@ function get_subtype_class_from_id($subtype_id) {
                return false;
        }
 
+       if (!$SUBTYPE_CACHE) {\r
+               populate_subtype_cache();\r
+       }\r
+       
        if (isset($SUBTYPE_CACHE[$subtype_id])) {
                return $SUBTYPE_CACHE[$subtype_id]->class;
        }
 
-       $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
-
-       if ($result) {
-               if (!$SUBTYPE_CACHE) {
-                       $SUBTYPE_CACHE = array();
-               }
-               $SUBTYPE_CACHE[$subtype_id] = $result;
-               return $result->class;
-       }
-
        return NULL;
 }
 
@@ -305,7 +319,7 @@ function get_subtype_class_from_id($subtype_id) {
  * @see get_entity()
  */
 function add_subtype($type, $subtype, $class = "") {
-       global $CONFIG;
+       global $CONFIG, $SUBTYPE_CACHE;
        $type = sanitise_string($type);
        $subtype = sanitise_string($subtype);
        $class = sanitise_string($class);
@@ -318,8 +332,18 @@ function add_subtype($type, $subtype, $class = "") {
        $id = get_subtype_id($type, $subtype);
 
        if ($id == 0) {
-               return insert_data("insert into {$CONFIG->dbprefix}entity_subtypes"
+               $result = insert_data("insert into {$CONFIG->dbprefix}entity_subtypes"
                        . " (type, subtype, class) values ('$type','$subtype','$class')");
+               
+               // add entry to cache
+               $obj = new stdClass();
+               $obj->id = $result;
+               $obj->type = $type;
+               $obj->subtype = $subtype;
+               $obj->class = $class;
+               $SUBTYPE_CACHE[$obj->id] = $obj;
+               \r
+               return $result;
        }
 
        return $id;
@@ -367,6 +391,10 @@ function update_subtype($type, $subtype, $class = '') {
        $type = sanitise_string($type);
        $subtype = sanitise_string($subtype);
 
+       if (!$SUBTYPE_CACHE) {\r
+               populate_subtype_cache();\r
+       }\r
+       
        $result = update_data("UPDATE {$CONFIG->dbprefix}entity_subtypes
                SET type = '$type', subtype = '$subtype', class = '$class'
                WHERE id = $id