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