$peopleQuery = People::where('status', 'active');
$people['total'] = $peopleQuery->count();
Cache::remember('people', 60, function () use ($people) {
return $people;
});
return Cache::get('people');
The cache is not working as expected.
The question is when I use Cache::remember should I use Cache::get() because from the documentation it seems not?
Thanks for the answer.
@yiga you are executing the query outside the cache callback; so it will run every time. As @bugsysha described, executing the query inside the callback will result in the query executing only whenever the cache expires!
@Yiga the variation is an incorrect use of caching; you cache because you want to prevent executing the query. However, your second snippet will execute the query every time.
This is the appropriate approach:
return Cache::remember('people', 60, function () {
return People::where('status', 'active')->count();
});
@Yiga understanding documentation should be the first thing you learn. Everything else is secondary. Especially considering that Laravel has easy and clear documentation.
I never learn to use tools, frameworks, libraries, etc. I only learn to interpret documentation.