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
相关产品推荐
相关产品推荐

