【已解决】redisCache注解失效,没写cacheConfig

环境配置

复制代码
jdk11    springboot 2.6.13    

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>3.2.0</version>
</dependency>

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
      <version>3.2.0</version>
</dependency>

  redis:
    host: 192.168.2.129
    port: 6379
    username: default
    password: 'linux02'
    database: 1
#    默认使用lettuce
    lettuce:
      pool:
#        最大连接数,最大空闲数,最小空闲数
        max-active: 5
        max-idle: 2
        min-idle: 2
  #    缓存10min,允许缓存null值防止缓存穿透
  cache:
    type: redis
    redis:
      time-to-live: 600
      cache-null-values: true

代码

java 复制代码
  @Cacheable(cacheNames = BLOG_CACHE_PREFIX + "otherPassages", key = "#userId")
  @Override
  public List<PassageTitleVO> getOtherPassagesByUserId(Long userId) {
       ....
        ....
    return passageTitleVOS;
  }

解决

网上找了一些案例,有的不需要写cacheConfig,有些需要写,我之前就用过cacheable的注解,当时就是上面的配置,没写配置类也有效果,这次我最开始就没写,然后Cacheable注解就没效果了,最后写了个cacheConfig才解决。

java 复制代码
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

  @Bean
  public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    return RedisCacheManager.builder(redisConnectionFactory).build();
  }

}

然后又发现yml设置的过期时间没有生效,存到redis的是永不过期,又在 cacheConfig配置了过期时间,600s

java 复制代码
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

  @Bean
  public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
        .entryTtl(Duration.ofSeconds(600));
    return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(redisCacheConfiguration)
        .build();
  }

}
相关推荐
wyhwust6 小时前
web应用技术--springboot01
java·开发语言
lulu12165440786 小时前
GPT-5.6 vs Claude Fable 5/Mythos 深度技术对比:kindle/kepler/Levi三版本实测全解析
java·人工智能·python·gpt
焦虑的说说6 小时前
mysql深分页性能瓶颈根源分析
数据库·mysql
想你依然心痛6 小时前
数据库技术在电力业务中的核心应用场景
java·开发语言·数据库
nice_lcj5206 小时前
排序(3)-第三篇:交换排序专题——从冒泡排序到快速排序的效率飞跃
java·数据结构·算法·排序算法
搬石头的马农6 小时前
御三家旗舰模型混战下的企业选型策略:GPT-5.6、Fable 5、Gemini 3.5 Pro 怎么选? - 微元算力(weytoken)
java·人工智能·python·gpt·ai编程
weixin_523185326 小时前
达梦数据库事务机制踩坑:默认不自动提交事务
数据库·oracle
小陈phd6 小时前
Qdrant 向量数据库从入门到实战:构建高效语义检索系统
数据库
xingyuzhisuan6 小时前
Redis 多级缓存落地聚合 API:重复请求降本 70% 实战数据
数据库·redis·缓存·ai
专注于大数据技术栈6 小时前
什么是Trino?大数据统一联邦查询引擎详解
大数据·数据库