1.增加pom依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>

2.配置redis连接


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

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

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

@Configuration
public class MyCacheConfig {@Bean("myKeyGenerator")public KeyGenerator keyGenerator(){return new KeyGenerator() {@Overridepublic 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

 /*** 自定义缓存管理器*/@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory factory) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();Set<String> cacheNames = new HashSet<>();cacheNames.add("eo");cacheNames.add("eo1");ConcurrentHashMap<String, RedisCacheConfiguration> 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

cacheable 过期设置相关推荐

  1. Oracle 12c 用户密码过期设置的一些问题

    Oracle 12c 用户密码过期设置的一些问题 查看dba_profiles视图的结构,如下: SQL> desc dba_profiles;名称 是否为空? 类型----------- -- ...

  2. SpringBoot @Cacheable Redis 设置缓存过期时间

    1.x 设置 @Bean @Primary public CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManag ...

  3. mysql密码过期设置,mysql5.6.X和mysql8.0.X密码过期策略

    mysql密码过期策略设置 关于mysql5.6.X和mysql8.0.X配置密码过期策略的学习心得 一.mysql5.6.X配置密码过期策略 网上说的方式,但这不适用于低于5.7.4版本 二.mys ...

  4. mysql密码过期设置

    1.可以在mysql的配置文件中设置一个默认值,这会使得所有mysql用户的密码过期时间都为90天,mysql会从启动时开始计算时间.my.cnf配置如下: [mysqld]default_passw ...

  5. session过期设置

    具体设置很简单,方法有三种: (1)在主页面或者公共页面中加入:session.setMaxInactiveInterval(600);参数600单位是秒,即在没有10分钟活动后,session将失效 ...

  6. c# redis hashid如何设置过期时间_Redis中Key过期策略amp;淘汰机制

    1. Redis中设置Key过期时间 我们有两种方式设置过期时间 1.1 设置多久后过期 设置一个 key 10s 过期,可以这样 127.0.0.1:6379> SET key value E ...

  7. Redis源码分析:过期key删除与设置key的过期时间

    Redis中设置key过期时间与过期key的处理流程 在Redis中,可以再设置值的时候就设置该Key的过期时间,也可以通过在expire命令来设置某个key值的过期时间,并且在了解完设置过期时间之后 ...

  8. c# redis hashid如何设置过期时间_Redis数据库实现原理(划重点)

    Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构的db数组中,db数组的每一项都是一个redis.h/redisDb结构,每个redisDb结构代表一个数据库, ...

  9. ElasticSearch文档过期时间设置

    [b][size=large]新建索引启用过期设置:[/size][/b] PUT testindex{ "mappings": { "testtype": { ...

最新文章

  1. 团队作业——随堂小测
  2. 简述深度学习预训练和正则化
  3. GIT入门笔记(18)- 标签创建和管理
  4. java druid sqlite_springboot整合druid抛出dbType not support : sqlite异常
  5. oracle证书洛阳,ORACLE手工建库
  6. yii2 smarty php,Yii2-smarty的一些小坑
  7. 【华为编程大赛】洞穴逃生
  8. Tellico-经管你的收藏品
  9. 扇贝有道180903每日一句
  10. Dictionary 索引超出数组界限
  11. 人大金仓数据库的备份与还原
  12. Java的keytool命令
  13. 11 款可替代 top 命令的工具
  14. 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
  15. Unable to find instance for system
  16. 一份Java面试宝典「Java核心知识点」囊括JVM,Spring等29个技术
  17. 11.18-刷题日结
  18. Docker从入门到上瘾,万字终极指南!
  19. LTO助推磁带大容量存储技术超越磁盘HDD
  20. Word学习笔记分享

热门文章

  1. OneNote 英文默认字体修改方法(2020.10)
  2. c语言rewind函数作用,C 文件 rewind() 函数
  3. win7的Par虚拟机版优化(parellel desktop)
  4. Android 包大小优化总结
  5. AutoSAR系列讲解(入门篇)4.1-BSW概述
  6. 电脑无法连接WiFi(显示地球图标)
  7. 车载网络结构(车内)-基础概念
  8. 手游推广加盟有哪些坑?
  9. 苹果电脑如何同时运行Mac和Windows--pd18
  10. 20191129每周精品之编程