springBoot访问日志记录,自定义注解清除redis缓存

springBoot访问日志记录,自定义注解清除redis缓存

annotation获取访问日志记录

使用annotation获取访问日志本质是没有什么难点,重点是获取请求和响应对象.代码如下
SaveLogAnnotation

1
2
3
4
5
6
7
8
/**
* Created by xiehui1956(@)gmail.com on 17-7-19.
*/
@Component
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SaveLogAnnotation {
}

SaveLogAspect,这里我使用的是前置通知。如果要获取响应结果的话可以使用后置或者环绕通知,大同小异

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Created by xiehui1956(@)gmail.com on 17-7-19.
*/
@Aspect
@Component
public class SaveLogAspect {
@Pointcut("@annotation(com.chinaredstar.authority.common.annotation.SaveLogAnnotation))")
public void saveLog() {
}
@Before("saveLog()")
public void before() throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
request.getParameterMap().entrySet().forEach(entry -> {
for (int i = entry.getValue().length - 1; i >= 0; i--) {
System.out.println(entry.getKey() + "; " + entry.getValue()[i]);
}
});
System.out.println("this is test aop");
}
}

自定义annotation清除redis缓存数据

业务场景是最近在作用权限管理系统,为了提高效率将业务数据放置到了redis缓存中了。因为系统做了web/mng分离,所以在后台更新缓存数据的时候需要清空缓存中的数据。
我的这个系统业务redis不做强依赖,当然这么做也是有风险的如果有人恶意刷不存在的key压力还是会落到redis中。如果对redis做强依赖完全信任,redis服务挂了那就尴尬了。
这里我选择前者。代码如下:

标记注解:CleanRedisAnnotation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 清除缓存注解
* Created by xiehui1956(@)gmail.com on 17-9-11-上午11:14.
*/
@Component
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CleanRedisAnnotation {
/**
* 缓存的key
* 默认key为rk
*
* @return
*/
String redisKey() default "rk";
}

清除逻辑:CleanRedisAspect
这里清除redis缓存逻辑之写到了获取key,有了key清除redis就太简单了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 清除redis缓存
* Created by xiehui1956(@)gmail.com on 17-9-11-下午12:49.
*/
@Aspect
@Component
public class CleanRedisAspect {
@Pointcut("@annotation(com.chinaredstar.authority.common.annotation.CleanRedisAnnotation))")
public void cleanRedis() {
}
@After("cleanRedis() && @annotation(redisAnnotation)")
public void after(CleanRedisAnnotation redisAnnotation) throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String key = redisAnnotation.redisKey();
List<String> redisKeys = (List<String>) request.getAttribute(key);
for (String redisKey : redisKeys) {
if (StringUtils.isBlank(redisKey))
throw new SerialException("清除缓存的时候报错了,redisKey为空");
System.out.println(redisKey);
}
}
}

使用实例:TestController

1
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);
}

以上。