PHPackages                             aldeebhasan/laravel-cache-flusher - 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. aldeebhasan/laravel-cache-flusher

ActiveLibrary[Caching](/categories/caching)

aldeebhasan/laravel-cache-flusher
=================================

A lightweight package to invalidate cache entries automatically when the cached data changed.

1.1.0(2y ago)36MITPHPPHP &gt;=8.1

Since Oct 27Pushed 2y ago1 watchersCompare

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

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

Laravel Cache Flusher
=====================

[](#laravel-cache-flusher)

A lightweight package to invalidate cache entries automatically when the cached data changed.

Why
---

[](#why)

The current cache management system provided by laravel is great and satisfiable. To handle the cache invalidation when data update, you can do that manually or you may group your cache entries under tags and do invalidation by tags (like Redis). Even with tagging, it becomes difficult to handle all of tags spread all over the code base when the project get larger.

This package provides an easy way to track the changes over models and do an automatic invalidation in your behave. Just define the relation between your cache and models and your life will become easier.

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

[](#installation)

Install using composer:

```
composer require aldeebhasan/laravel-cache-flusher
```

After installation run the following code to publish the config:

```
php artisan vendor:publish --tag=cache-flusher

```

Basic Usage
-----------

[](#basic-usage)

It is very simple to use this package, you need only to configure all the required variables and taste the sweet of this package

### 1) enabled (default : false)

[](#1-enabled-default--false)

To enable/disable this package in your project , you can control its value using the entry `CACHE_FLUSHER_ENABLED`in your .env file.

### 2) driver (default : your default cache driver)

[](#2-driver-default--your-default-cache-driver)

It will be used to specify the cache driver the package will work with, it is preferable to use the cache driver similar to the one you used in your project (to make sure that all the operations done on the same cache driver). you can control its value using the entry `CACHE_FLUSHER_DRIVER`in your .env file.

### 3) cool-down (default : null)

[](#3-cool-down-default--null)

Some time you have a bulk of operation done over the model/s within short period of time. To avoid the high rate cache invalidation for a specific period (cool down period) you can specify a value (`in seconds`) for this config entry. you can control its value using the entry `CACHE_FLUSHER_COOL_DOWN`in your .env file.

### 4) mapping (default : \[\])

[](#4-mapping-default--)

The most important part in our package. Here you can specify the cache keys along with the models that cause their invalidation.

In the following example, the `store_info`,`store_info.categories` will be invalidated when Category model changed (create|update|delete) the `store_info`,`store_info.products` will be invalidated when Product model changed (create|update|delete)

```
 'mapping' => [
    'store_info'  =>  [ Product::class, Category::class ],
    'store_info.categories'  =>  [  Category::class ],
    'store_info.products'  =>  [  Product::class ],
 ]
```

The keys can also be a regex expression, and when any change over the models occurred, all the matched keys will be invalidated

In the following example, all they cache entry started with `store.` or `mobile.` will be invalidated if Product or Category changed (create|update|delete)

```
 'mapping' => [
    '^(store\..+|mobile\..+)$'  =>  [ Product::class, Category::class ]
 ]
```

All they cache entry end with `.products` or `.categories.` will be invalidated if Product, Category, or Attribute changed (create|update|delete)

```
 'mapping' => [
    '^(.*\.products|.*\.categories)$'  =>  [ Product::class, Category::class,Attribute::class ]
 ]
```

Advanced Usage
--------------

[](#advanced-usage)

Some time you may want to provide some condition when invalidating the cache keys. One examples: you have a multi tenant project and you want to invalidate the cache entries of a specific company.

The package can help you with that by providing a binding resolving within the defined `mapping` config param. One last thing, you have to provide a binding function (in your service provider) to extract the related values from the model that trigger the invalidation operation.

Example:

```
// define the binding mapping function
class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
        //.... other boot methods

       CacheFlusher::setBindingFunction(
            function (string $bindingKey, Model $model): ?string {
                switch ($bindingKey) {
                    case "company_id":
                        return '1'; //$model->company_id
                    case "user_id":
                        return "2"; // $model->user_id;
                }
                return null;
            });
    }
}
```

The binding function accept two params: `$bindingKey` which represent the matched binding key. `$model` is the model who triggered the invalidation operation. The defined function will be called for each matched param in the defined cache entry patterns (`mapping` in config file)

Now, according to the following mapping configuration :

```
'mapping' => [
    '^companies\.{company_id}\.stores' => [User::class],
    '^companies\.{company_id}\.mobiles\.{user_id}' => [User::class],
]
```

When the `User` model changed, all the entries that start with `companies.1.stores` and `companies.1.mobiles.2` will be invalidated.

NOTE: {company\_id} is fixed to 1 in the binding function, {user\_id} is fixed to 2 in the binding function.

License
-------

[](#license)

Laravel Cache Flusher package is licensed under [The MIT License (MIT)](LICENSE).

Security contact information
----------------------------

[](#security-contact-information)

To report a security vulnerability, contact directly to the developer contact email [Here](mailto:aldeeb.91@gmail.com).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

Total

2

Last Release

928d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/74e7b9d93b0f666f462ad0be43dde05991e9d141f9fe840009775808107d440e?d=identicon)[aldeebhasan](/maintainers/aldeebhasan)

---

Top Contributors

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

---

Tags

bulk-cache-operationscach-invalidationcacheflush-cachelaravellaravel-cachepurge-cachelaravelcachelaravel-cachePurge Cacheflush cachbulk cach operation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/aldeebhasan-laravel-cache-flusher/health.svg)

```
[![Health](https://phpackages.com/badges/aldeebhasan-laravel-cache-flusher/health.svg)](https://phpackages.com/packages/aldeebhasan-laravel-cache-flusher)
```

###  Alternatives

[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[imanghafoori/laravel-widgetize

A minimal yet powerful package to give a better structure and caching opportunity for your Laravel apps.

909137.9k12](/packages/imanghafoori-laravel-widgetize)[swayok/alternative-laravel-cache

Replacements for Laravel's redis and file cache stores that properly implement tagging idea. Powered by cache pool implementations provided by http://www.php-cache.com/

202541.1k6](/packages/swayok-alternative-laravel-cache)[dragon-code/laravel-cache

An improved interface for working with cache

6844.8k10](/packages/dragon-code-laravel-cache)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)[karriere/state

Laravel package for storing current application state in cache/session

1718.5k](/packages/karriere-state)

PHPackages © 2026

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