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

ActiveLibrary[Caching](/categories/caching)

modethirteen/fluent-cache
=========================

A fluent cache builder API for PSR-16 compatible cache components

1.1.0(5y ago)03.1k1Apache-2.0PHPPHP ^7.2.0

Since Dec 19Pushed 4y ago1 watchersCompare

[ Source](https://github.com/modethirteen/FluentCache)[ Packagist](https://packagist.org/packages/modethirteen/fluent-cache)[ RSS](/packages/modethirteen-fluent-cache/feed)WikiDiscussions main Synced 5d ago

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

FluentCache
===========

[](#fluentcache)

A fluent cache builder API for [PSR-16](https://www.php-fig.org/psr/psr-16/) compatible cache components

[![github.com](https://github.com/modethirteen/FluentCache/workflows/build/badge.svg)](https://github.com/modethirteen/FluentCache/actions?query=workflow%3Abuild)[![codecov.io](https://camo.githubusercontent.com/28ebdf117b0fc42a493b3e01556643a9ce151a8ce83136c47e89979d268fbcfe/68747470733a2f2f636f6465636f762e696f2f6769746875622f6d6f6465746869727465656e2f466c75656e7443616368652f636f7665726167652e7376673f6272616e63683d6d61696e)](https://codecov.io/github/modethirteen/FluentCache?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/1d17d052ec6051f97b75e77268792cb08bc3702efc48f6b31ebef458901d7c49/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6465746869727465656e2f666c75656e742d63616368652f76657273696f6e2e737667)](https://packagist.org/packages/modethirteen/fluent-cache)[![Latest Unstable Version](https://camo.githubusercontent.com/f6c45e359e3e09deac61cb26d5896437fe7bf1dc683bfe8822e304f3824d51c3/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6465746869727465656e2f666c75656e742d63616368652f762f756e737461626c65)](https://packagist.org/packages/modethirteen/fluent-cache)

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

[](#requirements)

- PHP 7.4 (main, 2.x)

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

[](#installation)

Use [Composer](https://getcomposer.org/). There are two ways to add FluentCache to your project.

From the composer CLI:

```
./composer.phar require modethirteen/fluent-cache
```

Or add modethirteen/fluent-cache to your project's composer.json:

```
{
    "require": {
        "modethirteen/fluent-cache": "dev-main"
    }
}
```

`dev-main` is the main development branch. If you are using FluentCache in a production environment, it is advised that you use a stable release.

Assuming you have setup Composer's autoloader, FluentCache can be found in the `modethirteen\FluentCache\` namespace.

Usage
-----

[](#usage)

The principal type that FluentCache provides is `CacheBuilder`. `CacheBuilder` is an immutable object that takes references to a cache and anonymous functions to generate a cache key, build cacheable objects, validate results, and hook an event dispatcher.

`CacheBuilder` handles the responsibility for, and obfuscates, the steps required to orchestrate the most common scenario for handling cached data:

- Check the cache for an object
    - If miss, build an object, set it in the cache, and return the object
    - If hit, return the object

Managing these steps, choosing what to profile, when to validate, or other cache-specific decision-making isn't the responsibility of the calling code: the caller just wants to *get* an object, it shouldn't care if it comes from a cache, if the data is stale, or the object is built for the first time. `CacheBuilder` separates this concern and encapsulates it, exposing custom logic hooks for reasonable flexibility.

```
class Memcache implements \Psr\SimpleCache\CacheInterface {}

class Dispatcher implements \Psr\EventDispatcher\EventDispatcherInterface {}

$result = (new CacheBuilder())
    ->withCache(new Memcache(), function() : ?string {

        // generate a cache key - if null is returned, then the cache will be ignored
        return 'foo';
    })
    ->withCacheLifespanBuilder(function($result) : int {

        // what is the ttl for objects set in the cache?
        return 1500;
    })
    ->withCacheValidator(function($result) : bool {

        // assert that cached object is valid when fetched
        if(!($result instanceof Bar)) {
            return false;
        }
        return $result->someProperty === 'some value';
    })
    ->withBuilder(function() : object {

        // build a cacheable object if cache miss
        return new Bar();
    })
     ->withBuildValidator(function($result) : object {

        // assert that post-cache miss built object is valid
        return $result instanceof Bar;
    })

    // send cache and build stage events to trigger downstream actions such as profiling
    ->withEventDispatcher(new Dispatcher())

    // ...or set a lazy dispatcher to initialize when we attempt to fetch an object
    ->withLazyEventDispatcher(function(CacheBuilder $this) : EventDispatcherInterface {
        return new Dispatcher();
    })

    // set a custom identifier to track this cache session in downstream event processors
    // ...session ids are automatically generated if not provided
    // ...session ids are re-generated everytime the immutable cache builder is cloned
    ->withSessionId('123')

    // fetch object from cache or build it and set it in the cache - the caller doesn't care!
    ->get();
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

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 ~12 days

Total

2

Last Release

1961d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bfdbec0ff13b1d7836c00d751876485508071cf26e6dcc2bae7fb7333b6f40f?d=identicon)[modethirteen](/maintainers/modethirteen)

---

Top Contributors

[![modethirteen](https://avatars.githubusercontent.com/u/45862?v=4)](https://github.com/modethirteen "modethirteen (26 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/modethirteen-fluent-cache/health.svg)

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

###  Alternatives

[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[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)[neos/cache

Neos Cache Framework

102.0M31](/packages/neos-cache)[graham-campbell/bounded-cache

A Bounded TTL PSR-16 Cache Implementation

101.9M6](/packages/graham-campbell-bounded-cache)[cache/void-adapter

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

183.0M44](/packages/cache-void-adapter)

PHPackages © 2026

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