PHPackages                             smartondev/httpcache - 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. smartondev/httpcache

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

smartondev/httpcache
====================

Simple HTTP cache management library

0.6.0(1y ago)130.5k—1.6%Apache-2.0PHPPHP ^8.2CI passing

Since Jul 28Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/smartondev/httpcache)[ Packagist](https://packagist.org/packages/smartondev/httpcache)[ RSS](/packages/smartondev-httpcache/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (3)Versions (10)Used By (0)

Build HTTP cache headers, ETag and modified matchers
====================================================

[](#build-http-cache-headers-etag-and-modified-matchers)

[![GitHub Release](https://camo.githubusercontent.com/0ccb51767c3a209df5dcba00a7f3b5340c8c912d8a9de0e86ef0d1ae3eff9da6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f736d6172746f6e6465762f6874747063616368653f696e636c7564655f70726572656c6561736573)](https://camo.githubusercontent.com/0ccb51767c3a209df5dcba00a7f3b5340c8c912d8a9de0e86ef0d1ae3eff9da6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f736d6172746f6e6465762f6874747063616368653f696e636c7564655f70726572656c6561736573)[![Packagist Downloads](https://camo.githubusercontent.com/dec94c7433ad8ac5feb3dda95a3b1e9c7c79800871837ef55c4319def5769dca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f736d6172746f6e6465762f687474706361636865)](https://camo.githubusercontent.com/dec94c7433ad8ac5feb3dda95a3b1e9c7c79800871837ef55c4319def5769dca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f736d6172746f6e6465762f687474706361636865)[![Pest - GitHub Actions](https://camo.githubusercontent.com/6c140bc1c501c7615921be06c4bcdae79bb2a060d58d98a70722f42b93cc0485/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6172746f6e6465762f6874747063616368652f706573742e796d6c3f6c6162656c3d7465737473)](https://camo.githubusercontent.com/6c140bc1c501c7615921be06c4bcdae79bb2a060d58d98a70722f42b93cc0485/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6172746f6e6465762f6874747063616368652f706573742e796d6c3f6c6162656c3d7465737473)[![PHPStan level 10 - GitHub Actions](https://camo.githubusercontent.com/09c0baafcb8e420117b1f6980e7bc221ef22646deb3b0d919695e83ee819132e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6172746f6e6465762f6874747063616368652f7068707374616e2e796d6c3f6c6162656c3d5048505374616e2532306c6576656c2532303130)](https://camo.githubusercontent.com/09c0baafcb8e420117b1f6980e7bc221ef22646deb3b0d919695e83ee819132e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6172746f6e6465762f6874747063616368652f7068707374616e2e796d6c3f6c6162656c3d5048505374616e2532306c6576656c2532303130)[![Coverage Status](https://camo.githubusercontent.com/547b8498aa27c896b785afd6afd1b524a01185e13d4efc38478949b951ca574f/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c73436f7665726167652f6769746875622f736d6172746f6e6465762f6874747063616368653f6c6162656c3d636f766572616c6c73)](https://coveralls.io/github/smartondev/httpcache?branch=main)[![Codecov](https://camo.githubusercontent.com/502ef94aedddc0ea96bc3ab4cf61e983e3fb8ac26010b804badc45faf681a088/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f736d6172746f6e6465762f6874747063616368653f6c6162656c3d636f6465636f76)](https://app.codecov.io/gh/smartondev/httpcache)[![GitHub License](https://camo.githubusercontent.com/1d2d2d8960732db57585f96f4e13675577c79efeaa883cb5c8bc797c4c2990bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736d6172746f6e6465762f687474706361636865)](LICENSE)

This package helps you to build HTTP cache headers like `Cache-Control`, `ETag` and matchers like `If-None-Match`, `If-Modified-Since`. It is useful for building HTTP cache headers and matchers in your application.

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

[](#installation)

```
composer require smartondev/httpcache
```

Usage
-----

[](#usage)

### Cache headers

[](#cache-headers)

```
use SmartonDev\HttpCache\Builders\CacheHeaderBuilder;

// max-age 1 hour, private, no-store
$headers = (new CacheHeaderBuilder())
    ->maxAge(hours: 1)
    ->private()
    ->noStore()
    ->toHeaders();

// max-age 60 sec, shared max age 120 sec, stale-while-revalidate 30 sec
$headers = (new CacheHeaderBuilder())
    ->maxAge(60)
    ->sharedMaxAge(120)
    ->staleWhileRevalidate(30)
    ->toHeaders();
```

#### No-cache

[](#no-cache)

```
$noCacheHeaders = (new CacheHeaderBuilder())
    ->noCache()
    ->toHeaders();
```

#### Durations

[](#durations)

```
$builder = (new CacheHeaderBuilder())
    ->maxAge(30) // 30 sec
    ->maxAge(seconds: 30) // 30 sec
    ->maxAge(minutes: 30) // 30 min
    ->maxAge(hours: 30) // 30 hours
    ->maxAge(days: 30) // 30 days
    ->maxAge(weeks: 30) // 30 weeks
    ->maxAge(months: 30) // 30 months
    ->maxAge(years: 30) // 30 years
    ->maxAge(days: 10, hours: 5, minutes: 30) // 10 days 5 hours 30 minutes
```

### Etag check

[](#etag-check)

```
use SmartonDev\HttpCache\Matchers\ETagMatcher;

// ETag check with If-None-Match header
$etagMatcher = (new ETagMatcher())
    ->headers($requestHeaders);
$activeEtag = '"1234"';
if($etagMatcher->matches($activeEtag)->matchesIfNoneMatchHeader()) {
    // 304 Not Modified
    return response(null, 304);
}
```

### Modified check

[](#modified-check)

```
use SmartonDev\HttpCache\Matchers\ModifiedMatcher;

// Check If-Modified-Since header
$modifiedMatcher = (new ModifiedMatcher())
    ->headers($requestHeaders);
if(!$modifiedMatcher->matches($lastModified)->isModifiedSince()) {
    // 304 Not Modified - resource hasn't been modified
    return response(null, 304);
}
```

Mutable and immutable accessors
-------------------------------

[](#mutable-and-immutable-accessors)

- `with` prefixed methods are immutable, eg. `withMaxAge()`. Methods without `with` prefix are mutable, eg. `maxAge()`.
- `without` prefixed methods are immutable, eg. `withoutMaxAge()`. Methods with `reset` prefix are mutable, eg. `resetMaxAge()`.

```
$builderA = new CacheHeaderBuilder();
// mutable
$builderA->maxAge(30)
         ->resetMaxAge();

// immutable
$builderB = $builderA->withMaxAge(60);
$builderC = $builderB->withoutMaxAge();
```

More documentation
------------------

[](#more-documentation)

- [CacheHeaderBuilder](doc/cache-header-builder.md): building cache headers like `Cache-Control`
- [ETagHeaderBuilder](doc/etag-header-builder.md): building ETag header
- [ETagMatcher](doc/etag-matcher.md): matching ETag headers like `If-Match`, `If-None-Match`
- [ModifiedMatcher](doc/modified-matcher.md): matching modified headers like `If-Modified-Since`, `If-Unmodified-Since`

### Framework guides

[](#framework-guides)

- [Laravel guide](doc/laravel-guide.md)
- [Slim guide](doc/slim-guide.md)
- [Symfony guide](doc/symfony-guide.md)
- [Vanilla PHP guide](doc/vanilla-php-guide.md)

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

License
-------

[](#license)

This project is open-sourced software [licensed under](LICENSE).

Author
------

[](#author)

- [Márton Somogyi](https://github.com/kamarton)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance62

Regular maintenance activity

Popularity30

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~23 days

Recently: every ~34 days

Total

8

Last Release

495d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a752abf82b63be991429512fd0f95260532868511626d6aab9a9891b6177d6e?d=identicon)[smartondev](/maintainers/smartondev)

---

Top Contributors

[![kamarton](https://avatars.githubusercontent.com/u/9432032?v=4)](https://github.com/kamarton "kamarton (123 commits)")

---

Tags

etag-headerhttp-cachephp8

###  Code Quality

TestsPest

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smartondev-httpcache/health.svg)

```
[![Health](https://phpackages.com/badges/smartondev-httpcache/health.svg)](https://phpackages.com/packages/smartondev-httpcache)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

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