diff options
author | marcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-06-27 16:38:30 +0000 |
---|---|---|
committer | marcus <marcus@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2008-06-27 16:38:30 +0000 |
commit | 76d658aa952dfd9bdecf6cc9ae008f52e4362f06 (patch) | |
tree | ef24e313a003d5124839e01d6a7bb2a112577aef /engine/lib/entities.php | |
parent | 078906f1d511eebf070307a5222cce004bcb4037 (diff) | |
download | elgg-76d658aa952dfd9bdecf6cc9ae008f52e4362f06.tar.gz elgg-76d658aa952dfd9bdecf6cc9ae008f52e4362f06.tar.bz2 |
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
Diffstat (limited to 'engine/lib/entities.php')
-rw-r--r-- | engine/lib/entities.php | 37 |
1 files changed, 29 insertions, 8 deletions
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; } |