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.

Reply to

Use Markdown with GitHub-flavored code blocks.