Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// based off https://gist.github.com/soderlind/610a9b24dbf95a678c3e#file-a_faster_load_textdomain-php
class TexDomainCache
{
public function __construct($redis_dsn)
{
$this->redis = new Redis();
$this->redis->pconnect($redis_dsn);
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
add_filter('override_load_textdomain', [ & $this, 'cache_hook'], 1, 3);
}
public function cache_hook($retval, $domain, $mofile)
{
global $l10n;
if (!is_readable($mofile)) {
return false;
}
// WooCart defaults prunes all Redis keys prefixed with "cache:"
// on cache invalidation action.
$hash = "cache:mo:" . md5($mofile);
$data = $this->redis->get($hash);
$mo = new MO();
if (!$data) {
if (!$mo->import_from_file($mofile)) {
return false;
}
$data = [
'entries' => $mo->entries,
'headers' => $mo->headers,
];
$this->redis->set($hash, $data);
} else {
$mo->entries = $data['entries'];
$mo->headers = $data['headers'];
}
if (isset($l10n[$domain])) {
$mo->merge_with($l10n[$domain]);
}
$l10n[$domain] = &$mo;
return true;
}
}
new TexDomainCache(REDIS_DSN);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.