springBoot访问日志记录,自定义注解清除redis缓存
annotation获取访问日志记录
使用annotation获取访问日志本质是没有什么难点,重点是获取请求和响应对象.代码如下
SaveLogAnnotation
1 | /** |
SaveLogAspect,这里我使用的是前置通知。如果要获取响应结果的话可以使用后置或者环绕通知,大同小异
1 | /** |
自定义annotation清除redis缓存数据
业务场景是最近在作用权限管理系统,为了提高效率将业务数据放置到了redis缓存中了。因为系统做了web/mng分离,所以在后台更新缓存数据的时候需要清空缓存中的数据。
我的这个系统业务redis不做强依赖,当然这么做也是有风险的如果有人恶意刷不存在的key压力还是会落到redis中。如果对redis做强依赖完全信任,redis服务挂了那就尴尬了。
这里我选择前者。代码如下:
标记注解:CleanRedisAnnotation
1 | /** |
清除逻辑:CleanRedisAspect
这里清除redis缓存逻辑之写到了获取key,有了key清除redis就太简单了。
1 | /** |
使用实例:TestController1
2
3
4
5
6
7
8@CleanRedisAnnotation
@RequestMapping(value = "/get", method = RequestMethod.GET)
public CommonResult get(CommonResult commonResult, HttpServletRequest request) {
String userResourceKey = RedisKeyHelper.getUserResource(1L);
request.setAttribute("rk", ImmutableList.of(userResourceKey));
List<AuthResourcesEntity> list = redisDao.getList(userResourceKey);
return commonResult.setData(list);
}
以上。