/zend-expressive-cache

Cache module for PHP Expressive applications

Primary LanguagePHPBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Expressive Cache module

Build Status Coverage Status

Zend-expressive-cache is an Expressive module for caching any HTTP PSR-7 response using a PSR-16 cache system.

Installation

You can install the zend-expressive-cache module with composer:

$ composer require zendframework/zend-expressive-cache

Documentation

Documentation is in the doc tree, and can be compiled using mkdocs:

$ mkdocs build

You may also browse the documentation online.

Configuration

After the installation you need to set the following configuration in your Expressive application:

return [
    'cache' => [
        'service-name' => <cache-service-name>,
        'ttl' => 3600, // in seconds
    ]
];

where <cache-service-name> is the name of a PSR-16 cache service and ttl is the Time to live value, reported in seconds. The cache service should be provided using using a PSR-11 container of your Expressive application.

Usage

When configured, you can add the CacheMiddleware to the routes that you want to cache. For instance, if you have a /home route, you can add the caching as follows:

use Zend\Expressive\Cache\CacheMiddleware;
use App\Action\HomeAction;

$app->get('/home', [CacheMiddleware::class, HomeAction::class], 'home');

The CacheMiddleware is typically the first one in the pipe of the route. The first time, the cache will store the HTTP response provided by HomeAction::class. Starting from the second request, the cache will return the value stored until the ttl timeout.