PHPackages                             mikespub/middlewares-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. mikespub/middlewares-cache

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mikespub/middlewares-cache
==========================

Middleware with various cache utilities

v3.1.0(1y ago)06MITPHPPHP &gt;=8.1CI passing

Since Oct 3Pushed 5mo agoCompare

[ Source](https://github.com/mikespub-org/middlewares-cache)[ Packagist](https://packagist.org/packages/mikespub/middlewares-cache)[ Docs](https://github.com/middlewares/cache)[ RSS](/packages/mikespub-middlewares-cache/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (12)Versions (16)Used By (0)

middlewares/cache
=================

[](#middlewarescache)

[![Latest Version on Packagist](https://camo.githubusercontent.com/cabf215db10276e568c540ad51c43da4fbdaa0c34f3d3a0aaa92a2b43826b605/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/cache)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/cache/workflows/testing/badge.svg)](https://github.com/middlewares/cache/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/468b55966acd73ec3bd047c968fda993c26f5092d52d425fdf413b6457001fb4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/cache)

Middleware components with the following cache utilities:

- [CachePrevention](#cacheprevention)
- [Expires](#expires)
- [Cache](#cache)
- [ClearSiteData](#clearsitedata)

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

Installation
------------

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/cache](https://packagist.org/packages/middlewares/cache).

```
composer require middlewares/cache
```

CachePrevention
---------------

[](#cacheprevention)

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

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

Expires
-------

[](#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](src/expires_defaults.php).

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

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

### defaultExpires

[](#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
-----

[](#cache)

Saves the response headers in a [PSR-6 cache pool](http://www.php-fig.org/psr/psr-6/) and returns `304` responses (Not modified) if the response is still valid (based on its `ETag` or `Last-Modified` header). 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](https://github.com/middlewares/utils#factory) will be used to detect it automatically.

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

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

ClearSiteData
-------------

[](#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](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

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

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance59

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 75.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~237 days

Recently: every ~484 days

Total

14

Last Release

469d ago

Major Versions

v0.5.0 → v1.0.02018-01-25

v1.1.0 → v2.0.02019-12-01

2.2.0 → v3.0.02023-12-17

PHP version history (5 changes)v0.1.0PHP ^5.6 || ^7.0

v0.5.0PHP ^7.0

v2.0.0PHP ^7.2

v2.1.0PHP ^7.2 || ^8.0

2.2.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1447115?v=4)[Mike's Pub](/maintainers/mikespub)[@mikespub](https://github.com/mikespub)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (55 commits)")[![mikespub](https://avatars.githubusercontent.com/u/1447115?v=4)](https://github.com/mikespub "mikespub (13 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (5 commits)")

---

Tags

httppsr-7middlewarepsr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mikespub-middlewares-cache/health.svg)

```
[![Health](https://phpackages.com/badges/mikespub-middlewares-cache/health.svg)](https://phpackages.com/packages/mikespub-middlewares-cache)
```

###  Alternatives

[mezzio/mezzio

PSR-15 Middleware Microframework

3923.8M126](/packages/mezzio-mezzio)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

587.2M101](/packages/laminas-laminas-stratigility)[middlewares/request-handler

Middleware to execute request handlers

451.8M30](/packages/middlewares-request-handler)[middlewares/fast-route

Middleware to use FastRoute

98205.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

46458.6k11](/packages/middlewares-negotiation)[middlewares/trailing-slash

Middleware to normalize the trailing slash of the uri path

32539.7k14](/packages/middlewares-trailing-slash)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
