Package org.aksw.commons.cache.async
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>
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
FieldsModifier and TypeFieldDescriptionThe 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.Whether to cancel loading of items that were unclaimed before loading completed, if false, the future returned by level3 will not be cancelledThe cache with the primary loader -
Constructor Summary
ConstructorsConstructorDescriptionAsyncClaimingCache3Impl(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 TypeMethodDescriptionaddEvictionGuard(Predicate<? super K> predicate) Protect eviction of certain keys as long as the guard is not disposed.Claim a reference to the key's entry.claimIfPresent(K key) Cannot raise an ExecutionException because it does not trigger loadingclaimUnsafe(K key) 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) voidInvalidates all items in level3 Items that are currently referenced (level 1) or scheduled for sync (level 2) are unaffected.voidAttempts to run the level2 removal action on the entry with the given key.voidsyncAll()
-
Field Details
-
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>>> level2Items 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
The cache with the primary loader -
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
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
-
claim
Claim a reference to the key's entry.- Specified by:
claimin interfaceAsyncClaimingCache<K,V> - Parameters:
key-- Returns:
- Throws:
ExecutionException
-
claimIfPresent
Cannot raise an ExecutionException because it does not trigger loading- Specified by:
claimIfPresentin interfaceAsyncClaimingCache<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:
invalidateAllin interfaceAsyncClaimingCache<K,V>
-
hideInnerRef
-
hideInnerRef
-
addEvictionGuard
Description copied from interface:AsyncClaimingCacheProtect eviction of certain keys as long as the guard is not disposed. Disposable may immediately evict all no longer guarded items- Specified by:
addEvictionGuardin interfaceAsyncClaimingCache<K,V>
-