Class WeakCARCacheImpl<K,V>
java.lang.Object
org.glassfish.hk2.utilities.cache.internal.WeakCARCacheImpl<K,V>
- All Implemented Interfaces:
WeakCARCache<K,
V>
Implements the CAR algorithm as found here:
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.105.6057
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final WeakHashLRU
<K> private final WeakHashLRU
<K> private final Computable
<K, V> private final AtomicLong
private final int
private int
private final WeakHashClock
<K, WeakCARCacheImpl.CarValue<V>> private final WeakHashClock
<K, WeakCARCacheImpl.CarValue<V>> private final AtomicLong
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the current cache, making the current size zerovoid
Causes stale references to be cleared from the data structures.The method used to get or add values to this cacheReturns a string that will contain all the elements of the four listsint
Returns the number of items in the B1 LRUint
Returns the number of items in the B2 LRUComputable
<K, V> The computable associated with this cachedouble
Returns the hit rate from the last time clear was calledint
Returns the current number of keys in the cache.int
Gets the current maximum size of the cache (the maximum number of values that will be kept by the cache).int
getP()
Returns the value of p from the CAR algorithm, which is the target size of the t1 clockint
Returns the number of items in the T1 clockint
Returns the number of items in the T2 clockprivate V
getValueFromT
(K key) int
Returns the current number of values in the cache.void
releaseMatching
(CacheKeyFilter<K> filter) Releases all key/value pairs that match the filterboolean
Used to remove a single key and value from the cache (if the value is available)private void
replace()
toString()
-
Field Details
-
computable
-
maxSize
private final int maxSize -
t1
-
t2
-
b1
-
b2
-
p
private int p -
hits
-
tries
-
-
Constructor Details
-
WeakCARCacheImpl
-
-
Method Details
-
getValueFromT
-
compute
Description copied from interface:WeakCARCache
The method used to get or add values to this cache- Specified by:
compute
in interfaceWeakCARCache<K,
V> - Parameters:
key
- The key to add to the cache. If the value is not found, then the computable will be called to get the value. May not be null- Returns:
- The calculated return value. May not be null
-
replace
private void replace() -
getKeySize
public int getKeySize()Description copied from interface:WeakCARCache
Returns the current number of keys in the cache. Note that the number of keys can be up to 2x the maximum size of the cache- Specified by:
getKeySize
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of key entries in the cache
-
getValueSize
public int getValueSize()Description copied from interface:WeakCARCache
Returns the current number of values in the cache. Note that the number of values can be up the maximum size of the cache- Specified by:
getValueSize
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of value entries in the cache
-
clear
public void clear()Description copied from interface:WeakCARCache
Clears the current cache, making the current size zero- Specified by:
clear
in interfaceWeakCARCache<K,
V>
-
getMaxSize
public int getMaxSize()Description copied from interface:WeakCARCache
Gets the current maximum size of the cache (the maximum number of values that will be kept by the cache). Note that the number of keys kept will be 2x, where x is the maximum size of the cache (see CAR algorithm which keeps a key history)- Specified by:
getMaxSize
in interfaceWeakCARCache<K,
V> - Returns:
- The maximum size of the cache
-
getComputable
Description copied from interface:WeakCARCache
The computable associated with this cache- Specified by:
getComputable
in interfaceWeakCARCache<K,
V> - Returns:
- The computable associated with this cache
-
remove
Description copied from interface:WeakCARCache
Used to remove a single key and value from the cache (if the value is available)- Specified by:
remove
in interfaceWeakCARCache<K,
V> - Parameters:
key
- The key to remove. May not be null- Returns:
- true if a key was found and removed
-
releaseMatching
Description copied from interface:WeakCARCache
Releases all key/value pairs that match the filter- Specified by:
releaseMatching
in interfaceWeakCARCache<K,
V> - Parameters:
filter
- A non-null filter that can be used to delete every key/value pair that matches the filter
-
clearStaleReferences
public void clearStaleReferences()Description copied from interface:WeakCARCache
Causes stale references to be cleared from the data structures. Since this is a weak cache the references can go away at any time, which happens whenever any operation has been performed. However, it may be the case that no operation will be performed for a while and so this method is provided to have a no-op operation to call in order to clear out any stale references- Specified by:
clearStaleReferences
in interfaceWeakCARCache<K,
V>
-
getT1Size
public int getT1Size()Description copied from interface:WeakCARCache
Returns the number of items in the T1 clock- Specified by:
getT1Size
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of items in the T1 clock
-
getT2Size
public int getT2Size()Description copied from interface:WeakCARCache
Returns the number of items in the T2 clock- Specified by:
getT2Size
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of items in the T2 clock
-
getB1Size
public int getB1Size()Description copied from interface:WeakCARCache
Returns the number of items in the B1 LRU- Specified by:
getB1Size
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of items in the B1 LRU
-
getB2Size
public int getB2Size()Description copied from interface:WeakCARCache
Returns the number of items in the B2 LRU- Specified by:
getB2Size
in interfaceWeakCARCache<K,
V> - Returns:
- The current number of items in the B2 LRU
-
getP
public int getP()Description copied from interface:WeakCARCache
Returns the value of p from the CAR algorithm, which is the target size of the t1 clock- Specified by:
getP
in interfaceWeakCARCache<K,
V> - Returns:
- The current value of P
-
dumpAllLists
Description copied from interface:WeakCARCache
Returns a string that will contain all the elements of the four lists- Specified by:
dumpAllLists
in interfaceWeakCARCache<K,
V> - Returns:
- A String containing the values of T1, T2, B1 and B2
-
getHitRate
public double getHitRate()Description copied from interface:WeakCARCache
Returns the hit rate from the last time clear was called- Specified by:
getHitRate
in interfaceWeakCARCache<K,
V> - Returns:
- The Hit rate from the last time clear was called or 0 if there is no data
-
toString
-