public class ExpiryLRUMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
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) |
| 限定符和类型 | 方法和说明 |
|---|---|
V |
atomicGet(K key,
Supplier<V> supplier) |
void |
clear() |
V |
compute(K key,
BiFunction<? super K,? super V,? extends V> remappingFunction) |
V |
computeIfAbsent(K key,
Function<? super K,? extends V> mappingFunction) |
V |
computeIfPresent(K key,
BiFunction<? super K,? super V,? extends V> remappingFunction) |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet() |
boolean |
equals(Object o) |
void |
forEach(BiConsumer<? super K,? super V> action) |
V |
get(Object key) |
long |
getDefaultExpiryTime() |
static BlockingQueue<ExpiryLRUMap.Node<?,?>> |
getExpiryNotifyQueue() |
long |
getHitCount() |
static Set<ExpiryLRUMap> |
getInstanceSet() |
long |
getMaxCacheSize() |
long |
getMissCount() |
Consumer<ExpiryLRUMap.Node<K,V>> |
getOnEvictionConsumer() |
Consumer<ExpiryLRUMap.Node<K,V>> |
getOnExpiryConsumer() |
V |
getOrDefault(Object key,
V defaultValue) |
int |
hashCode() |
boolean |
isEmpty() |
static boolean |
isExpiry(ExpiryLRUMap.Node node) |
boolean |
isReplaceNullValueFlag() |
Set<K> |
keySet() |
static void |
main(String[] args) |
V |
merge(K key,
V value,
BiFunction<? super V,? super V,? extends V> remappingFunction) |
void |
onEviction(ExpiryLRUMap.Node<K,V> node) |
void |
onExpiry(ExpiryLRUMap.Node<K,V> node) |
V |
put(K key,
V value) |
V |
put(K key,
V value,
long expiryTime) |
V |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
boolean |
removeIfExpiry() |
boolean |
removeIfExpiry(Consumer<ExpiryLRUMap.Node<K,V>> removeConsumer) |
V |
replace(K key,
V newValue) |
boolean |
replace(K key,
V oldValue,
V newValue) |
void |
replaceAll(BiFunction<? super K,? super V,? extends V> function) |
void |
setDefaultExpiryTime(long defaultExpiryTime) |
void |
setMaxCacheSize(long maxCacheSize) |
void |
setOnEvictionConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onEvictionConsumer) |
void |
setOnExpiryConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onExpiryConsumer) |
void |
setReplaceNullValueFlag(boolean replaceNullValueFlag) |
int |
size() |
String |
toString() |
Collection<V> |
values() |
long |
weightedSize() |
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 long weightedSize()
public long getMaxCacheSize()
public void setMaxCacheSize(long maxCacheSize)
public static Set<ExpiryLRUMap> getInstanceSet()
public static BlockingQueue<ExpiryLRUMap.Node<?,?>> getExpiryNotifyQueue()
public Consumer<ExpiryLRUMap.Node<K,V>> getOnEvictionConsumer()
public void setOnEvictionConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onEvictionConsumer)
public long getMissCount()
public long getHitCount()
public void setReplaceNullValueFlag(boolean replaceNullValueFlag)
public Consumer<ExpiryLRUMap.Node<K,V>> getOnExpiryConsumer()
public void setOnExpiryConsumer(Consumer<ExpiryLRUMap.Node<K,V>> onExpiryConsumer)
public boolean isReplaceNullValueFlag()
public V put(K key, V value, long expiryTime)
key - keyvalue - valueexpiryTime - 键值对有效期 毫秒(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 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 boolean removeIfExpiry()
public boolean removeIfExpiry(Consumer<ExpiryLRUMap.Node<K,V>> removeConsumer)
public long getDefaultExpiryTime()
public void setDefaultExpiryTime(long defaultExpiryTime)
public static boolean isExpiry(ExpiryLRUMap.Node node)
public String toString()
toString 在类中 AbstractMap<K,V>public void onExpiry(ExpiryLRUMap.Node<K,V> node)
public void onEviction(ExpiryLRUMap.Node<K,V> node)
public static void main(String[] args) throws InterruptedException
Copyright © 2021. All rights reserved.