PHPackages                             dantleech/sf-http-cache-tagging - 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. dantleech/sf-http-cache-tagging

ActiveLibrary

dantleech/sf-http-cache-tagging
===============================

Symfony HTTP Cache Tagging middleware

71.0k1[1 issues](https://github.com/dantleech/sf-http-cache-tagging/issues)PHP

Since Mar 21Pushed 10y ago2 watchersCompare

[ Source](https://github.com/dantleech/sf-http-cache-tagging)[ Packagist](https://packagist.org/packages/dantleech/sf-http-cache-tagging)[ RSS](/packages/dantleech-sf-http-cache-tagging/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Symfony HTTP Cache Tagging Middleware
=====================================

[](#symfony-http-cache-tagging-middleware)

[![Build Status](https://camo.githubusercontent.com/0fa3379880b5e6247f6b2f66ca357cb57c609e9b2827bf68de9e879436780a86/68747470733a2f2f7472617669732d63692e6f72672f64616e746c656563682f73662d687474702d63616368652d74616767696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dantleech/sf-http-cache-tagging)[![StyleCI](https://camo.githubusercontent.com/58bfd960291594776be168207fdb3bf83d3bc5ddce4a36ab75979e4c7995e710/68747470733a2f2f7374796c6563692e696f2f7265706f732f35343133313434322f736869656c64)](https://styleci.io/repos/54131442)

Introduction
------------

[](#introduction)

This package provides a middleware which allows you to add "tagging" capabilties to the [Symfony HTTPCache](http://symfony.com/doc/current/book/http_cache.html).

What this means is that you can associate responses with tags, which can later be invalidated. What THIS means is that you can cache your responses indefinitely and invalidate them only when the content on the page changes.

Features
--------

[](#features)

- Middleware to add tagging capability to the Symfony HTTP Cache.
- Configurable and extensible tag storage.
- Local and remote tag invalidation.
- Configurable HTTP headers.
- Configurable tag encoding.

Quick Start
-----------

[](#quick-start)

### Installation

[](#installation)

Require the library with composer:

```
$ composer require dtl/http-cache-tagging

```

You will need a storage strategy, it is easiest to use the `DoctrineCache`strategy, and for this you will need the `doctrine/cache` package:

```
$ composer require doctrine/cache

```

### Wrapping the kernel

[](#wrapping-the-kernel)

```
use Doctrine\Common\Cache\PhpFileCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use DTL\Symfony\HttpCacheTagging\Storage\DoctrineCache;
use DTL\Symfony\HttpCacheTagging\Manager\TagManager;
use DTL\Symfony\HttpCacheTagging\TaggingKernel;

// your main application
$app = new TestKernel();

// the standard Symfony HTTP cache
$store = new Store('/path/to/keep/cache');
$httpCache = new HttpCache($app, $store);

// our tag storage strategy
$tagStorage = new DoctrineCache(new PhpFileCache('/path/to/keep/tags'));
$tagManager = new TagManager($tagStorage, $store);

// now you can procss the request
$app = new TaggingKernel($httpCache, $tagManager);
$app->handle(Request::create());
```

### Tagging your response

[](#tagging-your-response)

To tag the response just add the tags to the configured tag header (`X-Cache-Tags` by default).

```
class MyController
{
    // ..
    public function someAction(Request $request)
    {

        $id = 1;
        $entity = $this->entitymanager->find($id);
        $tag = get_class($entity) . $id;

        $response = Response::create($entity->getHelloWorld());
        $response->headers->set('X-Cache-Tags', json_encode([ $tag ]));

        return $response;
    }
}
```

Note that above we used JSON encode to convert the tags to a string. A JSON encoded string is expected by default, however you may also choose to use `comma-seperated` value strategy or an encoding system of your choice by sepecifying a callback in the `tag_encoding` option.

### Invalidating cache entries with tags

[](#invalidating-cache-entries-with-tags)

Invalidation can be done in three different ways:

- Direct invalidation.
- Request invalidation.
- Response invalidation.

#### Direct invalidation

[](#direct-invalidation)

Is where you purge the cache directly from your application, for this you will need to inject the `TagManager` which you instantiated into your Application kernel.

#### Request invalidation

[](#request-invalidation)

Is where you send separate HTTP request to the caching server or servers. By default this should be a `POST` request with the tags encoded in the `X-Cache-Invalidate-Tags` header.

Note that only request invalidation can be used when you have multiple servers.

#### Response invalidation

[](#response-invalidation)

Is where you set the invalidation headers in the HTTP response. This has the same advantage as direct invalidation, but avoids having to inject the tag manager as a service. The `X-Cache-Invalidate-Tags` header is expected by default.

The response method is probably the most simple:

```
class MyController
{
    // ...

    public function editAction(Request $request)
    {
        $id = 1;
        $entity = $this->entitymanager->find($id);
        $tag = get_class($entity) . $id;

        // update the entity

        $response = // get your response
        $response->headers->set('X-Cache-Invalidate-Tags', json_encode([ $tag ]));

        return $response;
    }
}
```

Here we are editing an object which represents a page on your website, we set the response header, after the response has been sent any cache entries which have the entities tag will be removed.

You will also need to update the kernel wrapping to:

```
$kernel = new TaggingKernel($httpCache, $tagManager, array('invalidate_from_response' => true));
```

### Configuration

[](#configuration)

- **purge\_method**: Purge method to use when invalidating remotely, note that the Symfony HTTP cache does not support the `PURGE` method.
- **header\_tags**: Header to use when tagging the response, `X-Cache-Tags`by default.
- **header\_invalidate\_tags**: Header to use for invalidating tags in either request (remote) or response (local). Default is `X-Cache-Invalidate-Tags`.
- **tag\_encoding**: How the tags should be decoded by the middleware, can be `json` (default), `comma-separate` or a PHP callable which will receive the raw tag string and return an array.
- **ips**: List of IP addresses which may remotely invalidate the cache.

License
-------

[](#license)

This library is released under the MIT license. See the included [LICENSE](LICENSE) file for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.2% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/24ec7d5d6b7ea54007be5d7b4f43800381cc1e22929f7d2276fba30e497fdfa6?d=identicon)[dantleech](/maintainers/dantleech)

---

Top Contributors

[![dantleech](https://avatars.githubusercontent.com/u/530801?v=4)](https://github.com/dantleech "dantleech (19 commits)")[![pbowyer](https://avatars.githubusercontent.com/u/89852?v=4)](https://github.com/pbowyer "pbowyer (4 commits)")[![xabbuh](https://avatars.githubusercontent.com/u/1957048?v=4)](https://github.com/xabbuh "xabbuh (1 commits)")

### Embed Badge

![Health badge](/badges/dantleech-sf-http-cache-tagging/health.svg)

```
[![Health](https://phpackages.com/badges/dantleech-sf-http-cache-tagging/health.svg)](https://phpackages.com/packages/dantleech-sf-http-cache-tagging)
```

PHPackages © 2026

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