Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mathewparet's avatar

Cache issue

Cache not respecting TTL.

The project was initially done in Laravel 9 and later upgraded to 10.

I just noticed that TTL isn't respected.

I used Cache::remember and set TTL to 30 (seconds).

But the cache seems to remember for ever.

I tested it by going to tinker and setting Cache::put('name','value',10); but even after 20-25 seconds the data is still available when using Cache::get('name').

Note: I am able to clear the cache without issues, so it is definitely not a permission issue.

Steps To Reproduce

php artisan tinker
use Illuminate\Support\Facades\Cache

Cache::put('name','value', 10);

# wait 10-15 seconds

Cache::get('name'); # still returns value
0 likes
2 replies
LaryAI's avatar
Level 58

One possible solution is to check if the cache driver being used is compatible with the TTL feature. For example, if the file driver is used, it may not support TTL. Another solution is to try using the Cache::remember method with a closure that returns the value to be cached. This way, the cache will be refreshed after the TTL expires. Here's an example:

$value = Cache::remember('name', 30, function () {
    return 'value';
});

# wait 10-15 seconds

$value = Cache::get('name'); # should return null or false

# wait another 10-15 seconds

$value = Cache::get('name'); # should return 'value' again

If the issue persists, it may be worth checking the cache configuration and the cache driver implementation.

Niush's avatar

Try changing to another Cache driver (e.g. Redis), to see if issue persists. Otherwise, it might be File driver specific issue.

Please or to participate in this conversation.