PHPackages                             laragear/cache-refresh - 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. laragear/cache-refresh

AbandonedArchivedLibrary[Caching](/categories/caching)

laragear/cache-refresh
======================

Save items in a bucket, retrieve them later.

v2.0.0(2y ago)819.1k↓49%1MITPHPPHP ^8.1CI passing

Since Jul 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Laragear/CacheRefresh)[ Packagist](https://packagist.org/packages/laragear/cache-refresh)[ Fund](https://github.com/sponsors/DarkGhostHunter)[ Fund](https://paypal.me/darkghosthunter)[ RSS](/packages/laragear-cache-refresh/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (7)Used By (0)

This package has been superseded by [Laravel Cache Atomic Lock](https://laravel.com/docs/cache#atomic-locks).
=============================================================================================================

[](#this-package-has-been-superseded-by-laravel-cache-atomic-lock)

---

Cache Refresh
=============

[](#cache-refresh)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a2c8becee469333a0c39220d6f2aae5812435394729d17b3928159ae85a7f2c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c617261676561722f63616368652d726566726573682e737667)](https://packagist.org/packages/laragear/cache-refresh)[![Latest stable test run](https://github.com/Laragear/CacheRefresh/workflows/Tests/badge.svg)](https://github.com/Laragear/CacheRefresh/actions)[![Codecov coverage](https://camo.githubusercontent.com/c8221f7fa8bc52d0ece4467339c1862ce478981c8625d66d0dcbc589d515dff8/68747470733a2f2f636f6465636f762e696f2f67682f4c617261676561722f4361636865526566726573682f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d4f5555576c754e627236)](https://codecov.io/gh/Laragear/CacheRefresh)[![Maintainability](https://camo.githubusercontent.com/6955a47620179ff33e052cf13a74dc61ee6fa59b0fd31c3c7a6b20c3b6aef2c9/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36666230636331363866323662336632343562632f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Laragear/CacheRefresh/maintainability)[![Sonarcloud Status](https://camo.githubusercontent.com/3fda4d90121ce4c623499184f5e0f8e4271e9cdbfb16006519d50ef32fa59684/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d4c617261676561725f436163686552656672657368266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=Laragear_CacheRefresh)[![Laravel Octane Compatibility](https://camo.githubusercontent.com/70359a356da237cd29561bc5d0bb80baae775b5ff62f288ed324755382858342/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2532304f6374616e652d436f6d70617469626c652d737563636573733f7374796c653d666c6174266c6f676f3d6c61726176656c)](https://laravel.com/docs/11.x/octane#introduction)

Refresh items in your cache without data races.

```
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use App\Models\Message;

public function send(Message $message)
{
    Cache::refresh(
        $message->to,
        fn ($messages) => Collection::wrap($messages)->push($message)
    );
}
```

Become a sponsor
----------------

[](#become-a-sponsor)

[![](.github/assets/support.png)](https://github.com/sponsors/DarkGhostHunter)

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **[spread the word!](http://twitter.com/share?text=I%20am%20using%20this%20cool%20PHP%20package&url=https://github.com%2FLaragear%2FCacheRefresh&hashtags=PHP,Laravel)**

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

[](#requirements)

- Laravel 10 or later
- Cache Driver with Lock support (\*).

Warning

You can still use Cache Refresh without a driver that supports locking, but bear in mind, **refreshing won't be atomic**.

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

[](#installation)

You can install the package via Composer:

```
composer require laragear/cache-refresh
```

Usage
-----

[](#usage)

Cache Refresh will retrieve a key value from your cache store that you can edit using a callback. This callback is free to change the value and return it to be persisted.

When the cached value doesn't exist, like when is first called, you will receive `null`, so remember to *un-null* the value when is first called.

```
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use App\Models\Message;

public function send(Message $message)
{
    // Add the incoming message to a list of messages, refreshing the overall list.
    $messages = Cache::refresh(
        $message->to,
        function (?Collection $messages) use ($message) {
            return Collection::wrap($messages)->push($message);
        },
        60 * 5
    );

    return 'Messages has been queued';
}
```

### Custom Expiration time

[](#custom-expiration-time)

The callback also receives an `Expire` instance, which will allow you to change the expiration time of the key inside the callback.

```
use Illuminate\Support\Facades\Cache;
use Laragear\CacheRefresh\Expire;
use App\Models\Mission;

Cache::refresh('mission', function ($mission, Expire $expire) {
    $mission ??= new Mission();

    if ($mission->ongoing()) {
        // Set a new expiration time.
        $expire->at(today()->endOfDay());
    }

    if ($mission->completed()) {
        // Expire the value immediately.
        $expire->now();
    }

    if ($mission->isVeryDifficult()) {
        // Put it forever.
        $expire->never();
    }

    return $mission;
}, 60 * 5);
```

### Custom Lock configuration

[](#custom-lock-configuration)

You can omit a callback to manage the lock time and the waiting time using `lock()` and `waitFor()`, respectively, and issue the callback using `put()`.

```
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use App\Models\Message;

Cache::refresh('mission')->lock(60)->waitFor(10)->put(fn ($value) => ..., 60 * 5);
```

PhpStorm stubs
--------------

[](#phpstorm-stubs)

For users of PhpStorm, there is a stub file to aid in macro autocompletion for this package. You can publish them using the `phpstorm` tag:

```
php artisan vendor:publish --provider="Laragear\CacheRefresh\CacheRefreshServiceProvider" --tag="phpstorm"
```

The file gets published into the `.stubs` folder of your project. You should point your [PhpStorm to these stubs](https://www.jetbrains.com/help/phpstorm/php.html#advanced-settings-area).

Laravel Octane compatibility
----------------------------

[](#laravel-octane-compatibility)

- There are no singletons using a stale application instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.

There should be no problems using this package with Laravel Octane.

Security
--------

[](#security)

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

License
=======

[](#license)

This specific package version is licensed under the terms of the [MIT License](LICENSE.md), at time of publishing.

[Laravel](https://laravel.com) is a Trademark of [Taylor Otwell](https://github.com/TaylorOtwell/). Copyright © 2011-2024 Laravel LLC.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 70.9% 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 ~156 days

Recently: every ~229 days

Total

7

Last Release

476d ago

Major Versions

1.x-dev → v2.0.02024-03-06

PHP version history (3 changes)v1.0.0PHP &gt;=8.0.2

v1.1.0PHP 8.\*

v2.0.0PHP ^8.1

### Community

Maintainers

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

---

Top Contributors

[![DarkGhostHunter](https://avatars.githubusercontent.com/u/5141911?v=4)](https://github.com/DarkGhostHunter "DarkGhostHunter (39 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (15 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

laravelpackagecacherefresh

### Embed Badge

![Health badge](/badges/laragear-cache-refresh/health.svg)

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

###  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)[dragon-code/laravel-cache

An improved interface for working with cache

6844.8k10](/packages/dragon-code-laravel-cache)[anahkiasen/flatten

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

33113.0k](/packages/anahkiasen-flatten)[nexxai/laravel-cfcache

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)[omaralalwi/lexi-translate

Laravel translation package with morph relationships and caching.

754.3k2](/packages/omaralalwi-lexi-translate)

PHPackages © 2026

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