Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Last active March 18, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dz0ny/9431d67a8c76bfbda47e8b37335c68b2 to your computer and use it in GitHub Desktop.
Save dz0ny/9431d67a8c76bfbda47e8b37335c68b2 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WooCart Machine Objects Cache
Description: Cache for localization files.
Version: 1.5.0
*/
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("127.0.0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment