Reputation: 622
I want to store an array in Cache. I'm using "MemCached" as Cache driver and it works like a charm in queries, but I want to use it for my search suggestions. I've been looking at Laravel Documentation (http://laravel.com/docs/5.1/cache) but I haven't been able to find what I were looking for.
For example, I have this function in which if I find the element on Cache I get it. If not, I get results from DB and then store them on Cache for 10 minutes:
public function get_suggestions() {
if(Cache::get('search_suggestions')) {
return $search_suggestions;
} else {
$search_suggestions = Suggestions::get();
Cache::put('search_suggestions', '$search_suggestions', 10);
return $search_suggestions;
}
}
Some questions:
Upvotes: 0
Views: 4242
Reputation: 15629
You could use Cache::has
to check for existance.
But in your case I suggest to use Cache::remember
public function get_suggestions() {
return Cache::remember('search_suggestions', 10, function() {
return Suggestions::get();
});
}
Upvotes: 1