超时定长HashMap可以当做简单的缓存使用,要比一般的jvm内部缓存要快很多,当然了也会比网络缓存有数量级的性能提升。
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* 可以设置hashmap删除过期的数据,还可以设置容器的最大容量。参考了下面路径
* https://blog.csdn.net/u011534095/article/details/54091337
* @author wzjin
* @date 2019年12月31日 上午11:31:24
* @param <K>
* @param <V>
*/
public class 超时定长HashMap<K, V> extends LinkedHashMap<K, V>
{
private static final long serialVersionUID = 1L;
private long EXPIRY = 1000 * 60 * 2;//两分钟后超时
private LinkedHashMap<K, Long> 超时HashMap = new LinkedHashMap<>();
private int capacity = 120;
/**
* 采用默认容量和默认超时时间
*/
public 超时定长HashMap()
{
super();
}
public 超时定长HashMap(long defaultExpiryTime)
{
this(16, defaultExpiryTime);
}
public 超时定长HashMap(long defaultExpiryTime,int capacity)
{
this(16, defaultExpiryTime);
this.capacity = capacity;
}
public 超时定长HashMap(int initialCapacity, long defaultExpiryTime)
{
super(initialCapacity);
this.EXPIRY = defaultExpiryTime;
}
/**
*
*/
public 超时定长HashMap(int capacity)
{
this.capacity = capacity;
}
public 超时定长HashMap(int initialCapacity, long defaultExpiryTime,int capacity)
{
super(initialCapacity);
this.EXPIRY = defaultExpiryTime;
this.capacity = capacity;
}
public V put(K key, V value)
{
超时HashMap.put(key, System.currentTimeMillis() + EXPIRY);
return super.put(key, value);
}
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)
{
超时HashMap.put(key, System.currentTimeMillis() + expiryTime);
return super.put(key, value);
}
public int size()
{
return entrySet().size();
}
public boolean isEmpty()
{
return entrySet().size() == 0;
}
public boolean containsValue(Object value)
{
if (value == null)
return Boolean.FALSE;
Set<java.util.Map.Entry<K, V>> set = super.entrySet();
Iterator<java.util.Map.Entry<K, V>> iterator = set.iterator();
while (iterator.hasNext())
{
java.util.Map.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;
}
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;
}
public V get(Object key)
{
if (key == null)
return null;
if (checkExpiry(key, true))
return null;
return super.get(key);
}
/**
*
* @Description: 是否过期
* @param key
* @return null:不存在或key为null -1:过期 存在且没过期返回value 因为过期的不是实时删除,所以稍微有点作用
*/
public Object isInvalid(Object key)
{
if (key == null)
return null;
if (!超时HashMap.containsKey(key))
{
return null;
}
long expiryTime = 超时HashMap.get(key);
boolean flag = System.currentTimeMillis() > expiryTime;
if (flag)
{
super.remove(key);
超时HashMap.remove(key);
return -1;
}
return super.get(key);
}
public void putAll(Map<? extends K, ? extends V> m)
{
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
超时HashMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY);
super.putAll(m);
}
public Set<Map.Entry<K, V>> entrySet()
{
Set<java.util.Map.Entry<K, V>> set = super.entrySet();
Iterator<java.util.Map.Entry<K, V>> iterator = set.iterator();
while (iterator.hasNext())
{
java.util.Map.Entry<K, V> entry = iterator.next();
if (checkExpiry(entry.getKey(), false))
iterator.remove();
}
return set;
}
/**
*
* @Description: 是否过期
* @author: qd-ankang
* @date: 2016-11-24 下午4:05:02
* @param expiryTime
* true 过期
* @param isRemoveSuper
* true super删除
* @return
*/
private boolean checkExpiry(Object key, boolean isRemoveSuper)
{
if (!超时HashMap.containsKey(key))
{
return Boolean.FALSE;
}
long expiryTime = 超时HashMap.get(key);
boolean flag = System.currentTimeMillis() > expiryTime;
if (flag)
{
if (isRemoveSuper)
super.remove(key);
超时HashMap.remove(key);
}
return flag;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> oldest)
{
return size() > capacity;
}
public static void main(String[] args) throws InterruptedException
{
超时定长HashMap<String, String> map = new 超时定长HashMap<>(10);
map.put("test", "ankang");
map.put("test1", "ankang");
map.put("test2", "ankang", 3000);
System.out.println("test1" + map.get("test"));
Thread.sleep(1000);
System.out.println("isInvalid:" + map.isInvalid("test"));
System.out.println("size:" + map.size());
System.out.println("size:" + ((HashMap<String, String>) map).size());
for (Map.Entry<String, String> m : map.entrySet())
{
System.out.println("isInvalid:" + map.isInvalid(m.getKey()));
map.containsKey(m.getKey());
System.out.println("key:" + m.getKey() + " value:" + m.getValue());
}
System.out.println("test1" + map.get("test"));
// System.out.println(1 << 4);
}
}