From 76d658aa952dfd9bdecf6cc9ae008f52e4362f06 Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 27 Jun 2008 16:38:30 +0000 Subject: Caching (most) subtype queries, drastically reduces database queries in most use cases, refs #101 git-svn-id: https://code.elgg.org/elgg/trunk@1193 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'engine/lib/entities.php') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index d0c637a3f..284140ca1 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -14,6 +14,9 @@ /// Cache objects in order to minimise database access. $ENTITY_CACHE = array(); + /// Cache subtype searches + $SUBTYPE_CACHE = array(); + /** * ElggEntity The elgg entity superclass * This class holds methods for accessing the main entities table. @@ -763,17 +766,21 @@ */ function get_subtype_id($type, $subtype) { - global $CONFIG; + global $CONFIG, $SUBTYPE_CACHE; $type = sanitise_string($type); $subtype = sanitise_string($subtype); if ($subtype=="") return $subtype; + // Todo: cache here? Or is looping less efficient that going to the db each time? + $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where type='$type' and subtype='$subtype'"); - if ($result) + if ($result) { + $SUBTYPE_CACHE[$result->id] = $result; return $result->id; + } return 0; } @@ -787,15 +794,20 @@ */ function get_subtype_from_id($subtype_id) { - global $CONFIG; + global $CONFIG, $SUBTYPE_CACHE; $subtype_id = (int)$subtype_id; if (!$subtype_id) return false; + 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 ($result) { + $SUBTYPE_CACHE[$subtype_id] = $result; return $result->subtype; + } return false; } @@ -809,14 +821,18 @@ */ function get_subtype_class($type, $subtype) { - global $CONFIG; + global $CONFIG, $SUBTYPE_CACHE; $type = sanitise_string($type); $subtype = sanitise_string($subtype); + // Todo: cache here? Or is looping less efficient that going to the db each time? + $result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where type='$type' and subtype='$subtype'"); - if ($result) + if ($result) { + $SUBTYPE_CACHE[$result->id] = $result; return $result->class; + } return NULL; } @@ -829,15 +845,20 @@ */ function get_subtype_class_from_id($subtype_id) { - global $CONFIG; + global $CONFIG, $SUBTYPE_CACHE; $subtype_id = (int)$subtype_id; if (!$subtype_id) return false; + 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 ($result) { + $SUBTYPE_CACHE[$subtype_id] = $result; return $result->class; + } return NULL; } -- cgit v1.2.3