Laravel Model Caching – Performance Boost

How to reduce 1002 MySQL queries in one page to 0 (zero) queries? In this tutorial, I will show how to use laravel model caching. This great library allows to fully cache entire eloquent objects and reduce queries to 0. Self-invalidating feature allows to regenerate data from database on model save, update, create etc..

Where to use model caching?

If You have menu on each page, complex form with many drop-downs populated from database, lot data loaded in background (Language, Date Format, Time Format , Dictionaries etc) Cache it.

1. Create new application

Create a new application with composer. (Note: I have composer installed on Ubuntu, if you are using windows download composer.phar)

cd into project and install DebugBar and laravel-model-caching.

2. Installing required libs

Use artisan composer command to download libs. Don’t forget to publish vendors at the end.

3. Creating models

I will create 2 models Company and City. Company will have City as belongTo relation.

4. Creating Migrations

We also need to create migrations for database tables.

5. Creating Seeders

We need some data to run tests

Update DatabaseSeeder.php and add our newly created seeder to default Laravel seeder.

6. Controller, Views, and Routing

Create new controller, update view and route.

 

Yes, ugly AF this should be done with(‘city’) eager-loading but this example shows how to use model cache.

Update Route

Update view

7. Testing

Now what will happen when You will load localhost:8000 in the browser ?

What is happening? Because Laravel uses “lazy load” approach page is generating 1002 queries. (Yes, yes You can use “eager load” as I described in the previous article about performance but here I will show what we can do with caching).

8. Updating model

What I did. I added Cachable trait – with this all data from database will be stored in cache. Now if You will run again Your web app You will see 0 database queries.

Now when you will run localhost:8000 again.

Quite amazing, right?

9. Summary

  • I use this library for all my Laravel project where I need to get data from database and those data are mostly static. For example all dictionaries (Date Format, Time Format, Countries etc).
  • This library is self-invalidating models this means its refreshing cache when you will create, update, delete record via eloquent.
  • This library requires PHP 7.1.*
  • It’s better to use Redis in production.

 

I love this Lib – The End.

Write in a comment what you think about that. If you liked this tutorial You can share it. Have fun coding.

Images source:

Freepik

Git:

https://github.com/GeneaLabs/laravel-model-caching

Laravel-BAP Modular Backend Application Platform + Example CRM with 17 modules

Join Newsletter - Together we will rule the galaxy

Related Post

4 Comments
  • Edd says:

    +1 I want to learn this kind of stuff! I Will try this.

    March 17, 2018 at 6:48 am
  • SGC-Fireball says:

    Company::query()->with(‘city’)->all();
    Two queries without Cache!

    March 20, 2018 at 11:35 am
    • laravel-bap says:

      Yes. If You will use with.

      March 20, 2018 at 12:41 pm
  • Iman says:

    Yet another package which helps you in caching not your database queries but your page partials giving the most performance possible.

    https://github.com/imanghafoori1/laravel-widgetize

    June 24, 2018 at 6:17 pm
Leave A Comment
*
*

Share159
Tweet
Pin1
+1
Share
160 Shares
%d bloggers like this: