Class AsyncRefCache<K,V>

java.lang.Object
org.aksw.commons.cache.async.AsyncRefCache<K,V>
Type Parameters:
K -
V -

public class AsyncRefCache<K,V> extends Object
A wrapper around a cache that on each lookup returns a fresh CompletableFuture that can be cancelled independently. Only if all 'slave' futures are cancelled then the 'master' future is cancelled. Once a master's future is loaded then the slave futures will close themselves as the loading can no longer be interrupted.
Author:
raven
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<K,V>
    The cache with the primary loader
    protected Map<K, RefFuture<V>>
    A 'slave' map that wraps the completable futures of the master as a ref The slave is synchronized with the master - it atomically contains the same keys.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AsyncRefCache(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> cacheBuilder, Function<K,V> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> removalListener)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K,V> AsyncRefCache<K,V>
    create(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> master, Function<K,V> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> removalListener)
    Whether to cancel loading of items that were unclaimed before loading completed, if false, the future returned by the master will not be cancelled
    Get a fresh complatable future to the key.
    Get a fresh reference to the item corresponding to 'key'.
     
    void
     
    static void
    main(String[] args)
     
    void
    put(K key, RefFuture<V> value)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • master

      protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<K,V> master
      The cache with the primary loader
    • slave

      protected Map<K, RefFuture<V>> slave
      A 'slave' map that wraps the completable futures of the master as a ref The slave is synchronized with the master - it atomically contains the same keys.
  • Constructor Details

    • AsyncRefCache

      public AsyncRefCache(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> cacheBuilder, Function<K,V> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> removalListener)
  • Method Details

    • create

      public static <K,V> AsyncRefCache<K,V> create(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> master, Function<K,V> cacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> removalListener)
      Whether to cancel loading of items that were unclaimed before loading completed, if false, the future returned by the master will not be cancelled
    • getAsCompletableFuture

      public CompletableFuture<V> getAsCompletableFuture(K key)
      Get a fresh complatable future to the key. Cannot be used if a close action on items is configured, because completion of the future would close the item.
    • put

      public void put(K key, RefFuture<V> value)
    • getAsRefFuture

      public RefFuture<V> getAsRefFuture(K key)
      Get a fresh reference to the item corresponding to 'key'.
      Parameters:
      key -
      Returns:
    • main

      public static void main(String[] args) throws InterruptedException, ExecutionException
      Throws:
      InterruptedException
      ExecutionException
    • getIfPresent

      public RefFuture<V> getIfPresent(K key)
    • invalidateAll

      public void invalidateAll()