Support the ongoing development of Laravel.io →
Cache

hey guys! how do I avoid cache slams (https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html#cache-slams) ? this question is not about doctrine but cache in general

I need something like this

//pseudo code
// $cacheKey = 'randomCacheKey'.
if(Cache::has($cacheKey)) {
 return Cache::get($cacheKey);
}
//do some work
$valueToCache = $this->someComplexTask();
Cache::set($cacheKey, $valueToCache);
return $valueToCache;

the question is, how do I need to do it, to avoid cache slams? For example if I have 200 parallel requests, and all of them will notice that there is no cache, they all will try to write to same, key, with will lead to spike in cpu/memory/db queries and etc. so I need only one of them to write to this cache, and all others should wait for it. How do I do it?

this one is probably has something to do with atomic locks but it's not clear to me how do I use it, doc's for it doesn't me (it's too much "hello world"-like example).

Last updated 3 years ago.

vladyslavstartsev liked this thread

1

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.