PHPackages                             natsumework/laravel-cache-manager - 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. natsumework/laravel-cache-manager

ActiveLibrary[Caching](/categories/caching)

natsumework/laravel-cache-manager
=================================

Allows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.

1.1.0(2y ago)4429MITPHPPHP ^7.3 || ^8.0

Since Jan 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/natsumework/laravel-cache-manager)[ Packagist](https://packagist.org/packages/natsumework/laravel-cache-manager)[ RSS](/packages/natsumework-laravel-cache-manager/feed)WikiDiscussions main Synced 1mo ago

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

Easily manage your cache for laravel
====================================

[](#easily-manage-your-cache-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bad3e84fd6ec42add672d823690dc2327925298bf930727fccfcd274472a12e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e617473756d65776f726b2f6c61726176656c2d63616368652d6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/natsumework/laravel-cache-manager)[![Test Status](https://camo.githubusercontent.com/e9d406a0b9726ff41fe72fc8978bfdab0b99e5ccefebb949705456a94ee2c178/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e617473756d65776f726b2f6c61726176656c2d63616368652d6d616e616765722f746573742e796d6c3f6272616e63683d6d61696e)](https://camo.githubusercontent.com/e9d406a0b9726ff41fe72fc8978bfdab0b99e5ccefebb949705456a94ee2c178/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6e617473756d65776f726b2f6c61726176656c2d63616368652d6d616e616765722f746573742e796d6c3f6272616e63683d6d61696e)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/eb30c6d7502b40a88a1bbfe3bd0249c0bf5c0ef32a22701c90f7efb3073d0c06/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e617473756d65776f726b2f6c61726176656c2d63616368652d6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/natsumework/laravel-cache-manager)

Allows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.

Contents
--------

[](#contents)

- [Installation](#installation)
- [Configuration](#configuration)
    - [General Configuration](#general-configuration)
    - [Hotspot Invalid Protection Configuration](#hotspot-invalid-protection-configuration)
    - [Penetrate Protection Configuration](#penetrate-protection-configuration)
    - [Types Configuration](#types-configuration)
- [Usage](#usage)
- [Changelog](#changelog)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

Install this package via composer:

```
composer require natsumework/laravel-cache-manager

```

Publish the configuration file to `config/cache-manager.php`:

```
php artisan vendor:publish --provider="Natsumework\CacheManager\CacheManagerServiceProvider"

```

Configuration
-------------

[](#configuration)

`config/cache-manager.php`

### General Configuration

[](#general-configuration)

```
return [
    // ...

    /*
    |--------------------------------------------------------------------------
    | Cache Store
    |--------------------------------------------------------------------------
    |
    | This option controls the cache connection that gets used while
    | using this caching library.
    |
    | Before use the "store", you need to define your `Cache Stores` in config/cache.php
    |
    | @see https://laravel.com/docs/master/cache#configuration
    */

    'store' => 'file',

    /*
    |--------------------------------------------------------------------------
    | Storage time
    |--------------------------------------------------------------------------
    |
    | Specifies that items will expire in a few seconds.
    | If set to null, items will be stored indefinitely.
    }
    */
    'ttl' => 600,

    // ...
];

```

### Hotspot Invalid Protection Configuration

[](#hotspot-invalid-protection-configuration)

```
return [
    // ...

    /*
    |--------------------------------------------------------------------------
    | Hotspot Invalid Protection
    |--------------------------------------------------------------------------
    |
    | When we set the cache, we usually set an expiration time for the cache.
    | After this time, the cache will be invalid.
    | For some hotspot data, when the cache fails, there will be a large number
    | of requests coming over, and then hit the database, which will cause the
    | database enormous pressure.
    |
    | Set "hotspot_protection" to `true` will use the "Mutex lock" to avoid
    | the problem of database corruption caused by the failure of hotspot data.
    |
    | You must confirm that the store you are using supports atomic-locks.
    | @see https://laravel.com/docs/master/cache#atomic-locks
    */
    'hotspot_protection' => false,

    /*
    |--------------------------------------------------------------------------
    | Hotspot Invalid Protection Lock Timeout
    |--------------------------------------------------------------------------
    |
    | Specifies that "Mutex lock" will be automatically released in a few seconds.
    |
    */
    'hotspot_protection_ttl' => 15,

    /*
    |--------------------------------------------------------------------------
    | Hotspot Invalid Protection Suffix
    |--------------------------------------------------------------------------
    |
    | When Hotspot Invalid Protection is enabled, `{type}:{index}:{hotspot_protection_suffix}`
    | will be used as the key of "Mutex lock".
    | You must specify a unique value and avoid using this value as the key for storing data,
    | otherwise the data may be overwritten due to duplicate keys.
    |
    */
    'hotspot_protection_suffix' => 'hotspot_protection',

    // ...
];

```

### Penetrate Protection Configuration

[](#penetrate-protection-configuration)

```
return [
    // ...

    /*
    |--------------------------------------------------------------------------
    | Penetrate Protection
    |--------------------------------------------------------------------------
    |
    | When the business system initiates a query, the query will first go to
    | the cache, if the cache does not exist, it will go to the database for query.
    | If no data is found in the database, the data will not be stored in the cache,
    | which will cause the query hit the database every time.
    |
    | When Penetrate protection is enabled, `null` data will be converted to `false`
    | and stored in the cache.
    */
    'penetrate_protection' => false,

    // ...
];

```

### Types Configuration

[](#types-configuration)

This configuration allows you to easily manage all caches here.

##### Notes

[](#notes)

- Before you put item into the cache storage, you have to define the type first.

```
return [
    // ...

    /*
    |--------------------------------------------------------------------------
    | Cache Types
    |--------------------------------------------------------------------------
    | Before you put item into the cache storage, you have to define the type first.
    |
    | You can also define `ttl`, `hotspot_protection`, `penetrate_protection`,
    | `hotspot_protection_ttl` for each type. It takes precedence over a globally defined one.
    |
    */
    'types' => [
        'user' => [
            'ttl' => 300, // seconds
            'hotspot_protection' => true,
            'penetrate_protection' => true,
            'hotspot_protection_ttl' => 10, // seconds
        ],
        'active_users' => [
            'ttl' => 60 * 60,
            'hotspot_protection' => false,
            'penetrate_protection' => false,
        ],
        'post' => [],

        // ...
    ],

    // ...
];

```

Usage
-----

[](#usage)

#### Retrieve &amp; Store item

[](#retrieve--store-item)

If the item does not exist in the cache, the Closure passed to the remember method will be executed and its result will be placed in the cache.

```
use Natsumework\CacheManager\Facades\CacheManager;

// CacheManager::remember($type, $index = null, \Closure $callback = null)

// you must define type `user` in config/cache-manager.php
$user = CacheManager::remember('user', 1, function () {
    return User::find(1);
});

```

```
$data = CacheManager::remember('type', 999, function () {
    return null;
});

// If `penetration_protection` is enabled, $data === false
// If `penetration_protection` is not enabled, $data === null

```

#### After the item is updated, use the Cache-Aside Pattern to invalid the item

[](#after-the-item-is-updated-use-the-cache-aside-pattern-to-invalid-the-item)

The `updated` method will remove item.

```
// CacheManager::updated($type, $index = null)

$user = User::find(1);
$user->name = 'New name';
$user->save();

// user updated
CacheManager::updated('user', 1);

```

You can also use the `forget` method to remove item.

```
CacheManager::forget('user', 1);

```

#### Retrieve item

[](#retrieve-item)

```
use Natsumework\CacheManager\Facades\CacheManager;

// CacheManager::get($type, $index = null, $default = null)

// you must define type `user` in config/cache-manager.php
$user = CacheManager::get('user', 1, 'user not found');

```

#### Storing Items In The Cache

[](#storing-items-in-the-cache)

```
use Natsumework\CacheManager\Facades\CacheManager;

// CacheManager::put($type, $index = null, $value = null)

// you must define type `user` in config/cache-manager.php
CacheManager::put('user', 1, User::find(1));

```

Changelog
---------

[](#changelog)

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

Testing
-------

[](#testing)

```
$ composer test
```

Security
--------

[](#security)

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

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

[](#contributing)

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

Credits
-------

[](#credits)

- [natsumework](https://github.com/natsumework)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

Total

2

Last Release

781d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

cachecachemanagerlaravelredislaravelrediscache

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/natsumework-laravel-cache-manager/health.svg)

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[anahkiasen/flatten

A package for the Illuminate framework that flattens pages to plain HTML

33313.0k](/packages/anahkiasen-flatten)

PHPackages © 2026

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