Class WeakReferenceCache<K,V>

java.lang.Object
generic.cache.WeakReferenceCache<K,V>
Type Parameters:
K - The type of the object stored in this cache
V - The type of the object stored in this cache

public class WeakReferenceCache<K,V> extends Object
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

    Constructors
    Constructor
    Description
    WeakReferenceCache(int hardCacheSize)
    Constructs a new WeakReferenceCache with a given hard cache size.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(K key, V data)
    Adds the given database object to the cache.
    void
    apply(Consumer<V> consumer)
    Applies the given consumer to all values in the cache.
    delete(K key)
    Removes the object with the given key from the cache.
    void
    deleteIf(Predicate<V> predicate)
     
    get(K key)
    Retrieves the database object with the given key from the cache.
    Returns an List of all the cached objects.
    void
    setHardCacheSize(int size)
    Sets the number of objects to protect against garbage collection.
    int
    Returns the number of objects currently in the cache.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public V get(K key)
      Retrieves the database object with the given key from the cache.
      Parameters:
      key - the key of the object to retrieve.
      Returns:
      the cached object or null if the object with that key is not currently cached.
    • 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

      public V add(K key, V data)
      Adds the given database object to the cache.
      Parameters:
      key - the key for the cached object
      data - the object to add to the cache
      Returns:
      the object that has been added to the cache
    • getCachedObjects

      public List<V> getCachedObjects()
      Returns an List of all the cached objects.
      Returns:
      an List of all the cached objects.
    • apply

      public void apply(Consumer<V> consumer)
      Applies the given consumer to all values in the cache.
      Parameters:
      consumer - the consumer to apply to all values in the cache
    • delete

      public V delete(K key)
      Removes the object with the given key from the cache.
      Parameters:
      key - the key of the object to remove
      Returns:
      the value that was removed from the cache
    • deleteIf

      public void deleteIf(Predicate<V> predicate)