From 6abb350a17931f139f3d4571e4c5a5fa4b0b1005 Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 27 Jun 2008 09:25:00 +0000 Subject: Closes #86 git-svn-id: https://code.elgg.org/elgg/trunk@1158 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/metastrings.php | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/engine/lib/metastrings.php b/engine/lib/metastrings.php index 74668ec81..5a91905e4 100644 --- a/engine/lib/metastrings.php +++ b/engine/lib/metastrings.php @@ -11,6 +11,9 @@ * @link http://elgg.org/ */ + /** Cache metastrings for a page */ + $METASTRINGS_CACHE = array(); + /** * Return the meta string id for a given tag, or false. * @@ -19,13 +22,21 @@ */ function get_metastring_id($string) { - global $CONFIG; + global $CONFIG, $METASTRINGS_CACHE; $string = sanitise_string($string); - + $result = array_search($string, $METASTRINGS_CACHE); + if ($result!==false) + return $result; + $row = get_data_row("SELECT * from {$CONFIG->dbprefix}metastrings where string='$string' limit 1"); - if ($row) + if ($row) { + $METASTRINGS_CACHE[$row->id] = $row->string; // Cache it + if ($CONFIG->debug) + error_log("Returning id for string {$row->string} from cache."); + return $row->id; + } return false; } @@ -38,13 +49,21 @@ */ function get_metastring($id) { - global $CONFIG; + global $CONFIG, $METASTRINGS_CACHE; - $id = (int) $id; + $id = (int) $id; + + if (isset($METASTRINGS_CACHE[$id])) + return $METASTRINGS_CACHE[$id]; $row = get_data_row("SELECT * from {$CONFIG->dbprefix}metastrings where id='$id' limit 1"); - if ($row) - return $row->string; + if ($row) { + $METASTRINGS_CACHE[$id] = $row->string; // Cache it + if ($CONFIG->debug) + error_log("Returning string {$row->string} from cache."); + + return $row->string; + } return false; @@ -59,14 +78,18 @@ */ function add_metastring($string) { - global $CONFIG; + global $CONFIG, $METASTRINGS_CACHE; $sanstring = sanitise_string($string); $id = get_metastring_id($string); if ($id) return $id; - return insert_data("INSERT into {$CONFIG->dbprefix}metastrings (string) values ('$sanstring')"); + $result = insert_data("INSERT into {$CONFIG->dbprefix}metastrings (string) values ('$sanstring')"); + if ($result) + $METASTRINGS_CACHE[$result] = $string; + + return $result; } ?> \ No newline at end of file -- cgit v1.2.3