1.可设置Map中的Entry在一段时间后自动过期。
2.可设置Map最大容纳值,当到达Maximum size后,再次插入值会导致Map中的第一个值过期。
3.可添加监听事件,在监听到Entry过期时调度监听函数。
4.可以设置懒加载,在调用get()方法时创建对象。
Github地址
Maven
/**
*expiration(5000,TimeUnit.MILLISECONDS)
*设置过期时间为5秒
*ExpirationPolicy.CREATED)
*设置过期策略为创建或更新值后
*/
ExpiringMap map = 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:value
null
过期协议:ExpirationPolicy.CREATED。
/**
* ExpirationPolicy.CREATED:在每次更新元素时,过期时间同时清零。
* ExpirationPolicy.ACCESSED:在每次访问元素时,过期时间同时清零。
*/
ExpiringMap map = 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:value
null
在过期时间内调用map.put(),map.replace()更新操作后:
ExpiringMap map = 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:value2
value2
过期协议:ExpirationPolicy.ACCESSED。
ExpiringMap map = 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:value
value
ExpiringMap map = 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:null
key2:value2
key2:null
//Maximum size设置为10个
Map map = 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过期:null
a:1未过期:a:1
ExpiringMap map = 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
也可在运行是添加/删除监听
ExpiringMap map = ExpiringMap.builder().
variableExpiration().
build();
ExpirationListener expirationListener = ((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:value
使用懒加载模式时,可以不用去向map中put对象,在调用get方法时自动去创建对象。
class Connection {
String ipAddress;
public Connection(String ipAddress) {
this.ipAddress = ipAddress;
}
}
Map map = 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