PHPackages                             thecodingmachine/cache-utils - 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. [Caching](/categories/caching)
4. /
5. thecodingmachine/cache-utils

ActiveLibrary[Caching](/categories/caching)

thecodingmachine/cache-utils
============================

Store file related cache items easily

v1.0.0(6y ago)22.8M↓26.5%3[2 PRs](https://github.com/thecodingmachine/cache-utils/pulls)3MITPHPPHP &gt;=7.2

Since Jun 20Pushed 1y ago2 watchersCompare

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

READMEChangelog (1)Dependencies (8)Versions (2)Used By (3)

[![Latest Stable Version](https://camo.githubusercontent.com/0d586a5a997d3d004786d411a47ef54ab01ce3dbbb288dab114b689f3d6a62fa/68747470733a2f2f706f7365722e707567782e6f72672f746865636f64696e676d616368696e652f63616368652d7574696c732f762f737461626c65)](https://packagist.org/packages/thecodingmachine/cache-utils)[![Total Downloads](https://camo.githubusercontent.com/0f693b681789634cd905c9b82c58b155e1d40f6c85284e41ea43169f888a6fed/68747470733a2f2f706f7365722e707567782e6f72672f746865636f64696e676d616368696e652f63616368652d7574696c732f646f776e6c6f616473)](https://packagist.org/packages/thecodingmachine/cache-utils)[![Latest Unstable Version](https://camo.githubusercontent.com/4de0a2989191b431395d0e2497043aa27a1b7e88db6fc67a9ac5234df0c60294/68747470733a2f2f706f7365722e707567782e6f72672f746865636f64696e676d616368696e652f63616368652d7574696c732f762f756e737461626c65)](https://packagist.org/packages/thecodingmachine/cache-utils)[![License](https://camo.githubusercontent.com/40a340b4ba877637a5620f687d3a2cb83d6f8933019402843ad2b3b2374df589/68747470733a2f2f706f7365722e707567782e6f72672f746865636f64696e676d616368696e652f63616368652d7574696c732f6c6963656e7365)](https://packagist.org/packages/thecodingmachine/cache-utils)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/50826c4f5a84cf95ca1589ca53854222ef68b5db7b4c151019e7fd36453a1601/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f746865636f64696e676d616368696e652f63616368652d7574696c732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/thecodingmachine/cache-utils/?branch=master)[![Build Status](https://camo.githubusercontent.com/1962cc5f8e3a5cff10de57ef6b2a3b482dee4fea17abfc9de02e9979af15db09/68747470733a2f2f7472617669732d63692e6f72672f746865636f64696e676d616368696e652f63616368652d7574696c732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/thecodingmachine/cache-utils)[![Coverage Status](https://camo.githubusercontent.com/3c2f074c7b1e74523143e0f9217a5ee0fc5f4397393f932e0dcefdf49a8961b8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f746865636f64696e676d616368696e652f63616368652d7574696c732f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/thecodingmachine/cache-utils?branch=master)

Why?
----

[](#why)

This package contains a number of utility classes to play with PSR-6 caches.

### File bound cache

[](#file-bound-cache)

Most PHP cache systems (like PSR-6 or PSR-16) are storing items in cache and attributing to items a time to live (TTL).

If you are developing a PHP framework or a PHP analysis library that relies a lot on reflection, it is quite common to have cache items that are related to PHP files or PHP classes.

For instance, Doctrine Annotations in a class do not change unless the class file(s) is changed. Therefore, it makes sense to bind the cache invalidation to the modification date of the file. *thecodingmachine/cache-utils* provides just that.

```
use TheCodingMachine\CacheUtils\FileBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);

// Put the $myDataToCache object in cache.
// If 'FooBar.php' and 'FooBaz.php' are modified, the cache item is purged.
$fileBoundCache->set('cache_key', $myDataToCache,
[
    'FooBar.php',
    'FooBaz.php'
]);

// Fetching data
$myDataToCache = $fileBoundCache->get('cache_key');
```

You can also use the `FileBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.

```
use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\FileBoundMemoryAdapter;

$fileBoundCache = new FileBoundMemoryAdapter(new FileBoundCache($psr6Cache));
```

### Class bound cache

[](#class-bound-cache)

You can also bind a cache item to a class / trait / interface using the `ClassBoundCache` class. The cache will expire if the class / trait / interface is modified.

```
use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundCache($fileBoundCache);

// Put the $myDataToCache object in cache.
// If the FooBar class is modified, the cache item is purged.
$classBoundCache->set('cache_key', $myDataToCache, new ReflectionClass(FooBar::class));

// Fetching data
$myDataToCache = $classBoundCache->get('cache_key');
```

The `ClassBoundCache` constructor accepts 3 additional parameters:

```
class ClassBoundCache implements ClassBoundCacheInterface
{
    public function __construct(FileBoundCacheInterface $fileBoundCache, bool $analyzeParentClasses = true, bool $analyzeTraits = true, bool $analyzeInterfaces = false)
}
```

- `$analyzeParentClasses`: if set to true, the cache will be invalidated if one of the parent classes is modified
- `$analyzeTraits`: if set to true, the cache will be invalidated if one of the traits is modified
- `$analyzeInterfaces`: if set to true, the cache will be invalidated if one of the interfaces implemented is modified

You can also use the `ClassBoundMemoryAdapter` to store the cache in memory for even faster access in the same query.

```
use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;

$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));
```

### Easier interface with cache contracts

[](#easier-interface-with-cache-contracts)

You can even get an easier to use class bound cache using the `ClassBoundCacheContract`.

```
use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));
$classBoundCacheContract = new ClassBoundCacheContract(new ClassBoundCache($fileBoundCache));

// If the FooBar class is modified, the cache item is purged.
$item = $classBoundCache->get(new ReflectionClass(FooBar::class), function() {
    // ...
    // let's return the item to be cached.
    // this function is called only if the item is not in cache yet.
    return $item;
});
```

With cache contracts, there is not setters. Only a getter that takes in parameter a callable that will resolve the cache item if the item is not available in the cache.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity54

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

Unknown

Total

1

Last Release

2525d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

![](https://avatars.githubusercontent.com/u/1847918?v=4)[TheCodingMachine](/maintainers/thecodingmachine)[@thecodingmachine](https://github.com/thecodingmachine)

---

Top Contributors

[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (24 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/thecodingmachine-cache-utils/health.svg)

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

###  Alternatives

[laminas/laminas-cache

Caching implementation with a variety of storage options, as well as codified caching strategies for callbacks, classes, and output

1076.9M130](/packages/laminas-laminas-cache)[cache/adapter-common

Common classes for PSR-6 adapters

11124.4M38](/packages/cache-adapter-common)[cache/filesystem-adapter

A PSR-6 cache implementation using filesystem. This implementation supports tags

705.8M82](/packages/cache-filesystem-adapter)[cache/array-adapter

A PSR-6 cache implementation using a php array. This implementation supports tags

548.3M151](/packages/cache-array-adapter)[cache/redis-adapter

A PSR-6 cache implementation using Redis (PhpRedis). This implementation supports tags

523.9M27](/packages/cache-redis-adapter)[cache/simple-cache-bridge

A PSR-6 bridge to PSR-16. This will make any PSR-6 cache compatible with SimpleCache.

423.1M27](/packages/cache-simple-cache-bridge)

PHPackages © 2026

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