4

I have a multisite wordpress with multiple localized sites (es_ES, de_DE, it_IT). I want improve page speed by caching wordpress translation file.

My code works (inspired by Tristan Darricau)! and my page speed is improved of 0.50 seconds.

Now, my question is: My code is right or can break something (core, theme, plug-in), i have tested and it seem to work, but i'm a newbie of wp!

/**
 * Cache translation
 * @param boolean $override
 * @param string  $domain
 * @param string  $mofile
 * @return boolean
 */
function my_override_load_textdomain($override, $domain, $mofile) {

    global $l10n;

    // check if $mofile exisiste and is readable
    if ( !(is_file($mofile) && is_readable($mofile)) ){
        return false;
    }

    // creates a unique key for cache
    $key   = md5($mofile);

    // I try to retrive data from cache
    $data  = wp_cache_get($key, $domain);

    // Retrieve the last modified date of the translation files
    $mtime = filemtime($mofile);

    $mo = new \MO();

    // if cache not return data or data it's old
    if ( !$data || !isset($data['mtime']) || ($mtime > $data['mtime']) ) {
        // retrive data from MO file
        if ( $mo->import_from_file( $mofile ) ){

            $data = array(
                'mtime'   => $mtime,
                'entries' => $mo->entries,
                'headers' => $mo->headers
            );

            // save data in cache
            wp_cache_set($key, $data, $domain, 0);

        } else {
            return false;
        }

    } else {
        $mo->entries = $data['entries'];
        $mo->headers = $data['headers'];
    }

    if ( isset( $l10n[$domain] ) ) {
        $mo->merge_with( $l10n[$domain] );
    }

    $l10n[$domain] = &$mo;

    return true;
}
add_filter('override_load_textdomain', 'my_override_load_textdomain',1,3);

Side note: i know this is a question for code review stack's platform, but all wordpress'guys are here, and this is a question for wordpress'guys.

I apologize for my bad English

1
  • FYI, there is a Wordpress tag on Code Review
    – Tim Malone
    Commented Jun 25, 2016 at 8:46

2 Answers 2

1

It seems your code is OK and won't break anything but for it to work properly across different pages, maybe you should use Transient API functions instead of Object Cache counterparts.

Why?

Object Cache is not persistent by default (i.e. does not work across different pages). It will persist only if install an persistent cache plugin and a persistent cache backend (e.g. redis, memcached).

The Transient API worksa little bit "smarter": it keeps the cached content on database to be used "persistently" if there's no persistent object cache set, but will use the object cache API if there is an persistent cache backend and plugin.

Use the Transients API instead of these functions if you need to guarantee that your data will be cached. If persistent caching is configured, then the transients functions will use the wp_cache functions described in this document. However if persistent caching has not been enabled, then the data will instead be cached to the options table.

(source: https://codex.wordpress.org/Class_Reference/WP_Object_Cache )

Be aware that database performance may be affected, so, test and measure carefully.

0
1

The only problem that seems to be with that code/idea is that you are caching the whole translation instead of caching the translation nodes (the phrases actually being translated). This leads to higher memory demand from the caching system, but probably not in any significant way (base assumption is that you use some object caching plugin as well, otherwise the whole code is pointless)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.