You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3 Redis缓存前缀与TTL配置不生效问题求助

问题根源

你配置的缓存前缀和TTL没生效,核心原因是自定义的RedisCacheConfiguration没有被关联到RedisCacheManager中——当前用RedisCacheManager.create(connectionFactory)创建的缓存管理器,用的是Spring默认配置,根本没启用你自定义的那套规则。

修复步骤

修改cacheManager Bean的创建逻辑,把自定义的缓存配置绑定进去:

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory, RedisCacheConfiguration cacheConfiguration) {
    return RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(cacheConfiguration) // 将自定义配置设为全局默认
            .build();
}
补充说明
  • 如果需要给特定缓存(比如你的profile缓存)单独设置差异化规则,可以通过withCacheConfiguration扩展:
    return RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(cacheConfiguration) // 全局默认配置
            .withCacheConfiguration("profile", 
                RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofMinutes(5)) // 单独给profile缓存设5分钟过期
                    .prefixCacheNameWith("profile-custom-"))
            .build();
    
  • 确认RedissonConnectionFactory配置正常,确保Redis连接无异常,避免因连接问题导致配置不生效。
  • 检查项目中是否存在其他缓存配置类,防止Spring Boot自动配置或其他自定义配置覆盖当前规则。

内容的提问来源于stack exchange,提问作者Hoang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 04:30:19