SpringBoot中的ExpiringMap代码实例
作者:yzm4399
这篇文章主要介绍了SpringBoot中的ExpiringMap代码实例,ExpiringMap是一个可以设置过期策略、可变条目过期、延迟条目加载和过期侦听器的线程安全存储容器,需要的朋友可以参考下
1、简介
ExpiringMap是一个可以设置过期策略、可变条目过期、延迟条目加载和过期侦听器的线程安全存储容器。
2、引入依赖
<dependency> <groupId>net.jodah</groupId> <artifactId>expiringmap</artifactId> <version>0.5.8</version> </dependency>
3、过期时间
private static void demo01() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder() // 过期时间:5000,单位毫秒 .expiration(5000, TimeUnit.MILLISECONDS) .build(); log.info("添加元素"); map.put("name", "yzm"); while (map.containsKey("name")) { log.info("name = " + map.get("name")); Thread.sleep(1000); } log.info("超过过期时间,自动删除"); }
4、重置过期时间
private static void demo01_2() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder() // 允许映射条目具有单独的到期日,并允许更改到期日 //.variableExpiration() // 过期时间:5000,单位毫秒 .expiration(5000, TimeUnit.MILLISECONDS) .build(); log.info("添加元素"); map.put("name", "yzm"); int count = 0; while (map.containsKey("name")) { count++; log.info("name = " + map.get("name")); Thread.sleep(1000); if (count == 4) { //指定key重置过期时间 map.setExpiration("name", 5000, TimeUnit.MILLISECONDS); } } log.info("超过过期时间,自动删除"); }
未启用variableExpiration
启用variableExpiration
5、过期策略
ExpirationPolicy.ACCESSED:访问或更新元素时,重置过期时间(即过期时间重新从零计算)
ExpirationPolicy.CREATED:创建或更新元素时,重置过期时间。默认策略。
private static void demo02() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder() .variableExpiration() .expiration(5000, TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.ACCESSED) .build(); log.info("添加元素"); map.put("name", "yzm"); int count = 0; while (map.containsKey("name")) { count++; log.info("name = " + map.get("name")); Thread.sleep(1000); if (count > 10) { break; } } log.info("count:" + count); }
private static void demo02() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder() .variableExpiration() .expiration(5000, TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.ACCESSED) .build(); log.info("添加元素"); map.put("name", "yzm"); int count = 0; while (map.containsKey("name")) { count++; //log.info("name = " + map.get("name")); Thread.sleep(1000); if (count == 4) { map.replace("name", "Yzm"); } } log.info("count:" + count); }
经测试,
- ACCESSED过期策略:调用get、put、replace等方法时,都会重置过期时间;
- CREATED过期策略:调用put、replace等方法时,会重置过期时间,get不会。
6、容器最大值
超过最大值,最早的元素被移除
private static void demo03() throws InterruptedException { ExpiringMap<Integer, Object> map = ExpiringMap.builder() .maxSize(5) .build(); for (int i = 1; i <= 10; i++) { map.put(i, i); System.out.println(map.keySet()); Thread.sleep(1000); } }
7、过期侦听器
private static void demo04() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder() .expiration(3000, TimeUnit.MILLISECONDS) // 同步通知 .expirationListener(((key, value) -> log.info("触发过期侦听器:" + key + " = " + value))) // 异步通知 .asyncExpirationListener(((key, value) -> log.info("触发过期侦听器:" + key + " = " + value))) .build(); log.info("添加元素"); map.put("name", "yzm"); map.put("name2", "yzm2"); map.put("name3", "yzm3"); Thread.sleep(10000); }
8、单个元素配置
private static void demo05() throws InterruptedException { ExpiringMap<String, Object> map = ExpiringMap.builder().variableExpiration().build(); map.put("key", "value", ExpirationPolicy.CREATED, 3, TimeUnit.SECONDS); while (map.containsKey("key")) { log.info("key = " + map.get("key")); Thread.sleep(1000); } }
到此这篇关于SpringBoot中的ExpiringMap代码实例的文章就介绍到这了,更多相关SpringBoot的ExpiringMap内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!