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

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代理失效,但当前方法在独立类中。想请教:

  1. 哪些情况会导致Spring不使用已配置的@Cacheable缓存?
  2. 还有哪些场景会意外破坏Spring AOP代理机制?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 11:45:31