0

i want to use a cache of laravel for ex: index method on a specific controllers! i use Cache::rememberForever method of laravel cache.

i dont use Cache::remember with ttl time for caching data!

my question: i dont no when and how i update data in cache

imaging: i cached user profile with all relations! now user change avatar or personal data! now i should be renew (update) cache data in redis! (update cache data for get in next call) i want to know the best solution for updating cache data when update main data

Rasoul Karimi
  • 49
  • 1
  • 6

1 Answers1

2

To update a cache you can use such. event function in your User model:

protected static function boot()
{
    parent::boot();

    $removeCacheFunc = function ($model) {
        $key = self::USER_CACHE_KEY . $model->id;  //compile cache key in your way
        \Cache::delete($key);
    };
    static::saved($removeCacheFunc);
    static::deleting($removeCacheFunc);
}

Next time you call Cache::rememberForever() will not find this entity by key and will make it on the fly

Vasyl Zhuryk
  • 1,228
  • 10
  • 23