HybridCacheEntry
items than could even instruct the cache to drop them as they get computed.
The cache will still make sure such items get computed just once in given time (based on computation length)
for given key.
Desired value will only be computed once and computed value stored in the cache.
The implementation is based on an example from the "Java Concurrency in Practice" book
authored by Brian Goetz and company.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| public static interface | LRUHybridCache.
Key type. K>Should a cycle be detected during computation of a value for given key, this interface allows client code to register a callback that would get invoked in such a case. |
| private class | |
| private class | LRUHybridCache.
Helper class, that remembers the future task origin thread, so that cycles could be detected. |
| Modifier and Type | Field and Description |
|---|---|
| private final ConcurrentHashMap | |
| private static final Comparator | |
| private final Computable | |
| private final LRUHybridCache. | |
| private static final LRUHybridCache. | |
| private final int | |
| private final ReentrantLock |
| Access | Constructor and Description |
|---|---|
| public | LRUHybridCache(int
The maximum number of entries in the cache maxCacheSize, Computable<K, HybridCacheEntry<V>> The thing that can create the entry computable)Create new cache with given computable to compute values. |
| public | LRUHybridCache(int
The maximum number of entries in the cache maxCacheSize, Computable<K, HybridCacheEntry<V>> The thing that can create the entry computable, LRUHybridCache.What to do if a cycle is detected cycleHandlerCreate new cache with given computable and cycleHandler. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public HybridCacheEntry | compute(final K key)
Implements org. Defines an expensive computation to retrieve value V from key K. |
| public boolean | Returns: true if given key is present in the cache.Returns true if the key has already been cached. |
| public HybridCacheEntry | Returns: an instance of cache entry.cache entry key. k, final V cache entry value. v, final boolean should this entry be kept in the cache this must be set to false. dropMe)Create cache entry for given values. |
| public int | |
| public void | releaseMatching(CacheKeyFilter<K>
Entries in the cache that match this filter will
be removed from the cache. If filter is null nothing
will be removed from the cache filter)This method will remove all cache entries for which this filter matches |
| public void | |
| private void | |
| public int |
| cache | back to summary |
|---|---|
| private final ConcurrentHashMap<K, LRUHybridCache<K, V>. | |
| COMPARATOR | back to summary |
|---|---|
| private static final Comparator<LRUHybridCache. | |
| computable | back to summary |
|---|---|
| private final Computable<K, HybridCacheEntry<V>> computable | |
| cycleHandler | back to summary |
|---|---|
| private final LRUHybridCache. | |
| EMPTY_CYCLE_HANDLER | back to summary |
|---|---|
| private static final LRUHybridCache. | |
| maxCacheSize | back to summary |
|---|---|
| private final int maxCacheSize | |
| prunningLock | back to summary |
|---|---|
| private final ReentrantLock prunningLock | |
| LRUHybridCache | back to summary |
|---|---|
| public LRUHybridCache(int maxCacheSize, Computable<K, HybridCacheEntry<V>> computable) Create new cache with given computable to compute values.
| |
| LRUHybridCache | back to summary |
|---|---|
| public LRUHybridCache(int maxCacheSize, Computable<K, HybridCacheEntry<V>> computable, LRUHybridCache. Create new cache with given computable and cycleHandler.
| |
| clear | back to summary |
|---|---|
| public void clear() Empty the cache. | |
| compute | back to summary |
|---|---|
| public HybridCacheEntry Implements org. Doc from org. Defines an expensive computation to retrieve value V from key K.
| |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final K key) Returns true if the key has already been cached.
| |
| createCacheEntry | back to summary |
|---|---|
| public HybridCacheEntry Create cache entry for given values.
| |
| getMaximumCacheSize | back to summary |
|---|---|
| public int getMaximumCacheSize() | |
| releaseMatching | back to summary |
|---|---|
| public void releaseMatching(CacheKeyFilter<K> filter) This method will remove all cache entries for which this filter matches
| |
| remove | back to summary |
|---|---|
| public void remove(final K key) Remove given item from the cache.
| |
| removeLRUItem | back to summary |
|---|---|
| private void removeLRUItem() Remove least recently used item form the cache. No checks are done here. The method just tries to remove the least recently used cache item. An exception will be thrown if the cache is empty. | |
| size | back to summary |
|---|---|
| public int size() Return the size of the cache
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public int | compare(LRUHybridCache<K, V>.
Implements java. Compares its two arguments for order. |
| CacheEntryImplComparator | back to summary |
|---|---|
| private CacheEntryImplComparator() | |
| compare | back to summary |
|---|---|
| public int compare(LRUHybridCache<K, V>. Implements java. Doc from java. Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
The implementor must ensure that
The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |
| Modifier and Type | Method and Description |
|---|---|
| public void | handleCycle(K
instance that caused the cycle. key)Handle cycle that was detected while computing a cache value for given key. |
| handleCycle | back to summary |
|---|---|
| public void handleCycle(K key) Handle cycle that was detected while computing a cache value for given key. This method would typically just throw a runtime exception.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private final K | |
| private final V1 |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | dropMe()
Implements org. Tell the cache if this entry should be dropped as opposed to being kept in the cache. |
| public boolean | equals(Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public V1 | getValue()
Implements org. Getter for this cache entry internal value. |
| public int | |
| public void | removeFromCache()
Implements org. Call this method on this entry to remove it from the LRUCache. |
| dropMe | back to summary |
|---|---|
| private final boolean dropMe | |
| key | back to summary |
|---|---|
| private final K key | |
| value | back to summary |
|---|---|
| private final V1 value | |
| HybridCacheEntryImpl | back to summary |
|---|---|
| public HybridCacheEntryImpl(K key, V1 value, boolean dropMe) | |
| dropMe | back to summary |
|---|---|
| public boolean dropMe() Implements org. Doc from org. Tell the cache if this entry should be dropped as opposed to being kept in the cache.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| getValue | back to summary |
|---|---|
| public V1 getValue() Implements org. Doc from org. Getter for this cache entry internal value.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| removeFromCache | back to summary |
|---|---|
| public void removeFromCache() Implements org. Doc from org. Call this method on this entry to remove it from the LRUCache. If this entry has already been removed this method will do nothing.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final FutureTask | |
| private final K | |
| private volatile long | |
| private volatile long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | cancel(boolean mayInterruptIfRunning)
Implements java. Attempts to cancel execution of this task. |
| public boolean | equals(Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public HybridCacheEntry | get()
Implements java. Waits if necessary for the computation to complete, and then retrieves its result. |
| public HybridCacheEntry | get(long timeout, TimeUnit unit)
Implements java. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. |
| public int | |
| public boolean | isCancelled()
Implements java. Returns |
| public boolean | |
| public void |
| future | back to summary |
|---|---|
| private final FutureTask<HybridCacheEntry<V>> future | |
| key | back to summary |
|---|---|
| private final K key | |
| lastHit | back to summary |
|---|---|
| private volatile long lastHit | |
| threadId | back to summary |
|---|---|
| private volatile long threadId | |
| OriginThreadAwareFuture | back to summary |
|---|---|
| pack-priv OriginThreadAwareFuture(LRUHybridCache<K, HybridCacheEntry<V>> cache, final K key) | |
| cancel | back to summary |
|---|---|
| public boolean cancel(boolean mayInterruptIfRunning) Implements java. Doc from java. Attempts to cancel execution of this task. This method has no
effect if the task is already completed or cancelled, or could
not be cancelled for some other reason. Otherwise, if this
task has not started when The return value from this method does not necessarily
indicate whether the task is now cancelled; use
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| get | back to summary |
|---|---|
| public HybridCacheEntry Implements java. Doc from java. Waits if necessary for the computation to complete, and then retrieves its result.
| |
| get | back to summary |
|---|---|
| public HybridCacheEntry Implements java. Doc from java. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isCancelled | back to summary |
|---|---|
| public boolean isCancelled() Implements java. Doc from java. Returns
| |
| isDone | back to summary |
|---|---|
| public boolean isDone() Implements java. Doc from java. Returns
| |
| run | back to summary |
|---|---|
| public void run() | |