java–基础–9.10–集合–Map–过期时间的Map
/**
* 可以设置过期时间的Map,ExpiryMap继承至HashMap 重写了所有对外的方法,对每个key值都设置了有效期
*
* @author feizhou
* @since 2020/8/15 13:50
*/
public class ExpiryMap<K, V> extends HashMap<K, V> {
private static final long serialVersionUID = 1L;
/**
* 默认过期时间是2秒
*
* @author feizhou
* @since 2020/8/15 13:51
*/
private long expiry = 1000 * 2;
/**
* 设置key 的过期时间
*
* @author feizhou
* @since 2020/8/15 13:55
*/
private HashMap<K, Long> expiryMap = new HashMap<>();
/**
* 缓存实例对象
*/
private static volatile ExpiryMap<Object, Object> singleMap = new ExpiryMap<>();
/**
* 采用单例模式获取实例
*
* @return
*/
public static ExpiryMap<Object, Object> getInstance() {
return singleMap;
}
public ExpiryMap() {
super();
}
public ExpiryMap(long defaultExpiryTime) {
this(1 << 4, defaultExpiryTime);
}
public ExpiryMap(int initialCapacity, long defaultExpiryTime) {
super(initialCapacity);
this.expiry = defaultExpiryTime;
}
@Override
public V put(K key, V value) {
expiryMap.put(key, System.currentTimeMillis() + expiry);
return super.put(key, value);
}
@Override
public boolean containsKey(Object key) {
return !checkExpiry(key, true) && super.containsKey(key);
}
/**
* @param key
* @param value
* @param expiryTime 键值对有效期 毫秒
* @return
*/
public V put(K key, V value, long expiryTime) {
expiryMap.put(key, System.currentTimeMillis() + expiryTime);
return super.put(key, value);
}
@Override
public int size() {
return entrySet().size();
}
@Override
public boolean isEmpty() {
return entrySet().size() == 0;
}
@Override
public boolean containsValue(Object value) {
if (value == null) {
return Boolean.FALSE;
}
Set<Entry<K, V>> set = super.entrySet();
Iterator<Entry<K, V>> iterator = set.iterator();
while (iterator.hasNext()) {
Entry<K, V> entry = iterator.next();
if (value.equals(entry.getValue())) {
if (checkExpiry(entry.getKey(), false)) {
iterator.remove();
return Boolean.FALSE;
} else {
return Boolean.TRUE;
}
}
}
return Boolean.FALSE;
}
@Override
public Collection<V> values() {
Collection<V> values = super.values();
if (values == null || values.size() < 1) {
return values;
}
Iterator<V> iterator = values.iterator();
while (iterator.hasNext()) {
V next = iterator.next();
if (!containsValue(next)) {
iterator.remove();
}
}
return values;
}
@Override
public V get(Object key) {
if (key == null) {
return null;
}
if (checkExpiry(key, true)) {
return null;
}
return super.get(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
for (Entry<? extends K, ? extends V> e : m.entrySet()) {
expiryMap.put(e.getKey(), System.currentTimeMillis() + expiry);
}
super.putAll(m);
}
@Override
public Set<Entry<K, V>> entrySet() {
Set<Entry<K, V>> set = super.entrySet();
Iterator<Entry<K, V>> iterator = set.iterator();
while (iterator.hasNext()) {
Entry<K, V> entry = iterator.next();
if (checkExpiry(entry.getKey(), false)) {
iterator.remove();
}
}
return set;
}
/**
* @param isRemoveSuper true super删除
* @return true 过期
* @Description: 是否过期
*/
private boolean checkExpiry(Object key, boolean isRemoveSuper) {
if (!expiryMap.containsKey(key)) {
return Boolean.FALSE;
}
long expiryTime = expiryMap.get(key);
boolean flag = System.currentTimeMillis() > expiryTime;
if (flag) {
if (isRemoveSuper) {
super.remove(key);
}
expiryMap.remove(key);
}
return flag;
}
public void cleanAllKey() {
Iterator<Entry<K, V>> iterator = super.entrySet().iterator();
while (iterator.hasNext()) {
try {
Entry<K, V> next = iterator.next();
if (next.getKey() != null && next.getValue() != null) {
iterator.remove();
}
} catch (Exception e) {
continue;
}
}
}
public static void main(String[] args) throws InterruptedException {
ExpiryMap<String, String> map = new ExpiryMap<>();
map.put("test", "xxx");
map.put("test2", "ankang", 5000);
// System.out.println("test==" + map.get("test"));
Thread.sleep(3000);
// System.out.println("test==" + map.get("test"));
// System.out.println("test2==" + map.get("test2"));
Thread.sleep(3000);
// System.out.println("test2==" + map.get("test2"));
}
}