How to implement Redis Cache in Laravel 8.x

tutspack laravel
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello Artisans,

How are you doing? In this tutorial, I’ll show you how to implement Redis Cache in Laravel 8.x . If you don’t know how to implement cache in laravel application, dont’t worry I will show you everything step by step. so here we go.

How to implement Redis Cache in Laravel 8.x

First of all, you have to what is cache? and why do you use cache in your application? Caching is the act of transparently storing data for future use in an attempt to make applications run faster.

Well, I assume You have a fresh installed laravel project which has a product Model. In this product’s table, you have more than 2000+ data. You may think that you’ve to insert 2000+ data manually if you think that you’re absolutely wrong. You can use factory to insert fake data into your database. I have a blog where I told about the factory.

Read More : How to generate fake data in Laravel using factory and faker

First you have to set up config/cache.php file.

 'default' => env('CACHE_DRIVER', 'redis'),

Your cache driver should be redis.

Note : You have to installed redis form this site https://redis.io/

How to implement Redis Cache in Laravel 8.x

Well, I assume you’ve generated 2000+ data using factory. You can check by tinker how much data you have inserted.

 implement-Redis-Cache
Check number of inserted data

Well, Now you have to make a controller. Run this command in your terminal.

php artisan make:controller ProductController

App\Http\Controllers\ProductController.php

public function index()
    {
        $products = cache('products', function () {
            return Product::with('category')->get();
        });

        return view('product.product',[
            'products' => $products
        ]);
    }

here you can see, first we have stored data in cache then return a view. Well, Now arise a question when you insert or update or delete a product how to you change cache data. For changing data we will use laravel model event feature.

We know laravel Eloquent models dispatch several events, allowing you to hook into the following moments in a model’s lifecycle: retrievedcreatingcreatedupdatingupdatedsavingsaveddeletingdeletedrestoringrestored, and replicating.

In this caching system, we will use [created,updated,deleted] event. so make three events, Run those commands in your terminal.

php artisan make:event ProductCreated
php artisan make:event ProductEdited
php artisan make:event ProductDeleted

Now you have to make a listener for those events. Run this command in your terminal.

php artisan make:listener ProductListener

Now you have to register event and listener in EventServiceProvider .

App\Providers\EvenserviceProvider

Inside the listener array you have to register like this.

 protected $listen = [
        ProductCreated::class => [
            ProductListener::class
        ],
       ProductEdited::class => [
            ProductListener::class
        ],
       ProductDeleted::class => [
            ProductListener::class
        ],
    ];

Now you have to add a protected array in product Model.

App\Models\Product

protected $dispatchesEvents = [
        'created' => ProductCreated::class,
        'updated' => ProductCreated::class,
        'deleted' => ProductCreated::class
    ];

Well, Our all step has done. Now run php artisan serve. Your Product data come from cache instead of the database.

Hope It will help you.


Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

One Comment on “How to implement Redis Cache in Laravel 8.x”

Leave a Reply

Your email address will not be published. Required fields are marked *