zend-framework2memcachedzend-translatezend-cache

Add prefix to cached ZF2 translation files


I am using ZF2 I18n translator and memcached. Setup is:

use Zend\I18n\Translator\Translator;
use Zend\Cache\Storage\Adapter\MemcachedResourceManager as MemcachedResourceManager;

//=== setup the cache ===//
//create a memcached resource manager
$memcached_resource_manager = new MemcachedResourceManager();
$memcached_resource_manager->addServer( RESOURCE_ID, array( 'localhost', '11211' ));

//create memcached options
$memcached_options = new \Zend\Cache\Storage\Adapter\MemcachedOptions(array(
        'resource_manager' => $memcached_resource_manager,
        'resource_id'      => RESOURCE_ID,
        'namespace'        => CACHE_NAMESPACE,
        'ttl'              => 3600,
        )
    );

//create memcached adapter
$memcached_adapter = new \Zend\Cache\Storage\Adapter\Memcached( $memcached_options );
//== end cache setup ===//

//create translator
$translator = new Translator();
$translator->setCache( $memcached_adapter );

(All the capitalized words are constants.)

Then I add files with a custom class CsvLoader that extends AbstractFileLoader. That part works great.

$translator->addTranslationFile('CsvLoader', $translation_file_name, 'section');

My issue is when I want to clear the cache. I can currently clear all locales/files at once by namespace or nothing. How can I add a prefix/suffix to each added translation file in the cache?

I currently see values like CACHE_NAMESPACE:Zend_I18n_Translator_Messages_7a1565097c5fca5a3138b2330c2451db

I would like to see entries like CACHE_NAMESPACE:Zend_I18n_Translator_Messages_es_MX_7a1565097c5fca5a3138b2330c2451db

Which show a locale. Thanks.

Additional Information To clear entries I am currently retrieving the entries with:

$entries = $translator->getCache()->getOptions()->getResourceManager()->getResource(self::RESOURCE_ID)->getAllKeys()

(which seems like a ridiculously long function chain to me - suggestions for a better way are welcome) and then looping through to check the item names. If an item matches a criteria, I call removeItem

Update

Here is a link to the relevant source code of the hash generation that Bram mentions https://github.com/zendframework/zf2/blob/master/library/Zend/I18n/Translator/Translator.php#L553


Solution

  • There is no option to configure the suffix you want to use in the cache key. You have 2 options imo.

    1) Extend the Translator class and override the loadMessages() method, customize the code to your needs. You will have to duplicate all the code in this method which is not really feasible and will cause maintenance issues when ZF dev's change the code or implement new features.

    2) The cache key is build up as follows:

    $cacheId = 'Zend_I18n_Translator_Messages_' . md5($textDomain . $locale);
    

    For invalidating the cache for locale es and the default textdomain you can just reconstruct the cache key yourself using the same code and remove the given key using the cache adapter.

    $memcache_adapter->removeItem('Zend_I18n_Translator_Messages_' . md5('defaultes');