事件侦听器缓存清除



有人可以帮助我如何使用laravel事件监听器清除我的缓存,因为我计划在登录后注销后清除我的缓存。有人能给我讲讲这个吗?非常感谢。

这样的内容应该涵盖侦听和清除缓存:

<?php
namespace AppListeners;
// You would need to define this event..
use AppEventsLogout;
class LogoutListener
{
public function handle(Logout $event)
{
// forget a particular key in cache, eg user (this will vary on what key you used to store it)
Cache::forget('user_' . auth()->id());
// or... remove everything in cache, but i would lean towards just removing what you need, like above
Cache::flush();
}
}

laravel有基于标签的缓存https://laravel.com/docs/8.x/cache#cache-tags,但它需要基于数组的缓存,基于文件的缓存不支持。你可以给每个用户缓存键添加用户id标签。

Cache::tags(['user_id_xx])->put('key', 'value', 100);

那么清晰,

Cache::tags(['user_id_xx'])->flush();

最新更新