Skip to content

Conversation

@giuspe
Copy link

@giuspe giuspe commented Oct 23, 2025

Problem

As a developer I would like to be able to use Cache::forever with a flexible approach to trigger background refreshes.

By definition, Cache::forever never automatically refreshes the value unless manually handled via application logic.

Proposal

We add a flexibleForever utility method to cache Repository which can serve stale values while triggering deferred refreshing (leveraging the new Cache::flexible method).

The new method exposes the same signature as Cache::remember while implementing a flexible version of Cache::forever

public function flexibleForever($key, $refreshTtl, $callback, $lock = null, $alwaysDefer = false)
{
   return $this->flexible($key, [$refreshTtl, Carbon::now()->addYears(1000)], $callback, $lock, $alwaysDefer);
}

Of course the same result could be achieved at application level via registering a macro in AppServiceProvider

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // [...]

        Cache::macro('flexibleForever', function (string $key, int $ttl, callable $generator) {
            return Cache::flexible($key, [$ttl, Carbon::now()->addYears(1000)], $generator);
        });
    }
}

or via simply calling Cache::flexible with a fixed, high upper ttl.

@giuspe giuspe changed the title Feature/cache flexible forever [12.x] Feature/cache flexible forever Oct 23, 2025
@ahinkle
Copy link
Contributor

ahinkle commented Oct 23, 2025

I believe the flexible pattern idea was about graceful degradation of stale data - serving cached content while refreshing it in the background. "Forever" implies content that never goes stale, which seems to contradict the pattern's purpose.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@giuspe
Copy link
Author

giuspe commented Oct 24, 2025

Thank you @ahinkle and @taylorotwell for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants