Home > OS >  how to forget multiple cache keys with laravel 9 with default cache driver
how to forget multiple cache keys with laravel 9 with default cache driver

Time:07-29

I have this controller with many functions and cache keys depending on the username....

I need when updating the information and delete all cache keys for a specific user? is there any way to do that?

my controller to get data from caches:

class HomeController extends Controller
{
    private $cache_duration = 60*60*72;

    function home($uname) {
        $info =  Cache::remember('get-all-info-home'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->about : null;
        });
        return view('home', compact('info'));
    }


    function info($uname) {

        $info =  Cache::remember('get-all-info'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->about : null;
        });
        return view('info', compact(['info']));
    }

    function skills($uname) {
        $info =  Cache::remember('get-all-user-skills'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->about : null;
        });
        $skills =   Cache::remember('get-all-skills'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->skills : null;
        });

        return view('skills', compact(['skills', 'info']));
    }

I have tried this way, but still not working:

$uname = $info->user->name;
  $keys = ['get-all-info-home', 'get-all-info', 'get-all-user-skills',
             'get-all-skills', 'get-all-user-users-education-experiences', 'get-all-education',
             'get-all-experiences', 'get-all-user-users-user-achievements', 'get-all-user-users-achievements',
            'get-all-user-users-user-services', 'get-all-user-users-services-unique',
             'get-all-user-users-user-services'];
             foreach($keys as $key) {
                $key = $key.$uname;
                Cache::forget($key);
             }

CodePudding user response:

Similar to what lagbox mentioned, change to memcache or redis driver that supports tags, and add

Cache::

to

Cache::tags('user.....

in your controller method:

    function home($uname) {
        $info =  Cache::tags('user'.$uname)
            ->remember('get-all-info-home'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->about : null;
        });
        return view('home', compact('info'));
    }

    function info($uname) {

        $info =  Cache::tags('user'.$uname)
->remember('get-all-info'.$uname, $this->cache_duration, function() use($uname){
            $user = User::where('name', '=', $uname)->firstOrFail();
            return $user !=null ? $user->about : null;
        });
        return view('info', compact(['info']));
    }
then for the cache tag clear you would do:
Cache::tags('user'.$info->user->name)->flush()

if adding tagging is not not feasible for any reason, try checking and making sure that the $key.uname is actually returning the data you are expecting

            foreach($keys as $key) {
                $key = $key.$uname; // <- maybe this is not returning the right data
                dd($key);  // echo this out just to see if it is
                Cache::forget($key);
             }
  • Related