Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sarim_Shahid's avatar

Cache Mechanism for Real-Time Updates

Hi everyone, I am working on an e-commerce store and want to load queries faster. Is it possible to set up caching in such a way that it automatically updates query whenever a model undergoes any CRUD action?

0 likes
6 replies
LaryAI's avatar
Level 58

To implement a caching mechanism that automatically updates when a model undergoes any CRUD action, you can use Laravel's model events in combination with a caching library like Laravel Cache. Here's a step-by-step solution:

  1. Choose a Cache Store: Ensure you have a cache store configured in your config/cache.php file. Common options include file, database, redis, or memcached.

  2. Cache the Query Results: Use Laravel's cache facade to cache the results of your queries. For example, you can cache a list of products like this:

    use Illuminate\Support\Facades\Cache;
    use App\Models\Product;
    
    $products = Cache::remember('products', 60, function () {
        return Product::all();
    });
    

    This caches the products for 60 minutes.

  3. Listen to Model Events: Use model events to clear the cache whenever a CRUD operation occurs. You can do this by defining event listeners in your model's boot method.

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Cache;
    
    class Product extends Model
    {
        protected static function boot()
        {
            parent::boot();
    
            static::saved(function ($product) {
                Cache::forget('products');
            });
    
            static::deleted(function ($product) {
                Cache::forget('products');
            });
        }
    }
    

    Here, saved and deleted events are used to clear the cache whenever a product is saved or deleted.

  4. Considerations for Real-Time Updates: If you need real-time updates, consider using broadcasting with Laravel Echo and Pusher or a similar service to notify clients of changes. This way, you can update the UI without needing to refresh the page.

  5. Testing: Ensure you test the caching mechanism thoroughly to confirm that the cache is being cleared and updated as expected when CRUD operations occur.

By following these steps, you can set up a caching mechanism that automatically updates when a model undergoes any CRUD action, improving the performance of your e-commerce store.

martinbean's avatar

@sarim_shahid You should only cache things that are called frequently, or are “expensive” (i.e. take a long time or a lot of resources) to execute. You don’t cache just to try and make things magically go faster.

2 likes
Sarim_Shahid's avatar

Hey @martinbean, thanks for the explanation! I was trying to achieve exactly what you mentioned—'making things magically go faster'—because I heard that caching queries significantly improves data loading speed. However, after some research, I’ve realized that caching works best for static data. I’d love to hear your take on this!

martinbean's avatar

@Sarim_Shahid Like I say, caching is meant for things that are slow or intensive to retrieve/calculate each time.

Just caching everything that makes a database query isn’t a magic bullet and can have adverse effects if you employ it indiscriminately.

Snapey's avatar

caching would be useful for something like a list of top 10 products that are the same on every page, and even more so if the data is the same for every user.

caching would be less useful for a shopping cart for instance since it is personal for the user and would need to be cleared every time the basket is changed.

Install Laravel Debugbar and look for frequent and lengthy queries, then tackle then first.

2 likes
Mahmoud04's avatar

Hi @sarim_shahid , this code will help you but I still agree with @snapey that the cache is for places that will not change much, such as categories or others.

code:

example:

any model import: CacheableTrait

in Controller:

Please or to participate in this conversation.