PSR-15 middleware with various cache utilities

Overview

middlewares/cache

Latest Version on Packagist Software License Testing Total Downloads

Middleware components with the following cache utilities:

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/cache.

composer require middlewares/cache

CachePrevention

To add the response headers for cache prevention. Useful in development environments:

Dispatcher::run([
    new Middlewares\CachePrevention()
]);

Expires

This middleware adds the Expires and Cache-Control: max-age headers to the response. You can configure the cache duration for each mimetype. If it's not defined, use the defaults.

// Use the default configuration
$expires = new Middlewares\Expires();

// Custom durations
$expires = new Middlewares\Expires([
    'text/css' => '+1 year',
    'text/js' => '+1 week',
]);

defaultExpires

Set the default expires value if the request mimetype is not configured. By default is 1 month. Example:

//set 1 year lifetime to css and js
$durations = [
    'text/css' => '+1 year',
    'text/javascript' => '+1 year',
];

//and 1 hour to everything else
$default = '+1 hour';

$expires = (new Middlewares\Expires($durations))->defaultExpires($default);

Cache

Saves the response headers in a PSR-6 cache pool and returns 304 responses (Not modified) if the response is still valid. This saves server resources and bandwidth because the body is returned empty. It's recomended to combine it with Expires to set the lifetime of the responses.

$cachePool = new Psr6CachePool();

Dispatcher::run([
    new Middlewares\Cache($cachePool),
    new Middlewares\Expires()
]);

Optionally, you can provide a Psr\Http\Message\ResponseFactoryInterface as the second argument to create the 304 empty responses. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.

$cachePool = new Psr6CachePool();
$responseFactory = new MyOwnResponseFactory();

$cache = new Middlewares\Cache($cachePool, $responseFactory);

ClearSiteData

Send the header Clear-Site-Data to remove all site data in the client (cache, cookies, storage etc)

Dispatcher::run([
    new Middlewares\ClearSiteData()
]);

Optionally, you can provide the list of data types that you want to clear.

$cache = new Middlewares\ClearSiteData('cache', 'cookies');

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.

You might also like...
LRU Cache implementation in PHP

PHP LRU Cache implementation Intro WTF is a LRU Cache? LRU stands for Least Recently Used. It's a type of cache that usually has a fixed capacity and

More Than Just a Cache: Redis Data Structures
More Than Just a Cache: Redis Data Structures

More Than Just a Cache: Redis Data Structures Redis is a popular key-value store, commonly used as a cache or message broker service. However, it can

Simple cache

Simple cache

Elephant - a highly performant PHP Cache Driver for Kirby 3

🐘 Kirby3 PHP Cache-Driver Elephant - a highly performant PHP Cache Driver for Kirby 3 Commerical Usage Support open source! This plugin is free but i

An improved helper for working with cache

Laravel Cache Installation To get the latest version of Laravel Cache, simply require the project using Composer: $ composer require dragon-code/larav

Zend Framework cache backend for MongoDB

Zend_Cache_Backend_Mongo Author: Anton Stöckl About Zend_Cache_Backend_Mongo is a Zend Framework Cache Backend for MongoDB. It supports tags and autoc

PHP local cache

__ ____ _________ ______/ /_ ___ / __ \/ ___/ __ `/ ___/ __ \/ _ \ / /_/ / /__/ /_/ / /__/ / / / __/ / ._

A fast, lock-free, shared memory user data cache for PHP

Yac is a shared and lockless memory user data cache for PHP.

Comments
  • Adding

    Adding "Etag" header

    Hello,

    First, thank you for this middleware collection!

    I'm wondering if, as stated on https://github.com/micheh/psr7-cache/blob/master/src/CacheUtil.php#L118-L121, it is a good idea to add the ETag header when we are returning some cached response?

    What do you think? I can try to do a PR if you think it's a good idea.

    opened by wysow 4
  • http-factory

    http-factory

    Package http-interop/http-factory is abandoned, you should avoid using it. Use psr/http-factory instead.

    This message appears in some of your repositories.

    Greetings.

    opened by playinteractive 1
Owner
Middlewares
PSR-15 HTTP Middlewares
Middlewares
PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

null 1 Oct 15, 2021
Simple cache abstraction layer implementing PSR-16

sabre/cache This repository is a simple abstraction layer for key-value caches. It implements PSR-16. If you need a super-simple way to support PSR-16

sabre.io 48 Sep 9, 2022
This is a Symfony bundle that lets you you integrate your PSR-6 compliant cache service with the framework

PSR-6 Cache bundle This is a Symfony bundle that lets you you integrate your PSR-6 compliant cache service with the framework. It lets you cache your

null 43 Oct 7, 2021
Distributed PSR-16 cache implementation for PHP 6 that uses browser cookies to store data

cookiecache Cache beyond the edge with cookies! This library provides a distributed PSR-16 compatible cache implementation for PHP 6 that uses browser

Colin O'Dell 8 Apr 19, 2022
DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

DataLoaderPHP is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

Webedia - Overblog 185 Nov 3, 2022
The place to keep your cache.

Stash - A PHP Caching Library Stash makes it easy to speed up your code by caching the results of expensive functions or code. Certain actions, like d

Tedious Developments 944 Jan 4, 2023
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

Donate/Support: Documentation: https://www.scrapbook.cash - API reference: https://docs.scrapbook.cash Table of contents Installation & usage Adapters

Matthias Mullie 295 Nov 28, 2022
Cache slam defense using a semaphore to prevent dogpile effect.

metaphore PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampending herd or Slashdot effect). Problem: t

Przemek Sobstel 102 Sep 28, 2022
:zap: Simple Cache Abstraction Layer for PHP

⚡ Simple Cache Class This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction with your cache-server. You can define

Lars Moelleken 27 Dec 8, 2022
Doctrine Cache component

Doctrine Cache Cache component extracted from the Doctrine Common project. Documentation This library is deprecated and will no longer receive bug fix

Doctrine 7.6k Jan 3, 2023