Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Using simple yet effective caching for Laravel queries

You know it’s always a good idea to have something at your disposal using which you can improve the performance of your applications. When it comes to web applications, one of the many things that power them is database queries and that’s why it’s no surprise that you may want to optimize those and make your application a little faster!

One of the ways using which you can “dramatically” improve the performance of your database queries is by caching them somewhere for some time. It turns out, if your application is powered by Laravel, you can do this pretty easily.

Setting up cache driver

For this, all we need to use is one of the cache drivers that Laravel supports. For simplicity, we can use the database cache driver.

For this to work, we’ll need to create a cache table. You can easily create this table in Laravel using the following Artisan command.

$ php artisan cache:table

This will generate a table with the following columns: key, value & expiration.

Laravel will use this table to create a “cache” in the form of key:value pairs which can store database results as well.

Using cache to store query results

Now, we can start using this cache by using Illuminate\Support\Facades\Cache facade anywhere you want. For instance, if we want to store the result of the following query into the cache for the next 10 minutes…

$books = Book::orderBy('title')->get();

…we can do it using the Cache::remember method like so.

$expire = Carbon::now()->addMinutes(10);

$books = Cache::remember('books', $expire, function() {
    return Book::orderBy('title')->get();
});

It’s as simple as it gets! Now, for the next 10 minutes, Laravel will fetch the result from the books key of the cache instead of querying the database. And this can especially improve the performance of your application when you’re performing complex queries to fetch results.

There’s a rememberForever method as well which can retrieve an item from the cache or store it forever but you probably shouldn’t use it unless the app you’re building doesn’t get updated in a timely manner.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?