R
R
Rastishka2021-05-22 23:49:53
caching
Rastishka, 2021-05-22 23:49:53

How to properly cache a long request so that parallel requests wait until the cache is generated?

The picture is generated for 10 seconds.
Is it possible to make it start being generated and placed in the cache the first time the picture is accessed, while other parallel requests (while the picture is being generated) would wait for the cache to be ready and return it from the cache?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Babichev, 2021-05-23
@Rastishka

Yes, there are locks for that.
https://laravel.com/docs/8.x/cache#managing-locks

use Illuminate\Support\Facades\Cache;

$lock = Cache::lock('pic' . $id, 10);

if ($lock->get()) {
    // генерируем картинку
    $lock->release();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question