K - the cache key entry typeV - the cache key value typepublic class SoftCache<K,V> extends Object
The actual cache is held through a soft reference, allowing it to be GCed under memory pressure.
This class is not thread-safe.
| Constructor and Description |
|---|
SoftCache()
Creates a new instance of a soft cache.
|
SoftCache(int theCapacity)
Creates a new instance of a soft cache.
|
SoftCache(int theCapacity,
float theLoadFactor,
boolean synchronizd)
Creates a new instance of a soft cache.
|
| Modifier and Type | Method and Description |
|---|---|
int |
capacity()
Returns the cache capacity.
|
void |
clear()
Clears the cache.
|
V |
compute(K key,
BiFunction<? super K,? super V,? extends V> remappingFunction)
Calls compute on underlying map, useful when cache is meant to behave as a pull-thru.
|
V |
computeIfAbsent(K key,
Function<? super K,? extends V> mappingFunction)
Calls computeIfAbsent on underlying map, useful when cache is meant to behave as a pull-thru.
|
boolean |
containsKey(K key) |
protected Map<K,V> |
createCache(int capacity,
float loadFactor,
boolean synchro)
Creates the underlying cache map.
|
Set<Map.Entry<K,V>> |
entrySet()
Produces the cache entry set.
|
V |
get(K key)
Gets a value from cache.
|
V |
put(K key,
V value)
Puts a value in cache.
|
V |
putIfAbsent(K key,
V value)
Calls putIfAbsent on underlying map, useful when cache is meant to behave as a pull-thru.
|
V |
remove(K key)
Removes a value from cache.
|
int |
size()
Returns the cache size.
|
Collection<V> |
values()
Produces the cache values collection.
|
public SoftCache()
public SoftCache(int theCapacity)
theCapacity - the cache sizepublic SoftCache(int theCapacity,
float theLoadFactor,
boolean synchronizd)
theCapacity - the cache capacitytheLoadFactor - the cache load actorsynchronizd - whether it is synchronized or notpublic int capacity()
public int size()
public void clear()
public Set<Map.Entry<K,V>> entrySet()
public Collection<V> values()
public boolean containsKey(K key)
public V get(K key)
key - the cache entry keypublic V put(K key, V value)
key - the cache entry keyvalue - the cache entry valuepublic V putIfAbsent(K key, V value)
key - tke keyvalue - the valuepublic V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
key - tke keymappingFunction - the function called if key does not associate to a valuepublic V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
key - tke keyremappingFunction - the function called if key does not associate to a valuepublic V remove(K key)
key - the cache entry keyprotected Map<K,V> createCache(int capacity, float loadFactor, boolean synchro)
capacity - the cache size, must be > 0loadFactor - the cache load factorsynchro - whether the cache is synchronized or notCopyright © 2024 The Apache Software Foundation. All rights reserved.