Class AsyncClaimingCacheImpl<K,V>

java.lang.Object
org.aksw.commons.cache.async.AsyncClaimingCacheImpl<K,V>
Type Parameters:
K -
V -
All Implemented Interfaces:
AsyncClaimingCache<K,V>

public class AsyncClaimingCacheImpl<K,V> extends Object implements AsyncClaimingCache<K,V>
Implementation of async claiming cache. Claimed entries will never be evicted. Conversely, unclaimed items remain are added to a cache such that timely re-claiming will be fast. Use cases: - Resource sharing: Ensure that the same resource is handed to all clients requesting one by key. - Resource pooling: Claimed resources will never be closed, but unclaimed resources (e.g. something backed by an input stream) may remain on standby for a while. Another way to view this class is as a mix of a map with weak values and a cache.
Author:
raven
  • Field Details

    • level1

      protected Map<K, RefFuture<V>> level1
    • level2

      protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<K,V> level2
    • level3

      protected Map<K,V> level3
    • claimListener

      protected BiConsumer<K, RefFuture<V>> claimListener
    • unclaimListener

      protected BiConsumer<K, RefFuture<V>> unclaimListener
    • invalidationLock

      protected ReentrantReadWriteLock invalidationLock
    • evictionGuards

      protected LinkedList<Predicate<? super K>> evictionGuards
    • evictionListener

      protected com.github.benmanes.caffeine.cache.RemovalListener<K,V> evictionListener
    • keyToSynchronizer

      protected Map<K, org.aksw.commons.cache.async.AsyncClaimingCacheImpl.Latch> keyToSynchronizer
  • Constructor Details

  • Method Details

    • addEvictionGuard

      public Disposable addEvictionGuard(Predicate<? super K> predicate)
      Registers a predicate that 'caches' entries about to be evicted When closing the registration then keys that have not moved back into the ache by reference will be immediately evicted.
      Specified by:
      addEvictionGuard in interface AsyncClaimingCache<K,V>
    • runLevel3Eviction

      protected void runLevel3Eviction()
    • claim

      public RefFuture<V> claim(K key)
      Description copied from interface: AsyncClaimingCache
      Claim a reference to the key's entry.
      Specified by:
      claim in interface AsyncClaimingCache<K,V>
      Parameters:
      key -
      Returns:
    • newBuilder

      public static <K,V> AsyncClaimingCacheImpl.Builder<K,V> newBuilder(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> caffeine)
    • claimIfPresent

      public RefFuture<V> claimIfPresent(K key)
      Claim a key only if it is already present. This implementation is a best effort approach: There is a very slim chance that just between testing a key for presence and claiming its entry an eviction occurs - causing claiming of a non-present key and thus triggering a load action.
      Specified by:
      claimIfPresent in interface AsyncClaimingCache<K,V>
    • invalidateAll

      public void invalidateAll()
      Specified by:
      invalidateAll in interface AsyncClaimingCache<K,V>