V
V
Valery Khizhevsky2018-05-17 12:54:19
Laravel
Valery Khizhevsky, 2018-05-17 12:54:19

How to cache data from the database every 15 minutes?

Good day!
A large amount of data is taken from the database to plot, but the data only changes every 15 minutes. How to make the data cached and no longer wait for the selection, but immediately give it to the user from the cache?
Does Laravel have any tools?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kairat Ubukulov, 2018-05-17
@Roshette

Everything is written in the documentation.
For example.

$result = DB::select("SELECT * FROM goods"); // пример запроса

if(!Cache::has('goods')){
       // если в кэше нет то сохраняем в кэш
      Cache::put('goods', $result, 15); // на 15 минут сохраняем в кэш
      $goods = Cache::get('goods'); // получаем из кэша
}else{
       // получаем из кэша
      $goods = Cache::get('goods'); // получаем из кэша
}

P
pLavrenov, 2018-06-04
@pLavrenov

There is no need to do extra checks. If there is no cache, then it creates it .. something like this.

$value = Cache::get('cacheId', function () {
    $data = Model::all();
    Cache::put('cacheId', $data, 15);
    return $data;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question