V
V
Vatrush2019-05-06 12:31:39
Laravel
Vatrush, 2019-05-06 12:31:39

How to properly cache JSON in laravel?

I cache JSON like this

$json = file_get_contents('http://json/');
        Cache::put('key',$json,5);
        $cache = Cache::get('key');

But the bottom line is that this json can only be accessed once a minute, what is the right way to cache it in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2019-05-06
@Vatrush

This is how the third-party service will be called no more than once per minute:

Cache::remember('key', 60, function () {
    return file_get_contents('http://json/');
});

But only if two requests do not arrive in parallel, when the data is rotten - then you need to use locks .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question