laminas/laminas-cache
Laminas Cache provides flexible caching for PHP applications, with adapters for common backends, cache storage, patterns, plugins, and PSR-compatible integrations. Includes tools for configuring, managing, and testing cache behavior in Laminas apps.
The following example shows one potential use case of laminas-cache within a laminas-mvc based application. The example uses a module, a controller and shows the resolving of dependencies of the controller by configuration.
Before starting, make sure laminas-cache is installed and configured.
MISSING: Installation Requirements 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 used the filesystem adapter of laminas-cache:
$ composer require laminas/laminas-cache-storage-adapter-filesystem
To configure the cache in a laminas-mvc based application, use either application or module configuration (such as config/autoload/*.global.php or module/Application/config/module.config.php, respectively), and define the configuration key caches.
In this example, the global configuration is used and a separate file is created for the cache configuration.
Create a configuration file with name like config/autoload/cache.global.php and it will automatically be included:
return [
'caches' => [
'default-cache' => [
'adapter' => Laminas\Cache\Storage\Adapter\Filesystem::class,
'options' => [
'cache_dir' => __DIR__ . '/../../data/cache',
],
],
],
];
The factory Laminas\Cache\Service\StorageCacheAbstractServiceFactory uses the configuration, searches for the configuration key caches and creates the storage adapters using the discovered configuration.
Create a controller class and inject the cache with the interface for all cache storage adapters via the constructor, e.g. module/Application/Controller/IndexController.php:
namespace Application\Controller;
use Laminas\Cache\Storage\StorageInterface;
use Laminas\Mvc\Controller\AbstractActionController;
final class IndexController extends AbstractActionController
{
public function __construct(
private readonly StorageInterface $cache
) {}
public function indexAction(): array
{
if (! $this->cache->hasItem('example')) {
$this->cache->addItem('example', 'value');
}
echo $this->cache->getItem('example') // value;
// …
return [];
}
}
To register the controller for the application, extend the configuration of the module.
Add the following lines to the module configuration file, e.g. module/Application/config/module.config.php:
The example uses the config factory from laminas-servicemanager which allows any string to be used to fetch a service from the application service container, like the name of the configured cache: default-cache.
This means that the factory searches for an appropriate configuration to create the controller and to resolve the constructor dependencies for the controller class.
Extend the module configuration file to add the configuration for the controller.
Use the name of the cache (default-cache), which was previously defined in the configuration of the caches, to retrieve the related cache storage instance:
The use more than one cache backend, the factory Laminas\Cache\Service\StorageCacheAbstractServiceFactory allows to define multiple cache storages.
Extend the cache configuration in config/autoload/cache.global.php and add more cache adapters:
MISSING: Installation Requirements Make sure that the used storage adapters are installed:
$ composer require laminas/laminas-cache-storage-adapter-memory laminas/laminas-cache-storage-adapter-blackhole
To use a different cache adapter for the controller, change the related module configuration and use one of the previously defined names:
How can I help you explore Laravel packages today?