PHPackages                             codefocus/managedcache - 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. codefocus/managedcache

ActiveLibrary[Caching](/categories/caching)

codefocus/managedcache
======================

Self-invalidating cache for Laravel

10[1 issues](https://github.com/codefocus/managedcache/issues)PHP

Since Aug 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/codefocus/managedcache)[ Packagist](https://packagist.org/packages/codefocus/managedcache)[ RSS](/packages/codefocus-managedcache/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3ba6b135029a9556b5477efdd98959b50f6bd99a0ef72c3e0262932a090e4678/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6465666f6375732f6d616e6167656463616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codefocus/managedcache)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/5e5dd33e599b88dc78899b5fafe0c79afe2ccf3286aa0bebac16cda6b3f4b9be/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6465666f6375732f6d616e6167656463616368652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/codefocus/managedcache)[![Code coverage](https://camo.githubusercontent.com/76c0d7278601d1872c9aa0860a154e0a2079edda8c92399bd4a14a02d199f888/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f636f6465666f6375732f6d616e6167656463616368652e737667)](https://codecov.io/gh/codefocus/managedcache)[![Quality Score](https://camo.githubusercontent.com/3d67b267eb0a96ed027eb7f374c9e6e5a20b0a452464ae376a44b9a41102be10/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f6465666f6375732f6d616e6167656463616368652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/codefocus/managedcache)[![StyleCI](https://camo.githubusercontent.com/f7afca6589d88578dc338489f02ec5b5509a07951ec38af1e8622bc98a4b4208/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130303737353937352f736869656c64)](https://styleci.io/repos/100775975)[![Total Downloads](https://camo.githubusercontent.com/56e9704dd8b606d6329f375b796623d13e5de269601a2469a8cf7b535e1fb26e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6465666f6375732f6d616e6167656463616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codefocus/managedcache)

ManagedCache
============

[](#managedcache)

> "There are only two hard problems in Computer Science: cache invalidation and naming things."

\-- Phil Karlton

ManagedCache solves one of these.

When caching data (say, a fully hydrated `User` with all of its related data -- roles, subscriptions, preferences, etc.), you would normally have to invalidate that cache in *every part of the code where one of those roles, subscriptions or preferences is modified*.

ManagedCache lets you define the invalidation criteria in the same statement that caches the data.

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

[](#requirements)

### Cache driver requirements

[](#cache-driver-requirements)

ManagedCache uses [tags](https://laravel.com/docs/master/cache#cache-tags) to perform its automatic event-based invalidation. This means that your cache driver should support tags (the `file` and `database` drivers do not).

ManagedCache currently only supports the Memcached cache driver. Support for other cache drivers such as Redis is planned.

### Model requirements

[](#model-requirements)

Models that are used in [invalidation conditions](#automatic-invalidation) should have an integer primary key.

Install
-------

[](#install)

Via Composer

```
$ composer require codefocus/managedcache
```

Quick start
-----------

[](#quick-start)

### Provider

[](#provider)

Add `ManagedCacheProvider` to the "providers" array in `config/app.php`

```
Codefocus\ManagedCache\Providers\ManagedCacheProvider::class
```

### Facade

[](#facade)

ManagedCache also provides a Facade.

To use it, add `Codefocus\ManagedCache\Facades\ManagedCache` to your `use` statements.

Usage
-----

[](#usage)

### Compatibility

[](#compatibility)

ManagedCache implements the `Illuminate\Contracts\Cache\Store` interface, and can be used as a drop-in replacement for the `Cache` Facade.

```
//  Retrieve a user.
$user = User::with(['roles', 'subscriptions', 'preferences'])->find($userId);

//  Store data as you normally would.
$cacheKey = 'users(' . $userId . ')';
ManagedCache::put($cacheKey, $user, 120);

...

//  Retrieve data as you normally would.
$user = ManagedCache::get($cacheKey);
```

### Automatic invalidation

[](#automatic-invalidation)

To automatically invalidate this data, call the `setForgetConditions()` function at the start of the function chain, passing in an array of invalidation conditions.

These invalidation conditions (`Codefocus\ManagedCache\Condition` objects) are named after the Eloquent events that trigger them, and can be created with intuitive helper functions:

```
//  Store data, and invalidate this cache when one of these conditions is met:
//  - This User is deleted
//  - This User is updated
//  - A Role is attached to this User
//  - A Role is detached from this User
//  - A Role attached to this User is updated
//  - A Subscription is attached to this User
//  - A Subscription is detached from this User
//  - A Subscription attached to this User is updated
//  - A Preference is attached to this User
//  - A Preference is detached from this User
//  - A Preference attached to this User is updated
ManagedCache
    ::setForgetConditions(function ($conditionBuilder) use ($user) {
        return $conditionBuilder
            ->modelDeleted($user)
            ->modelUpdated($user)
            ->relatedModelAttached($user, Role::class)
            ->relatedModelDetached($user, Role::class)
            ->relatedModelUpdated($user, Role::class)
            ->relatedModelAttached($user, Subscription::class)
            ->relatedModelDetached($user, Subscription::class)
            ->relatedModelUpdated($user, Subscription::class)
            ->relatedModelAttached($user, Preference::class)
            ->relatedModelDetached($user, Preference::class)
            ->relatedModelUpdated($user, Preference::class);
    })
    ->put($cacheKey, $user, 120);
```

As you can see, more complex data with many invalidation conditions could get cumbersome to define. To invalidate a cache key on *any* of the Eloquent events (`created`, `updated`, `saved`, `deleted` or `restored`) for the specified Model class or instance, use `anyModelEvent()` and `anyRelatedModelEvent()`:

```
//  Store data, and invalidate this cache when one of these conditions is met:
//  - This User is created, updated, saved, deleted or restored
//  - A Role attached to this User is created, updated, saved, deleted or restored
//  - A Subscription attached to this User is created, updated, saved, deleted or restored
//  - A Preference attached to this User is created, updated, saved, deleted or restored
ManagedCache
    ::setForgetConditions(function ($conditionBuilder) use ($user) {
        return $conditionBuilder
            ->anyModelEvent($user)
            ->anyRelatedModelEvent($user, Role::class)
            ->anyRelatedModelEvent($user, Subscription::class)
            ->anyRelatedModelEvent($user, Preference::class);
    })
    ->put($cacheKey, $user, 120);
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

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

[](#contributing)

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

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Menno van Ens](https://github.com/codefocus)
- [Anthony Tsui](https://github.com/matresstester)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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/1cd3f45eae9b147c79013baf9779842a8256bd56cdd5e1408b1f9aac9ff11aa0?d=identicon)[codefocus](/maintainers/codefocus)

### Embed Badge

![Health badge](/badges/codefocus-managedcache/health.svg)

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

###  Alternatives

[beryllium/cachebundle

Provides an interface to Memcache for Symfony2 applications

32136.0k](/packages/beryllium-cachebundle)

PHPackages © 2026

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