【已解决】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();
  }

}
相关推荐
每天都要进步哦3 分钟前
MySQL快速入门指南:从零基础到基本操作
数据库·mysql·oracle
bjzhang757 分钟前
mysql 常用命令
数据库·mysql
kakawzw10 分钟前
微服务组件源码2——Spring Ribbon原理(基于RibbonLoadBalancerClient)
java·微服务·ribbon
半壶清水11 分钟前
用python脚本加html自建的书法字典
开发语言·python·html
凯瑟琳.奥古斯特13 分钟前
力扣1003题C++解法详解
开发语言·c++·算法·leetcode·职场和发展
计算机安禾13 分钟前
【算法分析与设计】第48篇:流算法与数据概要技术
java·服务器·网络·数据库·算法
myenjoy_116 分钟前
Python + Snap7 实现西门子 S7-1200/1500 数据采集
开发语言·python
hunterkkk(c++)19 分钟前
SPFA最短路径算法(c++)
java·c++·算法
数据库小学妹20 分钟前
时序数据库核心原理拆解:写入吞吐、压缩存储、融合分析全链路分析
数据库·经验分享·时序数据库·dba
我是一颗柠檬27 分钟前
【Redis】Redis缓存应用实战Day12(2026年)
数据库·redis·缓存