If your application runs a large number of queries very often, with time it will become very, very sluggish. Here Laravel caching comes handy. Laravel provides a simple mechanism for caching these queries using a very simple chained method call. Here is an example using Laravel’s Fluent Query Builder:
1 2 3 4 5 |
$users = DB::table('users') ->orderBy('latest_activity', 'desc') ->take(10) ->remember(60) ->get(); |
Of course, we can do the same thing using Eloquent:
1 2 3 4 |
$users = User::orderBy('latest_activity', 'desc') ->take(10) ->remember(60) ->get(); |
Behind the scene Laravel executes the query and then stores it along with the query result using the cache adapter, with an expiration time of 60 minutes. Running the same query again will result that cached query will be found, which means it will not be executed again, instead the results will be taken from the cache. Continue reading Laravel: Caching Database Queries