Spring Boot 使用AOP+Redis搭建缓存

摘要

本文描述了如何使用Spring AOP来实现无侵入式的增强代码,为接口添加缓存功能。

策略

采用Cache-Aside模式,当查询缓存命中时,直接返回查询结果;
当查询没有命中时,查询数据库并将数据写入缓存(附带过期时间),再返回查询结果;
当资源被更新时,先更新数据库,再删除缓存记录。


备注

  1. 进行对象缓存时,使用 fastjson 来进行序列化和反序列化。
  2. 用户应自己维护api和key的映射关系。
  3. 实际使用时,key应和具体参数相关,可在切面里做具体绑定。

代码实现

  1. 依赖
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')
  1. 配置文件
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
  1. 缓存注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RedisCache {
     String value();
}
  1. 缓存切面
@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;
        }
    }
}
  1. 应用
    // 写操作
    @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进行连接后。单次查询平均统计如下:

  • 未命中时查询时间约:9ms
  • 命中时查询时间约:2ms

可以看出性能有明显的提升。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,167评论 2 89
  • 1.确认是否需要缓存 在使用缓存之前,需要确认你的项目是否真的需要缓存。使用缓存会引入的一定的技术复杂度,后文也将...
    Java黎先生阅读 12,350评论 2 22
  • 五种数据结构简介 Redis是使用C编写的,内部实现了一个struct结构体redisObject对象,通过结构体...
    彦帧阅读 11,881评论 0 14
  • 摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『产品...
    子木聊出海阅读 5,496评论 5 50
  • 有一个人,他从小到大都是一名失败者,他感到上天的不公平 ,于是,他决定去寻找上帝,询问成功是什么。 这人来到河边,...
    应果阅读 3,192评论 0 0

友情链接更多精彩内容