Class AbstractCache<K,V>

java.lang.Object
com.sodius.oslc.core.cache.AbstractCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values
All Implemented Interfaces:
Cache<K,V>

public abstract class AbstractCache<K,V> extends Object implements Cache<K,V>
A skeletal implementation of Cache. This class is to extend when creating builders of cluster compatible caches. Local caches are built with CacheBuilder.local(), one should not try implementing a local cache by any other mean.
Since:
3.7.0
  • Constructor Details

    • AbstractCache

      public AbstractCache()
  • Method Details

    • get

      public V get(K key, Function<? super K,? extends V> loader)
      Returns the value associated with the key in this cache, obtaining that value from the loader if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.

      If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this cache. The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this cache by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this cache.

      Warning: loader must not attempt to update any other mappings of this cache.

      Warning: loader must not return null; it may either return a non-null value or throw an exception.

      Important Note: contrary to the contract of the overridden method, this implementation does not guarantee the loader is applied at most once per key.

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - the key whose associated value is to be returned
      loader - the function to compute a value
      Returns:
      the current (existing or computed) value associated with the specified key
    • scheduleCleanUp

      protected void scheduleCleanUp()
      Schedules a call to the Cache.cleanUp() method, in a separate thread. Threads are created using the factory provided by Threads.getThreadFactory().

      Subclasses shall call this method in the implementation of Cache.put(Object, Object) and Cache.invalidate(Object), so that expired entries are removed.