Class WeakReferenceCache<K,V>
java.lang.Object
generic.cache.WeakReferenceCache<K,V>
- Type Parameters:
K- The type of the object stored in this cacheV- The type of the object stored in this cache
Generic cache implementation that removes items from the cache when they are no longer
referenced. It uses weak references for the values and a small hard cache
so that recent items don't get garbage collected immediately.
This class is thread safe. All public methods are synchronized.
-
Constructor Summary
ConstructorsConstructorDescriptionWeakReferenceCache(int hardCacheSize) Constructs a new WeakReferenceCache with a given hard cache size. -
Method Summary
Modifier and TypeMethodDescriptionAdds the given database object to the cache.voidApplies the given consumer to all values in the cache.Removes the object with the given key from the cache.voidRetrieves the database object with the given key from the cache.Returns an List of all the cached objects.voidsetHardCacheSize(int size) Sets the number of objects to protect against garbage collection.intsize()Returns the number of objects currently in the cache.
-
Constructor Details
-
WeakReferenceCache
public WeakReferenceCache(int hardCacheSize) Constructs a new WeakReferenceCache with a given hard cache size. The hard cache size is the minimum number of objects to keep in the cache. Typically, the cache will contain more than this number, but the excess objects are subject to garbage collection.- Parameters:
hardCacheSize- the minimum number of objects to keep in the cache.
-
-
Method Details
-
get
-
size
public int size()Returns the number of objects currently in the cache.- Returns:
- the number of objects currently in the cache.
-
setHardCacheSize
public void setHardCacheSize(int size) Sets the number of objects to protect against garbage collection.- Parameters:
size- the minimum number of objects to keep in the cache.
-
add
-
getCachedObjects
-
apply
-
delete
-
deleteIf
-