K
- type of the keysV
- type of the valuespublic class PriorityCache<K,V> extends Object
PriorityCache
implements a partial mapping from keys of type K
to values
of type V
. Mappings are associates with a cost, which states how expensive it is
to recreate that mapping. This cache uses the cost such that mappings with a higher cost
have a lower chance of being evicted than mappings with a lower cost. When an item from
this cache is successfully looked up its cost is incremented by one, unless it has reached
its maximum cost of Byte.MAX_VALUE
already.
Additionally this cache tracks a generation for mappings. Mappings of later generations always take precedence over mappings of earlier generations. That is, putting a mapping of a later generation into the cache can cause any mapping of an earlier generation to be evicted regardless of its cost.
This cache uses rehashing to resolve clashes. The number of rehashes is configurable. When a clash cannot be resolved by rehashing the given number of times the put operation fails.
This cache is thread safe.
Constructor and Description |
---|
PriorityCache(int size) |
PriorityCache(int size,
int rehash,
@NotNull Weigher<K,V> weigher)
Create a new instance of the given
size . |
PriorityCache(int size,
@NotNull Weigher<K,V> weigher)
Create a new instance of the given
size . |
Modifier and Type | Method and Description |
---|---|
long |
estimateCurrentWeight() |
static <K,V> Supplier<PriorityCache<K,V>> |
factory(int size)
Static factory for creating new
PriorityCache instances. |
static <K,V> Supplier<PriorityCache<K,V>> |
factory(int size,
@NotNull Weigher<K,V> weigher)
Static factory for creating new
PriorityCache instances. |
V |
get(K key,
int generation)
Look up a mapping from this cache by its
key and generation . |
@NotNull CacheStats |
getStats() |
static long |
nextPowerOfTwo(int size)
Round
size up to the next power of two or 1 for negative values. |
void |
purgeGenerations(@NotNull Predicate<Integer> purge)
Purge all keys from this cache whose entry's generation matches the
passed
purge predicate. |
boolean |
put(K key,
V value,
int generation,
byte initialCost)
Add a mapping to the cache.
|
long |
size() |
String |
toString() |
public PriorityCache(int size, int rehash, @NotNull @NotNull Weigher<K,V> weigher)
size
. rehash
specifies the number
of rehashes to resolve a clash.size
- Size of the cache. Must be a power of 2
.rehash
- Number of rehashes. Must be greater or equal to 0
and
smaller than 32 - numberOfTrailingZeros(size)
.weigher
- Needed to provide an estimation of the cache weight in memorypublic PriorityCache(int size, @NotNull @NotNull Weigher<K,V> weigher)
size
. The number of rehashes is
the maximum number allowed by the given size
. (31 - numberOfTrailingZeros(size)
.size
- Size of the cache. Must be a power of 2
.public PriorityCache(int size)
public static <K,V> Supplier<PriorityCache<K,V>> factory(int size, @NotNull @NotNull Weigher<K,V> weigher)
PriorityCache
instances.size
- size of the cache. Must be a power of 2.PriorityCache
instance of the given size
.public static <K,V> Supplier<PriorityCache<K,V>> factory(int size)
PriorityCache
instances.size
- size of the cache. Must be a power of 2.PriorityCache
instance of the given size
.public static long nextPowerOfTwo(int size)
size
up to the next power of two or 1 for negative values.size
- size
.public long size()
public boolean put(@NotNull K key, @NotNull V value, int generation, byte initialCost)
key
- the key of the mappingvalue
- the value of the mappinggeneration
- the generation of the mappinginitialCost
- the initial cost associated with this mappingtrue
if the mapping has been added, false
otherwise.@Nullable public V get(@NotNull K key, int generation)
key
and generation
.key
- key of the mapping to look upgeneration
- generation of the mapping to look upkey
and generation
or null
if this
cache does not contain such a mapping.public void purgeGenerations(@NotNull @NotNull Predicate<Integer> purge)
purge
predicate.purge
- @NotNull public @NotNull CacheStats getStats()
public long estimateCurrentWeight()
Copyright © 2012–2020 The Apache Software Foundation. All rights reserved.