public class ExpiryLRUMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
支持项 : 1.定时过期(过期事件通知) setOnExpiryConsumer(Consumer) onExpiry(Node)
2. LRU淘汰机制(淘汰事件通知) setOnEvictionConsumer(Consumer) onEviction(Node)
3. Map操作
4. 并发操作(线程安全) ConcurrentMap
5. gc回收 Reference(Weak,Soft,strong). ExpiryLRUMap(int, long, long, ConcurrentLinkedHashMap.Weigher, Class)
6. 统计功能(miss, hit) getHitCount() getMissCount()
7. null值替换, 防止缓存击穿 setReplaceNullValueFlag(boolean) NULL if(data == ExpiryLRUMap.NULL)
常用场景 : localCache
| 限定符和类型 | 类和说明 |
|---|---|
static class |
ExpiryLRUMap.ExpiresNotify |
static class |
ExpiryLRUMap.ExpiresScan |
static class |
ExpiryLRUMap.Node<KEY,VALUE> |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| 构造器和说明 |
|---|
ExpiryLRUMap()
默认永不过期 (相当于普通的 ConcurrentMap)
|
ExpiryLRUMap(int initialCapacity,
long maxCacheSize,
long defaultExpiryTime,
ConcurrentLinkedHashMap.Weigher<ExpiryLRUMap.Node<K,V>> weigher) |
ExpiryLRUMap(int initialCapacity,
long maxCacheSize,
long defaultExpiryTime,
ConcurrentLinkedHashMap.Weigher<ExpiryLRUMap.Node<K,V>> weigher,
Class<? extends Reference> referenceType) |
ExpiryLRUMap(long defaultExpiryTime) |
putAllpublic static final Object NULL
public ExpiryLRUMap()
public ExpiryLRUMap(long defaultExpiryTime)
public ExpiryLRUMap(int initialCapacity,
long maxCacheSize,
long defaultExpiryTime,
ConcurrentLinkedHashMap.Weigher<ExpiryLRUMap.Node<K,V>> weigher)
public ExpiryLRUMap(int initialCapacity,
long maxCacheSize,
long defaultExpiryTime,
ConcurrentLinkedHashMap.Weigher<ExpiryLRUMap.Node<K,V>> weigher,
Class<? extends Reference> referenceType)
initialCapacity - initialCapacitymaxCacheSize - maxCacheSizedefaultExpiryTime - defaultExpiryTimeweigher - weigherreferenceType - null is FinalReference.
else if WeakReference
else if SoftReferencepublic static Set<ExpiryLRUMap> getInstanceSet()
public static BlockingQueue<ExpiryLRUMap.Node<?,?>> getExpiryNotifyQueue()
public static boolean isExpiry(ExpiryLRUMap.Node node)
public static void main(String[] args) throws InterruptedException
public long weightedSize()
public long getMaxCacheSize()
public void setMaxCacheSize(long maxCacheSize)
public Consumer<ExpiryLRUMap.Node<K,V>> getOnEvictionConsumer()
public void setOnEvictionConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onEvictionConsumer)
public long getMissCount()
public long getHitCount()
public Consumer<ExpiryLRUMap.Node<K,V>> getOnExpiryConsumer()
public void setOnExpiryConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onExpiryConsumer)
public void setOnRemoveConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onRemoveConsumer)
public boolean isReplaceNullValueFlag()
public void setReplaceNullValueFlag(boolean replaceNullValueFlag)
public V put(K key, V value, long timeout)
key - keyvalue - valuetimeout - 键值对有效期 毫秒(Long.MAX_VALUE 表示永不过期)public boolean containsKey(Object key)
containsKey 在接口中 Map<K,V>containsKey 在类中 AbstractMap<K,V>public boolean containsValue(Object value)
containsValue 在接口中 Map<K,V>containsValue 在类中 AbstractMap<K,V>public void notifyRemove(ExpiryLRUMap.Node<K,V> node)
public Collection<V> values()
public V getOrDefault(Object key, V defaultValue)
getOrDefault 在接口中 ConcurrentMap<K,V>getOrDefault 在接口中 Map<K,V>public void forEach(BiConsumer<? super K,? super V> action)
public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
replaceAll 在接口中 ConcurrentMap<K,V>replaceAll 在接口中 Map<K,V>public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
computeIfAbsent 在接口中 ConcurrentMap<K,V>computeIfAbsent 在接口中 Map<K,V>public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
computeIfPresent 在接口中 ConcurrentMap<K,V>computeIfPresent 在接口中 Map<K,V>public V putIfAbsent(K key, V value)
putIfAbsent 在接口中 ConcurrentMap<K,V>putIfAbsent 在接口中 Map<K,V>public long getDefaultExpiryTime()
public void setDefaultExpiryTime(long defaultExpiryTime)
public String toString()
toString 在类中 AbstractMap<K,V>public void onExpiry(ExpiryLRUMap.Node<K,V> node)
public void onEviction(ExpiryLRUMap.Node<K,V> node)
public void onRemove(ExpiryLRUMap.Node<K,V> node)
Copyright © 2023. All rights reserved.