ExpiringMap用法详解
1.功能简介
2.使用说明
3.详细介绍
3.1 设置过期时间与过期协议3.2为Map中单个元素设置过期时间3.3设置Map的Maximum size3.4过期监听事件3.5懒加载1.功能简介1.可设置Map中的Entry在一段时间后自动过期。2.可设置Map最大容纳值,当到达Maximum size后,再次插入值会导致Map中的第一个值过期。3.可添加监听事件,在监听到Entry过期时调度监听函数。4.可以设置懒加载,在调用get()方法时创建对象。2.使用说明Github地址Mavennet.jodahexpiringmap0.5.83.详细介绍3.1 设置过期时间与过期协议/** *expiration(5000,TimeUnit.MILLISECONDS) *设置过期时间为5秒 *ExpirationPolicy.CREATED) *设置过期策略为创建或更新值后 */ ExpiringMapmap = ExpiringMap.builder().expiration(5000,TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.CREATED) .build(); map.put("key","value"); System.out.println("key:"+map.get("key")); //等待5秒 Thread.sleep(5001); System.out.println(map.get("key"));执行结果:key:valuenull过期协议:ExpirationPolicy.CREATED。/** * ExpirationPolicy.CREATED:在每次更新元素时,过期时间同时清零。 * ExpirationPolicy.ACCESSED:在每次访问元素时,过期时间同时清零。 */ ExpiringMapmap = ExpiringMap.builder().expiration(5000,TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.CREATED) .build(); map.put("key","value"); System.out.println(map.get("key")); Thread.sleep(4000); System.out.println("访问map:"+map.get("key")); Thread.sleep(1001); System.out.println(map.get("key"));执行结果:value访问map:valuenull在过期时间内调用map.put(),map.replace()更新操作后:ExpiringMapmap = ExpiringMap.builder().expiration(5000,TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.CREATED) .build(); map.put("key","value"); System.out.println(map.get("key")); Thread.sleep(4000); map.put("key","value2"); System.out.println("更新map:"+map.get("key")); Thread.sleep(1001); System.out.println(map.get("key"));执行结果:value更新map:value2value2过期协议:ExpirationPolicy.ACCESSED。ExpiringMapmap = ExpiringMap.builder().expiration(5000,TimeUnit.MILLISECONDS) .expirationPolicy(ExpirationPolicy.ACCESSED) .build(); map.put("key","value"); System.out.println(map.get("key")); Thread.sleep(4000); System.out.println("访问map:"+map.get("key")); Thread.sleep(1001); System.out.println(map.get("key"));执行结果:value访问map:valuevalue3.2为Map中单个元素设置过期时间ExpiringMapmap = ExpiringMap.builder().variableExpiration() .expirationPolicy(ExpirationPolicy.CREATED) .build(); map.put("key1","value1",ExpirationPolicy.ACCESSED,5000,TimeUnit.MILLISECONDS); map.put("key2","value2",10000,TimeUnit.MILLISECONDS); Thread.sleep(5001); System.out.println("key1:"+map.get("key1")); System.out.println("key2:"+map.get("key2")); Thread.sleep(5000); System.out.println("key2:"+map.get("key2"));执行结果key1:nullkey2:value2key2:null3.3设置Map的Maximum size//Maximum size设置为10个 Mapmap = ExpiringMap.builder().maxSize(10) .build(); //向Map中插入10个数 for (int i = 0;i<10;i++){ map.put("a:"+i,"v:"+i); } System.out.println("a:0未过期:"+map.get("a:0")); System.out.println("插入a:10后"); map.put("a:10","v:10"); System.out.println("a:0过期:"+map.get("a:0")); System.out.println("a:1未过期:"+"a:1");执行结果a:0未过期:v:0插入a:10后a:0过期:nulla:1未过期:a:13.4过期监听事件ExpiringMapmap = ExpiringMap.builder().variableExpiration() .expirationListener((thekey, thevalue) -> { System.out.println("key:"+thekey+"过期"); }).build(); map.put("key","value", 5000, TimeUnit.MILLISECONDS); Thread.sleep(5001); System.out.println("key:"+map.get("key"));运行结果key:key过期key:null也可在运行是添加/删除监听ExpiringMapmap = ExpiringMap.builder(). variableExpiration(). build(); ExpirationListenerexpirationListener = ((theKey,theValue)->{ System.out.println("过期监听事件"+"key:"+theKey+" value:"+theValue); }); map.put("key","value",ExpirationPolicy.CREATED,5000,TimeUnit.MILLISECONDS); //添加监听 map.addExpirationListener(expirationListener); Thread.sleep(5001); //清除监听 map.removeExpirationListener(expirationListener);运行结果过期监听事件key:key value:value3.5懒加载使用懒加载模式时,可以不用去向map中put对象,在调用get方法时自动去创建对象。class Connection { String ipAddress; public Connection(String ipAddress) { this.ipAddress = ipAddress; } } Mapmap = ExpiringMap.builder()
.expiration(5000, TimeUnit.MILLISECONDS)
.expirationListener((theKey,theValue)->{
System.out.println("过期key:"+theKey);
})
.entryLoader(address -> new Connection((String) address))
.build();
System.out.println(map.get("127.0.0.1"));
Thread.sleep(5001);
执行结果:
com.zhizhou.netty.SocketChannelMapTest$1Connection@b1e9b8
过期key:127.0.0.1
网友评论