On this page
Cookbook
Using laminas-cache for Caching
The following cookbook covers using laminas-cache for caching.
Installation Requirements
The following example uses laminas-cache, so make sure to have it installed before getting started:
$ composer require laminas/laminas-cache
laminas-cache is shipped without a specific cache adapter to allow free choice of storage backends and their dependencies. So make sure that the required adapters are installed.
The following example uses the memory adapter of laminas-cache:
$ composer require laminas/laminas-cache-storage-adapter-memory
By using the laminas-component-installer the config-providers for laminas-cache and the memory adapter are automatically injected into the dependency-injection container.
Define the Cache Service
The standard cache must be decorated with the CacheItemPoolDecorator to provide the Psr\Cache\CacheItemPoolInterface interface (PSR-6) or with the SimpleCacheDecorator to provide the Psr\SimpleCache\CacheInterface interface (PSR-16).
$cachePluginManager = $container->get(
Laminas\Cache\Storage\AdapterPluginManager::class
);
$container->setService(
'MyApp\CacheService',
new Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator(
$cachePluginManager->get(
Laminas\Cache\Storage\Adapter\Memory::class
)
)
);
Preparing to Access the Cache Features
The caching is decoupled from the translator itself.
The translation collector is responsible for caching translations.
To fetch the general translation collector, use the TranslationCollectorInterface service:
/** @var Laminas\I18n\Translator\TranslationCollector\PSR6CachingCollector|Laminas\I18n\Translator\TranslationCollector\PSR16CachingCollector $collector */
$collector = $container->get(
Laminas\I18n\Translator\TranslationCollector\TranslationCollectorInterface::class
);
If the cache is configured, this will return a decorated translation collector instance.
Clear Cache
To clear the cache for a specific text domain and locale, use the clearCache method.
$collector->clearCache('default', 'en_US');
Get Cache Identifier
To get the cache identifier for a specific text domain and locale, use the cacheKey method:
$collector->cacheKey('default', 'en_US');
Stand-Alone Usage
Configure the Dependency Injection Container
Configure the dependency-injection container with the help of the config provider and add the translation file and set the cache name via configuration:$container = (new Laminas\ServiceManager\ServiceManager(
(new Laminas\I18n\ConfigProvider())->getDependencyConfig()
));
$container->setService(
'config',
[
'laminas-i18n' => [
'translator' => [
'psr6_cache' => 'MyApp\CacheService',
// or 'psr16_cache' => 'MyApp\CacheService',
'translation_files' => [
[
'type' => Laminas\I18n\Translator\Loader\PhpArray::class,
'filename' => __DIR__ . '/languages/de_DE.php',
'locale' => 'de_DE',
],
],
],
],
]
);
Configure the Cache Adapter
Extend the dependency-injection container with the help of the config providers to add laminas-cache and cache adapter:$container->configure(
(new Laminas\Cache\ConfigProvider())->getDependencyConfig()
);
$container->configure(
(new Laminas\Cache\Storage\Adapter\Memory\ConfigProvider(
))->getServiceDependencies()
);
The next steps are the same as before.