The caching that the GeSHi Filter currently does is awesome. It reads in the language names, and then holds onto them throughout GeSHi execution. When we look at a function that does this caching, we see:
/**
* List of enabled languages.
*/
function _geshifilter_get_enabled_languages() {
static $enabled_languages = NULL;
if ($enabled_languages == NULL) {
$enabled_languages = array();
$languages = _geshifilter_get_available_languages();
foreach ($languages as $key => $language) {
if (variable_get("geshifilter_language_enabled_{$language}", TRUE)) {
$enabled_languages[$key] = $language;
}
}
}
return $enabled_languages;
}
As you can see, we use a static variable to hold the data. It would make the caching even awesomer (yes, awesomer is a word) if we used cache_get and cache_set in addition to this for some added database mastery. The end result would look something like this:
/**
* List of enabled languages.
*/
function _geshifilter_get_enabled_languages() {
static $enabled_languages = NULL;
if ($enabled_languages == NULL) {
$enabled_languages = cache_get('geshi_enabled_languages', 'cache_filter');
if ($enabled_languages == NULL) {
$enabled_languages = array();
$languages = _geshifilter_get_available_languages();
foreach ($languages as $key => $language) {
if (variable_get("geshifilter_language_enabled_{$language}", TRUE)) {
$enabled_languages[$key] = $language;
}
}
cache_set('geshi_enabled_languages', 'cache_filter', $enabled_languages);
}
return $enabled_languages;
}
?>
As you see in this pseudo code, not only do we use the static variable, we also use the database caching table to hold onto the enabled languages. This makes the method extremely fast, even when the static variable is lost. When the static variable is lost, and the database cache isn't there, it creates the cached item in the database, making it ready for action.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | cache_enabled_languages.patch | 2.05 KB | soxofaan |
Comments
Comment #1
robloach... Forgot the PHP tag around the code.....
Comment #2
soxofaan commentedI'm not sure if this level of caching is really needed and would make a difference,
but the attached patch should do it.
Do you have a setup where you can "measure" the difference in performance in some way?
Comment #3
robloachYou can also do this to the other functions that implement the static caching as well. Performance-wise? Not quite sure how much it helps. I'll see if I can put up a patch.
Comment #4
soxofaan commentedI'll mark this as won't fix because I don't believe the patch will make a difference that's worth the extra code/complexity and maintenance .
Note that _geshifilter_get_enabled_languages() already uses caching (with a static variable) and on a cache miss it calls _geshifilter_get_available_languages(), which also uses DB-caching through variable_get().
Comment #5
wim leersSubscribing.
Comment #6
soxofaan commentedat #5: do you believe this patch (#2) will make a significant difference?
PS: groetjes uit Gent
Comment #7
wim leersEh... we posted almost simultaneously, hence the form was still set to the old status for me. A bit unfortunate :)
I have no idea how much time GeSHi takes to do its work, but it might make a difference on a site with lots of code, like Rob Loach's. However, now that I think of it, it makes more sense to cache the entire rendered node, by using the patches of the Advanced Cache module then. So I guess I agree.