maven
net.jodah
expiringmap
0.5.8
gradle
compile group: 'net.jodah', name: 'expiringmap', version: '0.5.8'
/**
* 默认5000毫秒失效 时间单位可以自定义 过期策略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:value
null
/**
* 更详细的观察
*/
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
ExpiringMap map = ExpiringMap.builder().expiration(3, TimeUnit.SECONDS)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
map.put("key","value");
Thread.sleep(2000);
map.put("key","valu2");
System.out.println("更新value=" + map.get("key"));
Thread.sleep(1001);
if (map.containsKey("key")) {
map.remove("key");
System.out.println("删除 key ");
}
更新value=value2
删除 key
ExpiringMap map = ExpiringMap.builder().expiration(3, TimeUnit.SECONDS)
.expirationPolicy(ExpirationPolicy.CREATED)
.build();
map.put("key","value");
Thread.sleep(2000);
map.put("key","value");
System.out.println("更新value=" + map.get("key"));
Thread.sleep(1001);
if (map.containsKey("key")) {
map.remove("key");
System.out.println("删除 key ");
}
更新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("第一个key a:0未过期:"+map.get("a:0"));
// 插入第10个key后 第一个key过期
map.put("a:10","v:10");
System.out.println("第一个key a:0过期:"+map.get("a:0"));
System.out.println("第二个key a:1未过期:"+map.get("a:1"));
第一个key a:0未过期:v:0
第一个key a:0过期:null
第二个key a:1未过期:v: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 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