Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Cache Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Usage in a laminas-mvc Application

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.

Preparation

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

Configure Cache

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 Controller

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 [];
    }
}

Register Controller

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.

Add Factory Configuration For Controller

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:

Using Multiple Caches

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

Change Used Adapter for Controller

To use a different cache adapter for the controller, change the related module configuration and use one of the previously defined names:

Learn More

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport