Last active
March 18, 2020 13:13
-
-
Save dz0ny/9431d67a8c76bfbda47e8b37335c68b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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