How to send your mysql server to holidays by caching all Eloquent queries in Laravel

How to send your mysql server to holidays by caching all Eloquent queries in Laravel

  • Model caching in Larvel

  • Performance booster using model caching in Laravel 8

  • How to make a laravel app load faster

Dec 01,2022

Hiting mysql db everytime a user requests a page can be cumbersome for the server and make it slow down quickly. If your server slows down it will impact negatively your traffic and if you are hosted on a shared hosting package you can be temporaly suspended because your website is slowing down the entire server.

This said, you need reduce the number of connection your website does to the database server in order to keep it healthy and fast to serve all users that are connected to it.

Laravel, one of most popular framework, is used to develop different kind of apps from blogs to complex company applications. 

It is shepped with data Caching option but this one is not powerfull enough because it does not support tags('A way to segment data in order to unchache them as needed without deleting the entire cache'). Moreover it does't allow to cache all queries made to a model automatically.

This is why you need to enhance it in order to optimize your application to support more traffic but also in order to keep your code clean.

I know how you may feel frustrated when your website slows down or is being suspended by the hosting provider. I have been struggling with this some months ago until I found this solution. And after I saw that it works for me, I decided to share it with you. Enought talking, Let's dive into this:

Install Laravel-eloquent-query-cache package

Open command promt and switch to your app folder. Then run the following command:

composer require rennokki/laravel-eloquent-query-cache

 

Preparing your models:

Each model you want to cache on should use the Rennokki\QueryCache\Traits\QueryCacheable trait.

Suppose that you want to cache the Post model. Open it and modify it to look as follows(Code in bold):

use Rennokki\QueryCache\Traits\QueryCacheable;

class Podcast extends Model

{

    use QueryCacheable;

}

After this your model is ready to cache queries. Let's know cache all queries made to Post Model automatically.

 

Query Caching:

 

use Rennokki\QueryCache\Traits\QueryCacheable;

class Post extends Model

{

    use QueryCacheable;

    /**

     * Specify the amount of time to cache queries.

     * Do not specify or set it to null to disable caching.

     *

     * @var int|\DateTime

     */

    public $cacheFor = 3600;/**Cache each query for one hour**/

    /**

     * The tags for the query cache. Can be useful

     * if flushing cache for specific tags only.

     *

     * @var null|array

     */

    public $cacheTags = ['posts'];

    /**

     * A cache prefix string that will be prefixed

     * on each cache key generation.

     *

     * @var string

     */

    public $cachePrefix = 'posts_';/**Optional*/

}

Now all queries to Post model are cached automatically for one hour.

 

Cache queries by Tags

Some caching stores accept tags. This is really useful if you plan on tagging your cached queries and invalidate only some of the queries when needed.

In fact, this is the recommended way of caching your queries because it will be much easier to invalidate them later, even in such complex situations where, for example, you will be handling a lot of queries, like queries for listing, retrieving by ID and eventually invalidating the listing cache upon a new record, updated record or deleted record.

The following example states queries that retrieve posts based on their self_id. The books from shelf 1 will be invalidated, and the books from shelf 2 will be not.

 

$shelfOneBooks = Book::where('shelf_id', 1)

    ->cacheTags(['shelf:1'])

    ->get();

 

$shelfTwoBooks = Book::where('shelf_id', 2)

    ->cacheTags(['shelf:2'])

    ->get();

 

Cache invalidation

You may need this after you create a new post, update it, delete it or publish it.

Flushing or invalidating is done by calling flushQueryCache with the respective tags.

You can delete the cache by one tag or by multiple tags at once:

Book::flushQueryCache(['shelf:1']);/*Deletes only the cache with tag: shelf:1*/

Book::flushQueryCache(['shelf:1', 'shelf:2']);/**Deletes both shelf:1 and shelf:2*/

The difference between Tags and SQL Hash Key

Please be careful that the same tags do not mean the same cached key. In the following example, we might have a users table and retrieve them by their name. 

Even when specifying the same tag, the caching will be done according to the SQL query (which is different), but we can invalidate both caches if needed later:

$alice = User::whereName('Alice')

    ->cacheTags(['users'])

    ->first();

$bob = User::whereName('Bob')

    ->cacheTags(['users'])

    ->first();

 

Book::flushQueryCache(['users']);/**Even if the two lines above represent 2 diferent queries, this last line will invalidate both of them.*/

Thanks to Renoki: For more info please visit RENOKI webiste