PHPackages                             light-speak/laravel\_model\_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. light-speak/laravel\_model\_cache

ActiveLibrary[Caching](/categories/caching)

light-speak/laravel\_model\_cache
=================================

基于Redis的laravel模型数值缓存，可以减少并发修改对数据库的压力，减少数据库事务锁的使用

v2.0.9(2y ago)573MITPHPPHP &gt;=8.0

Since Dec 10Pushed 2y ago1 watchersCompare

[ Source](https://github.com/light-speak/laravel_model_cache)[ Packagist](https://packagist.org/packages/light-speak/laravel_model_cache)[ Docs](https://github.com/light-speak/laravel_model_cache)[ RSS](/packages/light-speak-laravel-model-cache/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (1)Versions (16)Used By (0)

Laravel Model Cache
===================

[](#laravel-model-cache)

A caching plugin based on Laravel ORM and Redis Cache, relying on queue's model attribute caching

Hold Model instance, proxy fields, use cache modification

[中文文档](https://github.com/light-speak/laravel_model_cache/blob/master/README_zh_CN.md)

Notice！
-------

[](#notice)

Data modification is only applicable to numeric types, and the operation is performed by 1000 times, please pay attention to the precision

Install
-------

[](#install)

```
composer require light-speak/laravel_model_cache
```

illustrate
----------

[](#illustrate)

This plug-in all uses Cache. The Redis storage function used by Cache is to proxy a field of an object of a certain model, and use Cache to maintain it for a period of time (the default is 15 seconds) All internal modifications are made in the Cache to optimize performance

- Modify the cache for 15 seconds
- Read cache for 3~24 hours

use
---

[](#use)

- A queue has been added to the queue, named model-cache, which is used to maintain fields
- Embed in the Model class that needs to be used

```
    use useGYCache;

```

- call the cache() method after the original model

```
    $wallet = Wallet::query()->first()->cache();

```

- Start using it normally

Detailed usage
--------------

[](#detailed-usage)

### Modify data and use plugins for model proxying

[](#modify-data-and-use-plugins-for-model-proxying)

```
    /**
     * @param bool $useTransaction Whether to use transactions, if true, you must call the saveCache() method to save
     *
     * @return self|CacheModel
     */
    public function cache(bool $useTransaction = false): self|CacheModel
    {
        $this->has_cache = true;
        return ModelCache::make($this, __CLASS__, $useTransaction);
    }

```

Call the cache method of the Model, return a CacheModel instance, and at the same time have the return type of the Model instance itself, without affecting the IDE prompt

```
    $wallet = Wallet::query()->first()->cache();

    debug($wallet->balance); // Normal access to Model's field, the first access will store it in the cache

```

Use incrementByCache and decrementByCache to modify values

At this time, the regular model Wallet::query()-&gt;first()-&gt;balance will also get the latest cached data

```
    $wallet = Wallet::query()->first()->cache();
    debug("Current wallet amount (check Cache): $wallet->balance"); // 100
    $wallet->incrementByCache('balance', 100);

    $wallet = Wallet::query()->first();
    $wallet->balance = 100;
    debug("Current wallet amount (check DB): $wallet->balance"); // 200
    $wallet->save();
    debug("Current wallet amount (check DB): $wallet->balance"); // 100
    $wallet = Wallet::query()->first()->cache();
    debug("Current wallet amount (check Cache): $wallet->balance"); // 100

```

CacheModel object and Model try to keep consistency

### Transactions

[](#transactions)

```
    $wallet = Wallet::query()->first();
    $wallet->balance = 200;
    $wallet->save();

    $wallet1 = Wallet::query()->first()->cache(true); // This must be saved to be useful here
    $wallet1->incrementByCache('balance', 100); // Originally 200 here is an invalid modification

    $wallet = Wallet::query()->first();
    info($wallet->balance); // 200

    $wallet = Wallet::query()->first()->cache();
    info($wallet->balance); // 200

    $wallet1 = Wallet::query()->first()->cache(true);
    $wallet1->incrementByCache('balance', 100);
    $wallet1->saveCache(); // It is equal to 100 from here, and it will take effect if it is saved

```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Recently: every ~90 days

Total

15

Last Release

1032d ago

Major Versions

v1.0.4 → v2.0.02022-09-04

PHP version history (2 changes)v1.0.1PHP &gt;=7.1.0

v2.0.1PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/42b4cae99d5d1ac4fe8d90779782585100d95250603347ebee5021143ec8a00e?d=identicon)[guangyu](/maintainers/guangyu)

---

Top Contributors

[![light-speak](https://avatars.githubusercontent.com/u/31815444?v=4)](https://github.com/light-speak "light-speak (31 commits)")

### Embed Badge

![Health badge](/badges/light-speak-laravel-model-cache/health.svg)

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

###  Alternatives

[backpack/crud

Quickly build admin interfaces using Laravel, Bootstrap and JavaScript.

3.4k3.7M223](/packages/backpack-crud)[unopim/unopim

UnoPim Laravel PIM

10.5k2.4k](/packages/unopim-unopim)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21318.6k3](/packages/ecotone-laravel)[duncanmcclean/statamic-cargo

Comprehensive e-commerce addon for Statamic. Build bespoke e-commerce sites without the complexity.

3416.6k](/packages/duncanmcclean-statamic-cargo)[byerikas/cache-tags

Allows for Redis/Valkey cache flushing multiple tagged items by a single tag.

1420.4k](/packages/byerikas-cache-tags)

PHPackages © 2026

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