Quick tip on how to use cache in Laravel

Categorized as Laravel Tagged

You can easily cache a database query or any other data in the HTTP requests with Laravel’s Cache facade. The closure function passed to the remember method will be used if the cache doesn’t exist. See the example below:

Route::get('/countries', function () {
  return Cache::remember('countries', 600, function () {
    // cache for 10 minutes
    return DB::table('countries')->get();
  });
});

Leave a reply

Your email address will not be published. Required fields are marked *