Class LRUCacheWithFuture<K,V>

java.lang.Object
tigase.pubsub.utils.LRUCacheWithFuture<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
Cache<K,V>, tigase.stats.StatisticHolder

public class LRUCacheWithFuture<K,V> extends Object implements Cache<K,V>
This class is implementation of Cache interface. The main difference between LRUCache and LRUCacheWithFuture is LRUCache implementation of computeIfAbsent(K key, CacheSupplier). In case when there is no value, LRUCache executes provided instance of CacheSupplier in a synchronized block resulting in blocking of all access to the cache while result to "put" in being generated. On the other hand, LRUCacheWithFuture, when there is no result, puts instance of a CompletableFuture in the cache (instance of LRUCache) as a value and then waits for the completion of the CacheSupplier to return the value. In this implementation, long process of generating value by CacheSupplier is not blocking access to the cache for other "keys". Parallel requests to get value for the same key will be "lock" and CacheSupplier will be called only once if the value is not in the cache.
  • Constructor Details

    • LRUCacheWithFuture

      public LRUCacheWithFuture()
    • LRUCacheWithFuture

      public LRUCacheWithFuture(int maxSize)
  • Method Details

    • computeIfAbsent

      public V computeIfAbsent(K key, Cache.CacheSupplier<V> supplier) throws Cache.CacheException
      Specified by:
      computeIfAbsent in interface Cache<K,V>
      Throws:
      Cache.CacheException
    • get

      public V get(K key)
      Specified by:
      get in interface Cache<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Cache<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface Cache<K,V>
    • remove

      public V remove(K key)
      Specified by:
      remove in interface Cache<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Cache<K,V>
    • values

      public Stream<V> values()
      Specified by:
      values in interface Cache<K,V>
    • size

      public int size()
      Specified by:
      size in interface Cache<K,V>
    • setMaxSize

      public void setMaxSize(int size)
      Specified by:
      setMaxSize in interface Cache<K,V>
    • everyHour

      public void everyHour()
      Specified by:
      everyHour in interface tigase.stats.StatisticHolder
    • everyMinute

      public void everyMinute()
      Specified by:
      everyMinute in interface tigase.stats.StatisticHolder
    • everySecond

      public void everySecond()
      Specified by:
      everySecond in interface tigase.stats.StatisticHolder
    • getStatistics

      public void getStatistics(String compName, tigase.stats.StatisticsList list)
      Specified by:
      getStatistics in interface tigase.stats.StatisticHolder
    • setStatisticsPrefix

      public void setStatisticsPrefix(String prefix)
      Specified by:
      setStatisticsPrefix in interface tigase.stats.StatisticHolder
    • statisticExecutedIn

      public void statisticExecutedIn(long executionTime)
      Specified by:
      statisticExecutedIn in interface tigase.stats.StatisticHolder