Top Description Inners Fields Constructors Methods
org.glassfish.hk2.utilities.cache.internal

public Class LRUCacheCheapRead<K, V>

extends LRUCache<K, V>
Class Inheritance
Type Parameters
<K>
The key of the cache
<V>
The values in the cache
Imports
java.util.Collection, .Collections, .Comparator, .HashMap, .Map, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.locks.ReentrantLock, org.glassfish.hk2.utilities.cache.CacheEntry, .CacheKeyFilter, .LRUCache

LRU Cache implementation that relies on entries that keep last hit (get/put) timestamp in order to be able to remove least recently accessed items when running out of cache capacity. Item order is not being maintained during regular cache usage (mainly reads). This makes pruning operation expensive in exchange for making reads quite cheap in a multi-threaded environment.
Author
Jakub Podlesak (jakub.podlesak at oracle.com)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
pack-priv Map<K, LRUCacheCheapRead.CacheEntryImpl<K, V>>
private static final LRUCacheCheapRead.CacheEntryImplComparator
pack-priv final int
pack-priv final ReentrantLock

Constructor Summary

AccessConstructor and Description
public
LRUCacheCheapRead(int
Maximum number of items to keep.
maxCacheSize
)

Create new cache with given maximum capacity.

Method Summary

Modifier and TypeMethod and Description
public V
get(K key)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.get.

Returns the value associated with the given key.

public int
getMaxCacheSize()

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.getMaxCacheSize.

Returns the maximum number of entries that will be stored in this cache

public CacheEntry
put(K key, V value)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.put.

Adds the given key and value pair into the cache

public void
releaseCache()

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.releaseCache.

Clears all entries in the cache, for use when a known event makes the cache incorrect

public void
releaseMatching(CacheKeyFilter<K> filter)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.releaseMatching.

This method will remove all cache entries for which this filter matches

private void
removeLRUItem()

Remove least recently used item form the cache.

Inherited from org.glassfish.hk2.utilities.cache.LRUCache:
createCache

Field Detail

cacheback to summary
pack-priv Map<K, LRUCacheCheapRead.CacheEntryImpl<K, V>> cache
COMPARATORback to summary
private static final LRUCacheCheapRead.CacheEntryImplComparator COMPARATOR
maxCacheSizeback to summary
pack-priv final int maxCacheSize
prunningLockback to summary
pack-priv final ReentrantLock prunningLock

Constructor Detail

LRUCacheCheapReadback to summary
public LRUCacheCheapRead(int maxCacheSize)

Create new cache with given maximum capacity.

Parameters
maxCacheSize:int

Maximum number of items to keep.

Method Detail

getback to summary
public V get(K key)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.get.

Doc from org.glassfish.hk2.utilities.cache.LRUCache.get.

Returns the value associated with the given key. If there is no value, returns null

Annotations
@Override
getMaxCacheSizeback to summary
public int getMaxCacheSize()

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.getMaxCacheSize.

Doc from org.glassfish.hk2.utilities.cache.LRUCache.getMaxCacheSize.

Returns the maximum number of entries that will be stored in this cache

Annotations
@Override
putback to summary
public CacheEntry put(K key, V value)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.put.

Doc from org.glassfish.hk2.utilities.cache.LRUCache.put.

Adds the given key and value pair into the cache

Annotations
@Override
releaseCacheback to summary
public void releaseCache()

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.releaseCache.

Doc from org.glassfish.hk2.utilities.cache.LRUCache.releaseCache.

Clears all entries in the cache, for use when a known event makes the cache incorrect

Annotations
@Override
releaseMatchingback to summary
public void releaseMatching(CacheKeyFilter<K> filter)

Implements abstract org.glassfish.hk2.utilities.cache.LRUCache.releaseMatching.

Doc from org.glassfish.hk2.utilities.cache.LRUCache.releaseMatching.

This method will remove all cache entries for which this filter matches

Annotations
@Override
removeLRUItemback 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.

org.glassfish.hk2.utilities.cache.internal back to summary

private Class LRUCacheCheapRead.CacheEntryImpl<K, V>

extends Object
implements CacheEntry
Class Inheritance
  • java.lang.Object
  • org.glassfish.hk2.utilities.cache.internal.LRUCacheCheapRead.CacheEntryImpl
All Implemented Interfaces
org.glassfish.hk2.utilities.cache.CacheEntry

Field Summary

Modifier and TypeField and Description
pack-priv final K
pack-priv long
pack-priv final LRUCacheCheapRead<K, V>
pack-priv final V

Constructor Summary

AccessConstructor and Description
public
CacheEntryImpl(K k, V v, LRUCacheCheapRead<K, V> cache)

Method Summary

Modifier and TypeMethod and Description
public LRUCacheCheapRead.CacheEntryImpl<K, V>
hit()

public void
removeFromCache()

Implements org.glassfish.hk2.utilities.cache.CacheEntry.removeFromCache.

Call this method on this entry to remove it from the LRUCache.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
pack-priv final K key
lastHitback to summary
pack-priv long lastHit
parentback to summary
pack-priv final LRUCacheCheapRead<K, V> parent
valueback to summary
pack-priv final V value

Constructor Detail

CacheEntryImplback to summary
public CacheEntryImpl(K k, V v, LRUCacheCheapRead<K, V> cache)

Method Detail

hitback to summary
public LRUCacheCheapRead.CacheEntryImpl<K, V> hit()
removeFromCacheback to summary
public void removeFromCache()

Implements org.glassfish.hk2.utilities.cache.CacheEntry.removeFromCache.

Doc from org.glassfish.hk2.utilities.cache.CacheEntry.removeFromCache.

Call this method on this entry to remove it from the LRUCache. If this entry has already been removed this method will do nothing.

Annotations
@Override
org.glassfish.hk2.utilities.cache.internal back to summary

private Class LRUCacheCheapRead.CacheEntryImplComparator

extends Object
implements Comparator<LRUCacheCheapRead.CacheEntryImpl<?, ?>>
Class Inheritance
  • java.lang.Object
  • org.glassfish.hk2.utilities.cache.internal.LRUCacheCheapRead.CacheEntryImplComparator
All Implemented Interfaces
java.util.Comparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(LRUCacheCheapRead.CacheEntryImpl<?, ?> first, LRUCacheCheapRead.CacheEntryImpl<?, ?> second)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CacheEntryImplComparatorback to summary
private CacheEntryImplComparator()

Method Detail

compareback to summary
public int compare(LRUCacheCheapRead.CacheEntryImpl<?, ?> first, LRUCacheCheapRead.CacheEntryImpl<?, ?> second)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

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 signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Annotations
@Override