Class AsyncClaimingCache3Impl<K,V>

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

public class AsyncClaimingCache3Impl<K,V> extends Object implements AsyncClaimingCache<K,V>
An extension of loading cache that allows for making explicit references to cached entries such that they won't be evicted. Internally 3 levels of caching are used: level1: This is the map of claimed items. Unclaimed items are moved to level2. level2: A caffeine cache that acts as the sync queue. After a configurable amount of time a custom action is applied and the items are moven to level3. level3: A caffeine cache that acts as a conventional cache. Items evicted from level2 are added here. Uses 'smart' references. The cache: Upon load, the cache first checks whether for the key there is already a claimed reference. Upon load it references its own item, and releases this reference upon removal. Claiming: Claiming an item acquires an additional reference and adds that reference to the claimed map. When a claimed item is no longer referenced, the item is put back to cache.
Author:
raven
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Map<K,RefFuture<V>>
    The map of claimed items; the value is a reference to the RefFuture of level3 Once the outer reference gets closed (i.e.
    protected com.github.benmanes.caffeine.cache.Cache<K,SingleValuedAccessor<CompletableFuture<V>>>
    Items that are no longer claimed are added to level2 from where after a delay - they are atomically evicted from level2 and added to level3.
    protected com.github.benmanes.caffeine.cache.RemovalListener<K,V>
    Whether to cancel loading of items that were unclaimed before loading completed, if false, the future returned by level3 will not be cancelled
    protected com.github.benmanes.caffeine.cache.AsyncLoadingCache<K,V>
    The cache with the primary loader
  • Constructor Summary

    Constructors
    Constructor
    Description
    AsyncClaimingCache3Impl(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> level3Master, Function<K,V> level3CacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level3RemovalListener, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level2RemovalListener, Duration syncDelayDuration)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    addEvictionGuard(Predicate<? super K> predicate)
    Protect eviction of certain keys as long as the guard is not disposed.
    claim(K key)
    Claim a reference to the key's entry.
    Cannot raise an ExecutionException because it does not trigger loading
     
    static <K, V> AsyncClaimingCache3Impl<K,V>
    create(Duration syncDelayDuration, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> level3Master, Function<K,V> level3CacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level3RemovalListener, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level2RemovalListener)
     
    hideInnerRef(Ref<? extends Ref<V>> refToRef)
     
    static <V> Ref<V>
    hideInnerRef(Ref<? extends Ref<V>> refToRef, Object synchronizer)
     
    void
    Invalidates all items in level3 Items that are currently referenced (level 1) or scheduled for sync (level 2) are unaffected.
    void
    sync(K key)
    Attempts to run the level2 removal action on the entry with the given key.
    void
     

    Methods inherited from class java.lang.Object

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

    • level1

      protected Map<K,RefFuture<V>> level1
      The map of claimed items; the value is a reference to the RefFuture of level3 Once the outer reference gets closed (i.e. when there are no more claims), the value is immediately added to level2.
    • level2

      protected com.github.benmanes.caffeine.cache.Cache<K,SingleValuedAccessor<CompletableFuture<V>>> level2
      Items that are no longer claimed are added to level2 from where after a delay - they are atomically evicted from level2 and added to level3. The SingleValuedAccessor wrapper acts as a 'holder' which allows 'resurrecting' items from level2 into level1 without level2's eviction action to act due to setting the held reference to null.
    • level3

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

      protected com.github.benmanes.caffeine.cache.RemovalListener<K,V> level2RemovalListener
      Whether to cancel loading of items that were unclaimed before loading completed, if false, the future returned by level3 will not be cancelled
  • Constructor Details

    • AsyncClaimingCache3Impl

      public AsyncClaimingCache3Impl(com.github.benmanes.caffeine.cache.Caffeine<Object,Object> level3Master, Function<K,V> level3CacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level3RemovalListener, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level2RemovalListener, Duration syncDelayDuration)
  • Method Details

    • create

      public static <K, V> AsyncClaimingCache3Impl<K,V> create(Duration syncDelayDuration, com.github.benmanes.caffeine.cache.Caffeine<Object,Object> level3Master, Function<K,V> level3CacheLoader, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level3RemovalListener, com.github.benmanes.caffeine.cache.RemovalListener<K,V> level2RemovalListener)
    • sync

      public void sync(K key)
      Attempts to run the level2 removal action on the entry with the given key. The action is only run under the following conditions:
      • If the entry is in level1 and the future is loaded
      • If the entry is in level2 then the entry is simply evicted which implicitly runs the action (and adds the entry to level3)
      Parameters:
      key -
    • syncAll

      public void syncAll()
    • claimUnsafe

      public RefFuture<V> claimUnsafe(K key)
    • claim

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

      public RefFuture<V> claimIfPresent(K key)
      Cannot raise an ExecutionException because it does not trigger loading
      Specified by:
      claimIfPresent in interface AsyncClaimingCache<K,V>
    • invalidateAll

      public void invalidateAll()
      Invalidates all items in level3 Items that are currently referenced (level 1) or scheduled for sync (level 2) are unaffected.
      Specified by:
      invalidateAll in interface AsyncClaimingCache<K,V>
    • hideInnerRef

      public static <V> Ref<V> hideInnerRef(Ref<? extends Ref<V>> refToRef, Object synchronizer)
    • hideInnerRef

      public Ref<V> hideInnerRef(Ref<? extends Ref<V>> refToRef)
    • addEvictionGuard

      public Disposable addEvictionGuard(Predicate<? super K> predicate)
      Description copied from interface: AsyncClaimingCache
      Protect eviction of certain keys as long as the guard is not disposed. Disposable may immediately evict all no longer guarded items
      Specified by:
      addEvictionGuard in interface AsyncClaimingCache<K,V>