Cache Redis Binder
使用 Spring Data Redis 实现 Spring Cache 的 CacheManager。消费工程需要同时选择 loadup-components-cache-api 与本
binder,并提供可用的 RedisConnectionFactory。
引入
<dependency>
<groupId>io.github.loadup-cloud</groupId>
<artifactId>loadup-components-kv-binder-redis</artifactId>
</dependency>配置
loadup:
cache:
type: redis
binder:
redis:
key-prefix: 'loadup:cache:'
spring:
data:
redis:
host: localhost
port: 6379连接地址、认证、Sentinel/Cluster 等连接设置由 Spring Boot 的 spring.data.redis.* 和 RedisConnectionFactory 管理。
loadup.cache.binder.redis.key-prefix 仅控制缓存 key 前缀;TTL、空值和随机过期由公共的 loadup.cache.*
配置管理。内部设计见 ARCHITECTURE.md。
Spring 装配使用 loadup-components-runtime 提供的应用 JSON mapper / Clock;消费者通过参数注入,不创建默认兜底实例。独立测试需显式提供所需 Bean。
设计与实现
RedisCacheAutoConfiguration 在 loadup.cache.type=redis 且 Redis 类可用时创建 Spring Data Redis RedisCacheManager
。它复用应用现有 RedisConnectionFactory,不自行创建第二套连接配置,因此旧的 loadup.cache.redis.host/port 配置不生效。
值序列化使用基于共享 Jackson 配置的 JSON codec,key 使用字符串序列化;默认 key 前缀由
loadup.cache.binder.redis.key-prefix 控制。公共 LoadupCacheProperties 决定默认 TTL、每个 cache 的 TTL、随机过期范围与空值策略。其他
binder 由不同模块实现,应用选择一个与 loadup.cache.type 匹配的 binder。
缓存连接和可用性由消费应用负责;Redis 服务异常会按 Spring Data Redis 的异常语义向调用方传播。上层业务通过标准 Spring Cache API 使用,不依赖本模块类型。
分层与调用路径
Spring Cache 调用进入当前 CacheManager,由选中的 binder 处理缓存读写;切换后端不改变业务调用。
业务代码 → API/facade → 当前 binder → 第三方引擎或基础设施binder 实现框架契约,把实现库及其配置隔离在业务 API 之外。
装配规则
-
RedisCacheAutoConfiguration是自动配置入口。@ConditionalOnClass({RedisCacheManager.class, RedisConnectionFactory.class})@ConditionalOnProperty(prefix = "loadup.cache", name = "type", havingValue = CacheBackendType.REDIS)@ConditionalOnMissingBean(CacheManager.class)
配置归属
RedisCacheProperties绑定loadup.cache.binder.redis。
设计取舍
选择独立 binder,使 Spring Cache 的实现依赖留在集成应用;替换底层实现时,业务侧仍使用 CacheManager 契约。
集成方式与配置示例见 README.md。