From 2a3affe9915bcac14b4f6c3c216a790ad183c039 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 27 Mar 2005 20:57:50 +0000 Subject: [PATCH] Now format_string() uses an in-memory cache per request to make things faster. Really noticeable in logs page! --- lib/weblib.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/weblib.php b/lib/weblib.php index 2442b298c2d..aba81cbdfee 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1065,6 +1065,17 @@ function format_string ($string, $striplinks = false, $courseid=NULL ) { global $CFG, $course; + //We'll use a in-memory cache here to speed up repeated strings + static $strcache; + + //Calculate md5 + $md5 = md5($string.$striplinks); + + //Fetch from cache if possible + if(isset($strcache[$md5])) { + return $strcache[$md5]; + } + if (empty($courseid)) { if (!empty($course->id)) { // An ugly hack for better compatibility $courseid = $course->id; // (copied from format_text) @@ -1079,6 +1090,9 @@ function format_string ($string, $striplinks = false, $courseid=NULL ) { $string = preg_replace('/(]+?>)(.+?)(<\/a>)/is','$2',$string); } + //Store to cache + $strcache[$md5] = $string; + return $string; }