本文描述了如何使用Spring AOP来实现无侵入式的增强代码,为接口添加缓存功能。
采用Cache-Aside模式,当查询缓存命中时,直接返回查询结果;
当查询没有命中时,查询数据库并将数据写入缓存(附带过期时间),再返回查询结果;
当资源被更新时,先更新数据库,再删除缓存记录。
fastjson
来进行序列化和反序列化。implementation('com.alibaba:fastjson:1.2.46')
implementation('mysql:mysql-connector-java:5.1.21')
implementation('org.springframework.boot:spring-boot-starter-aop')
implementation('org.springframework.boot:spring-boot-starter-data-redis')
implementation('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=12345678
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RedisCache {
String value();
}
@Aspect
@Component
public class RedisCacheAspect {
private final int expireTime = 100;
@Autowired
private StringRedisTemplate redisTemplate;
@Pointcut("@annotation(com.rainlf.cache.service.RedisCache)")
private void pointcut() {}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
Object result;
MethodSignature methodSignature = ((MethodSignature) joinPoint.getSignature());
RedisCache annotation = AnnotationUtils.getAnnotation(methodSignature.getMethod(), RedisCache.class);
// 读资源
String key = annotation.value();
if (!"".endsWith(key)) {
String objStr = redisTemplate.opsForValue().get(key);
if (objStr != null) {
// 命中
result = JSON.parseObject(objStr, methodSignature.getReturnType());
} else {
// 未命中
result = joinPoint.proceed();
redisTemplate.opsForValue().set(key, JSON.toJSONString(result), expireTime, TimeUnit.SECONDS);
}
return result;
}
// 写资源
String[] deleteList = annotation.deleteList();
if (deleteList.length > 0) {
result = joinPoint.proceed();
redisTemplate.delete(Arrays.asList(deleteList));
return result;
}
// 其他情况,不做任何功能增强
result = joinPoint.proceed();
return result;
}
}
}
// 写操作
@RedisCache(deleteList = {"skyline", "skyline2"})
@PostMapping("/user")
void addUserInfo(@RequestBody User user) {
cacheService.addUserInfo(user);
}
// 读操作
@RedisCache("skyline")
@GetMapping("/user/id/{id}")
User getUserInfo(@PathVariable("id") Integer id) {
return cacheService.getUserInfoById(id);
}
在应用和MySQL、Redis进行连接后。单次查询平均统计如下:
可以看出性能有明显的提升。