Image for post
Image for post

Cache Eloquent queries in Laravel 6+

Alex Renoki
Nov 23, 2019 · 2 min read

When it comes to speeding up your application, caching can be the best thing to achieve. Laravel comes up with cache drivers pre-installed so you can enjoy the experience out-of-the-box. Redis, Memcached or just using local files, Laravel comes packed with this.

This time, we will talk about caching Eloquent queries directly from the models, thus making the database caching a breeze!

The package can be found on GitHub, where the documentation will approach all of the main points of the app. However, during this article, you’ll learn just the basics of caching and clearing cache so you can get started before you will dig deeper.

The package can be installed through Composer:

$ composer require rennokki/laravel-eloquent-query-cache

Your models will need the QueryCacheable trait:

use Rennokki\QueryCache\Traits\QueryCacheable;class Article extends Model
{
use QueryCacheable;
...
}

By default, the package does not enable query caching. To achieve this, add the $cacheFor variable in your model:

use Rennokki\QueryCache\Traits\QueryCacheable;class Article extends Model
{
use QueryCacheable;
protected $cacheFor = 180; // 3 minutes
}

Whenever a query will be triggered, the cache will intervene and in case the cache is empty for that query, it will store it and next time will retrieve it from the caching database; in case it exists, it will retrieve it and serve it, without hitting the database.

// database hit; the query result was stored in the cache
Article::latest()->get();
// database was not hit; the query result was retrieved from the cache
Article::latest()->get();

If you simply want to avoid hitting the cache, you can use the ->dontCache() before hitting the final method.

Article::latest()->dontCache()->firstOrFail();

The alternative is to enable cache query-by-query if caching by default doesn’t sound like a good option to you.

First of all, remove the $cacheFor variable from your model.

On each query, you can call ->cacheFor(...) to specify that you want to cache that query.

Article::cacheFor(now()->addHours(24))->paginate(15);

Some cache storages, like Redis or Memcached, come up with the support of tagging your keys. This is useful because we can tag our queries in-cache and invalidate the needed cache whenever we want to.

As a simple example, this can be useful if we want to invalidate the articles list cache when one article gets updated.

$articles = Article::cacheFor(60)->cacheTags(['latest:articles'])->latest()->get();$article = Article::find($id);
$article->update(['title' => 'My new title']);
Article::flushQueryCache(['latest:articles']);

The method flushQueryCache invalidates only the caches that were tagged with latest:articles. If some other queries would have been tagged with tags other than latest:articles, they would be kept in the cache.

For more about this package, check out the project’s page on GitHub.

The Startup

Medium's largest active publication, followed by +755K people. Follow to join our community.

Alex Renoki

Written by

23 yo minimalist maker & laravel dev. founder of @safemenudotorg

The Startup

Medium's largest active publication, followed by +755K people. Follow to join our community.

Alex Renoki

Written by

23 yo minimalist maker & laravel dev. founder of @safemenudotorg

The Startup

Medium's largest active publication, followed by +755K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store