public class ExpiryMap<K, V> implements Map<K, V> {
private ConcurrentHashMap workMap;
private ConcurrentHashMap expiryMap;
/**
* 默认保存时间2分钟
*/
private long EXPIRYTIME = 1000 * 60 * 2;
/**
* circulation 循环时间 默认5分钟
*/
private long CIRCULATIONTIME = 1000 * 60 * 5;
/**
* delay 启动延迟时间 默认1分钟
*/
private long DELAY = 1000 * 60;
public ExpiryMap() {
this(16, 0);
}
/**
* 单位秒
*
* @param expiryTime
*/
public ExpiryMap(long expiryTime) {
this(16, expiryTime);
}
/**
* 单位秒
*
* @param initialCapacity
* @param expiryTime
*/
public ExpiryMap(int initialCapacity, long expiryTime) {
if (expiryTime > 0) {
this.EXPIRYTIME = expiryTime * 1000;
}
workMap = new ConcurrentHashMap(initialCapacity);
expiryMap = new ConcurrentHashMap(initialCapacity);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
removeExpiryKeys();
}
}, DELAY, CIRCULATIONTIME);
}
/**
* 使用 全局过期时间
*
* @param key
* @param value
* @return
*/
@Override
public V put(K key, V value) {
expiryMap.put(key, System.currentTimeMillis() + EXPIRYTIME);
return (V) workMap.put(key, value);
}
/**
* 使用 自定义过期时间 单位秒
*
* @param key
* @param value
* @return
*/
public V put(K key, V value, long exrity) {
expiryMap.put(key, System.currentTimeMillis() + exrity * 1000);
return (V) workMap.put(key, value);
}
/**
* 重新设置超时时间
*
* @param key
* @return
*/
public void put(K key, long exrity) {
expiryMap.put(key, System.currentTimeMillis() + exrity * 1000);
}
private void removeExpiryKeys() {
expiryMap.keySet().forEach(key -> {
checkExpiry((K) key);
});
}
/**
* 是否过期判断函数
*
* @param key
* @return 过期true 不过期false
*/
public boolean checkExpiry(K key) {
Object timeObject = expiryMap.get(key);
if (timeObject == null) {
return true;
}
long setTime = (long) timeObject;
boolean isExpiry = System.currentTimeMillis() - setTime >= 0;
if (isExpiry) {
expiryMap.remove(key);
workMap.remove(key);
return true;
}
return false;
}
@Override
public V get(Object key) {
boolean isExpiry = checkExpiry((K) key);
if (isExpiry) {
return null;
}
return (V) workMap.get(key);
}
@Override
public int size() {
removeExpiryKeys();
return workMap.size();
}
@Override
public boolean isEmpty() {
removeExpiryKeys();
return workMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
removeExpiryKeys();
return workMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
removeExpiryKeys();
return workMap.containsValue(value);
}
@Override
public V remove(Object key) {
expiryMap.remove(key);
return (V) workMap.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
m.entrySet().forEach(en -> {
expiryMap.put(en.getKey(), System.currentTimeMillis() + EXPIRYTIME);
workMap.put(en.getKey(), en.getValue());
});
}
@Override
public void clear() {
expiryMap.clear();
workMap.clear();
}
@Override
public Set<K> keySet() {
removeExpiryKeys();
return workMap.keySet();
}
@Override
public Collection<V> values() {
removeExpiryKeys();
return workMap.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
removeExpiryKeys();
return workMap.entrySet();
}
public void setCIRCULATIONTIME(long CIRCULATIONTIME) {
this.CIRCULATIONTIME = CIRCULATIONTIME;
}
public void setDELAY(long DELAY) {
this.DELAY = DELAY;
}
public long getCIRCULATIONTIME() {
return CIRCULATIONTIME;
}
public long getDELAY() {
return DELAY;
}
}
带过期时间的Map类
最新推荐文章于 2024-06-24 16:13:55 发布