K - key typeV - value typepublic class GuardedStrongRefLoaderRemoverCache<K,V,D> extends Object implements LoaderRemoverCache<K,V,D>
LoaderRemoverCache that is backed by a cache with strong references to
values. At the moment the backing cache is we use is
caffeine. We can easily
add Guava, cache2k, etc options later.
In addition we keep weak and phantom references to values. This ensures that we never lose track of values that are still in use elsewhere, although they have been evicted from the backing cache. Thus we never end up in a situation where two distinct values are associated to the same key (associated conceptually by the application, not by the cache map).
TODO: Consider periodically calling cleanUp() from a background
thread. Otherwise, freeing memory depends on the cache being regularly used.
(This is different for PhantomRefs than for Weak/SoftRefs. PhantomRefs must
be explicitly clear()ed before the referent is freed.)
defaultParallelismThreshold| Constructor and Description |
|---|
GuardedStrongRefLoaderRemoverCache(long maximumSize) |
| Modifier and Type | Method and Description |
|---|---|
void |
cleanUp()
Remove entries from the cache whose references have been
garbage-collected.
|
V |
get(K key,
CacheLoader<? super K,? extends V> loader,
CacheRemover<? super K,V,D> remover) |
V |
getIfPresent(K key) |
void |
invalidate(K key)
Removes and discards the entry with the specified
key. |
void |
invalidateAll(long parallelismThreshold)
Removes and discards all entries.
|
void |
invalidateIf(long parallelismThreshold,
Predicate<K> condition)
Removes and discards all entries with keys matching
condition. |
void |
persist(K key)
TODO
Persist entry through any backing cache end eventually a CacheRemover.
|
void |
persistAll()
TODO
|
void |
persistIf(Predicate<K> condition)
TODO
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitmapKeys, withLoader, withRemoverinvalidateAll, invalidateIfpublic GuardedStrongRefLoaderRemoverCache(long maximumSize)
public V getIfPresent(K key)
getIfPresent in interface AbstractCache<K,V>public V get(K key, CacheLoader<? super K,? extends V> loader, CacheRemover<? super K,V,D> remover) throws ExecutionException
get in interface LoaderRemoverCache<K,V,D>ExecutionExceptionpublic void persist(K key)
AbstractCachekey.persist in interface AbstractCache<K,V>public void persistIf(Predicate<K> condition)
AbstractCachepersistIf in interface AbstractCache<K,V>condition - condition on keys of entries to persistpublic void persistAll()
AbstractCachepersistAll in interface AbstractCache<K,V>public void invalidate(K key)
AbstractCachekey.
Note that this will not call
CacheRemover.onRemoval(Object, Object) for the discarded entry.
If this cache has a CacheRemover, calls CacheRemover.invalidate(Object) for the discarded
entry, (which should in turn remove it from any backing cache).
Note that this applies only when the whole cache has a CacheRemover,
instead of each individual entry as in LoaderRemoverCache.
There must be no concurrent get() operations for key.
This may result in cache corruption and/or a deadlock.
invalidate in interface AbstractCache<K,V>invalidate in interface Invalidate<K>key - key of the entry to removepublic void invalidateIf(long parallelismThreshold,
Predicate<K> condition)
AbstractCachecondition.
Note that this will not call
CacheRemover.onRemoval(Object, Object) for the discarded entries.
If this cache has a CacheRemover, calls Invalidate.invalidateIf(Predicate) for the discarded
entries, (which should in turn remove them from any backing cache).
Note that this applies only when the whole cache has a CacheRemover,
instead of each individual entry as in LoaderRemoverCache.
There must be no concurrent get() operations for keys
matching condition. This may result in cache corruption and/or a
deadlock.
invalidateIf in interface AbstractCache<K,V>invalidateIf in interface Invalidate<K>parallelismThreshold - the (estimated) number of entries in the cache needed for this
operation to be executed in parallelcondition - condition on keys of entries to removepublic void invalidateAll(long parallelismThreshold)
AbstractCache
Note that this will not call
CacheRemover.onRemoval(Object, Object) for the discarded entries.
If this cache has a CacheRemover, calls CacheRemover.invalidateAll(long)
(which should in turn invalidate any backing cache).
Note that this applies only when the whole cache has a CacheRemover,
instead of each individual entry as in LoaderRemoverCache.
There must be no concurrent get() operations. This may
result in cache corruption and/or a deadlock.
invalidateAll in interface AbstractCache<K,V>invalidateAll in interface Invalidate<K>parallelismThreshold - the (estimated) number of entries in the cache needed for this
operation to be executed in parallelpublic void cleanUp()
CacheRemover (specified in
get(Object, CacheLoader, CacheRemover)), is notified for each
removed entry.Copyright © 2015–2022 ImgLib2. All rights reserved.