Working with caching in Laravel 5

Posted 1 year ago by hillcow

Hi,

so if I would like to cache database queries, I would have to do the following every single time, right?

if(!Cache::has('test')) {
        return Cache::remember('test', 1, function () {
            return App\Post::all();
        });
    } else {
        return Cache::get('test');
    }

Since the above syntax is always the same, I could easily create a model "cacheHelper" and then call cacheHelper::action($name, $time, $query):

if(!Cache::has($name)) {
        return Cache::remember($name, $time, function () {
            return $query;
        });
    } else {
        return Cache::get($name);
    }

Is this how you would do it since Laravel 5 doesn't have the ::remember() function for Eloquent models any more? Any help is truly appreciated, thanks!

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.