SpringBoot @Cacheable注解缓存失效问题排查求助
SpringBoot @Cacheable缓存失效排查
我正在给SpringBoot应用添加缓存功能,最初使用REDIS作为缓存提供者未达预期,换成SpringBoot默认缓存后仍失效。已在独立测试应用中成功启用缓存,但当前服务的缓存功能被未知因素阻止。
要缓存的方法接收URI字符串参数,从AWS获取字符串类型的密钥,逻辑简单。已做如下排查:
- 确认传入URI和返回结果固定不变,仍未缓存
- 验证导入的是Spring相关正确包
- @Cacheable标记的方法位于独立服务类
- 应用已添加@EnableCaching注解
- 尝试过独立缓存配置类及将@EnableCaching移至启动类,均无效
启动类代码如下:
@SpringBootApplication @EnableCaching @EnableAsync public class MdhApplication { public static void main(String[] args) { SpringApplication.run(MdhApplication.class, args); } }
开启org.springframework.cache包TRACE日志后,启动时能看到@Cacheable方法被正确加载:
Adding cacheable method 'getSecret' with attribute: [Builder[String org.mdh.service.AwsSecretsManager.getSecret(String)] caches=[graphSecret] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false']
但调用该方法时,无任何缓存检查、命中/未命中的日志输出,而测试应用中能看到如下日志:
TRACE 28148 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : Computed cache key '4' for operation Builder TRACE 28148 --- [nio-8080-exec-1] o.s.cache.interceptor.CacheInterceptor : No cache entry for key '4' in cache(s) [student] TRACE 28148 --- [nio-8080-exec-2] o.s.cache.interceptor.CacheInterceptor : Cache entry for key '4' found in cache 'student'
已知同Bean内调用会导致AOP代理失效,但当前方法在独立类中。想请教:
- 哪些情况会导致Spring不使用已配置的@Cacheable缓存?
- 还有哪些场景会意外破坏Spring AOP代理机制?
内容的提问来源于stack exchange,提问作者Michael M
相关产品推荐
相关产品推荐

