栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

cacheable 过期设置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cacheable 过期设置

1.增加pom依赖


    org.springframework.boot
    spring-boot-starter-data-redis


    org.apache.commons
    commons-pool2

2.配置redis连接

#  redis配置
spring:
  redis:
    database: 0
    host: 10.0.0.36
    password: 123456
    port: 6379
    timeout:  5000
    lettuce:
      pool:
        max-active: 300
        max-wait: -1
        max-idle: 20
        min-idle: 10

3.启动类添加 @EnableCaching // 开启缓存

4. keyGenerator :key 的生成器。 key 和 keyGenerator 二选一使用

@Configuration
public class MyCacheConfig {
    @Bean("myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return method.getName()+"["+ Arrays.asList(params).toString() +"]";
            }
        };
    }
    
}

5.service实现

@Cacheable(cacheNames = "eo",key = "#id")
    public Integer getEo(int id) throws Exception{
        log.info("111");
        Thread.sleep(1000);
        log.info("id is {}",id);
        return id;
    }

    @Cacheable(cacheNames = "eo1",keyGenerator = "myKeyGenerator")
    public Integer getEo1(int id) throws Exception{
        log.info("222");
        Thread.sleep(1000);
        log.info("id is {}",id);
        return id;
    }

6.controller

    @GetMapping("/xx")
    public Integer xx(Integer id) throws Exception {
       return cacheService.getEo(id);
    }
    @GetMapping("/xx1")
    public Integer xx1(Integer id) throws Exception {
       return cacheService.getEo1(id);
    }

7.RedisConfig

 
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        Set cacheNames = new HashSet<>();
        cacheNames.add("eo");
        cacheNames.add("eo1");
        ConcurrentHashMap configMap = new ConcurrentHashMap<>();
        configMap.put("eo", config.entryTtl(Duration.ofMinutes(1L)));
        configMap.put("eo1", config);

        //需要先初始化缓存名称,再初始化其它的配置。
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory).initialCacheNames(cacheNames).withInitialCacheConfigurations(configMap).build();
        return cacheManager;
    }

8.运行结果

2022-05-13 15:10:49.818  INFO 344756 --- [nio-8080-exec-2] com.example.demo.cache.CacheService      : 111
2022-05-13 15:10:50.818  INFO 344756 --- [nio-8080-exec-2] com.example.demo.cache.CacheService      : id is 3
2022-05-13 15:12:03.241  INFO 344756 --- [nio-8080-exec-9] com.example.demo.cache.CacheService      : 111
2022-05-13 15:12:04.242  INFO 344756 --- [nio-8080-exec-9] com.example.demo.cache.CacheService      : id is 3
2022-05-13 15:17:33.073  INFO 344756 --- [nio-8080-exec-4] com.example.demo.cache.CacheService      : 111
2022-05-13 15:17:34.073  INFO 344756 --- [nio-8080-exec-4] com.example.demo.cache.CacheService      : id is 3
2022-05-13 15:18:01.530  INFO 344756 --- [nio-8080-exec-8] com.example.demo.cache.CacheService      : 222
2022-05-13 15:18:02.531  INFO 344756 --- [nio-8080-exec-8] com.example.demo.cache.CacheService      : id is 1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号