Redis 缓存

来源:https://cloud.iocoder.cn/redis-cache/

yudao-spring-boot-starter-redis 技术组件,使用 Redis 实现缓存的功能,它有 2 种使用方式:

1. 编程式缓存

友情提示:

如果你未学习过 Spring Data Redis 框架,可以后续阅读 《芋道 Spring Boot Redis 入门》 文章。

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
</dependency>

由于 Redisson 提供了分布式锁、队列、限流等特性,所以使用它作为 Spring Data Redis 的客户端。

1.1 Spring Data Redis 配置

① 在 application-local.yaml 配置文件中,通过 spring.redis 配置项,设置 Redis 的配置。如下图所示:

Spring Data Redis 配置

② 在 YudaoRedisAutoConfiguration 配置类,设置使用 JSON 序列化 value 值。如下图所示:

YudaoRedisAutoConfiguration 配置类

1.2 实战案例

以访问令牌 Access Token 的缓存来举例子,讲解项目中是如何使用 Spring Data Redis 框架的。

Access Token 示例

1.2.1 引入依赖

yudao-module-system-server 模块中,引入 yudao-spring-boot-starter-redis 技术组件。如下所示:

<dependency>
    <groupId>cn.iocoder.cloud</groupId>
    <artifactId>yudao-spring-boot-starter-redis</artifactId>
</dependency>

1.2.2 OAuth2AccessTokenDO

新建 OAuth2AccessTokenDO 类,访问令牌 Access Token 类。代码如下:

图片纠错:最新版本将 yudao-module-system-biz 子模块,重命名为 yudao-module-system-server 子模块,更好表达它是一个服务

OAuth2AccessTokenDO 类

友情提示:

1.2.3 RedisKeyConstants

为什么要定义 Redis Key 常量?

每个 yudao-module-xxx 模块,都有一个 RedisKeyConstants 类,定义该模块的 Redis Key 的信息。目的是,避免 Redis Key 散落在 Service 业务代码中,像对待数据库的表一样,对待每个 Redis Key。通过这样的方式,如果我们想要了解一个模块的 Redis 的使用情况,只需要查看 RedisKeyConstants 类即可。

yudao-module-system 模块的 RedisKeyConstants 类中,新建 OAuth2AccessTokenDO 对应的 Redis Key 定义 OAUTH2_ACCESS_TOKEN。如下图所示:

图片纠错:最新版本将 yudao-module-system-biz 子模块,重命名为 yudao-module-system-server 子模块,更好表达它是一个服务

RedisKeyConstants 类

1.2.4 OAuth2AccessTokenRedisDAO

新建 OAuth2AccessTokenRedisDAO 类,是 OAuth2AccessTokenDO 的 RedisDAO 实现。代码如下:

图片纠错:最新版本将 yudao-module-system-biz 子模块,重命名为 yudao-module-system-server 子模块,更好表达它是一个服务

OAuth2AccessTokenRedisDAO 类

1.2.5 OAuth2TokenServiceImpl

OAuth2TokenServiceImpl 中,只要注入 OAuth2AccessTokenRedisDAO Bean,非常简洁干净的进行 OAuth2AccessTokenDO 的缓存操作,无需关心具体的实现。代码如下:

OAuth2TokenServiceImpl 类

2. 声明式缓存

友情提示:

如果你未学习过 Spring Cache 框架,可以后续阅读 《芋道 Spring Boot Cache 入门》 文章。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

相比来说 Spring Data Redis 编程式缓存,Spring Cache 声明式缓存的使用更加便利,一个 @Cacheable 注解即可实现缓存的功能。示例如下:

@Cacheable(value = "users", key = "#id")
UserDO getUserById(Integer id);

2.1 Spring Cache 配置

① 在 application.yaml 配置文件中,通过 spring.redis 配置项,设置 Redis 的配置。如下图所示:

Spring Cache 配置

② 在 YudaoCacheAutoConfiguration 配置类,设置使用 JSON 序列化 value 值。如下图所示:

YudaoCacheAutoConfiguration 配置类

2.2 常见注解

2.2.1 @Cacheable 注解

@Cacheable 注解:添加在方法上,缓存方法的执行结果。执行过程如下:

2.2.2 @CachePut 注解

@CachePut 注解,添加在方法上,缓存方法的执行结果。不同于 @Cacheable 注解,它的执行过程如下:

2.2.3 @CacheEvict 注解

@CacheEvict 注解,添加在方法上,删除缓存。

不使用 allEntries 属性,但是想批量删除一些缓存,怎么办?

可参考 https://t.zsxq.com/phOrM 帖子,手动删除一些。 ::

2.3 实战案例

RoleServiceImpl 中,使用 Spring Cache 实现了 Role 角色缓存,采用【被动读】的方案。原因是:

RoleServiceImpl

友情提示:

如果你未学习过 MySQL 与 Redis 一致性的问题,可以后续阅读 《Redis 与 MySQL 双写一致性如何保证? 》 文章。

① 执行 #getRoleFromCache(...) 方法,从 MySQL 读取数据后,向 Redis 写入缓存。如下图所示:

getTestDemo 方法

② 执行 #updateRole(...)#deleteRole(...) 方法,在更新或者删除 MySQL 数据后,从 Redis 删除缓存。如下图所示:

getTestDemo 方法

补充说明:

如果你在多个项目里,使用了 Redis 想通 db 的话,可以通过 spring.cache.redis.key-prefix 解决,可见 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/998/

2.4 过期时间

Spring Cache 默认使用 spring.cache.redis.time-to-live 配置项,设置缓存的过期时间,项目默认为 1 小时。

如果你想自定义过期时间,可以在 @Cacheable 注解中的 cacheNames 属性中,添加 #{过期时间} 后缀,单位是秒。如下图所示:

过期时间

实现的原来,参考 《Spring @Cacheable 扩展支持自定义过期时间 》 文章。

3. Redis 监控

yudao-module-infraredis 模块,提供了 Redis 监控的功能。

点击 [基础设施 -> 监控中心 -> Redis 监控] 菜单,可以查看到 Redis 的基础信息、命令统计、内存信息。如下图所示:

Redis 监控