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

GodziLaravel's avatar

How to remove all caches prefixed by a specific string ?

Hello ,

I created a cache depending to the user id :

Cache::forever('users_list'.$userId, $values);

I want when I add users to update the cache values , so I delete all the caches starting with users_list

like that I will delete users_list1 users_list2 users_list3 users_list4 users_list5 users_list6...

0 likes
5 replies
GodziLaravel's avatar

@michaloravec ,

I have one more question please : If I want to put the TTL value outside of this controller , where can I put it ?

(something like config)

MichalOravec's avatar

I prefer to have it there and use a carbon for example

$value = Cache::remember('users', now()->addHours(12), function () {
    return DB::table('users')->get();
});
MichalOravec's avatar

I mean that instead of putting a number there, it;s better to use for example now()->addHours(12)

1 like

Please or to participate in this conversation.