Class SoftCache<K,V>
- java.lang.Object
-
- com.palantir.util.MBeanCache<K,V>
-
- com.palantir.util.SoftCache<K,V>
-
- All Implemented Interfaces:
DistributedCacheMgrCache<K,V>
,NonDistributedCache<K,V>
public class SoftCache<K,V> extends MBeanCache<K,V>
Thread Safe
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
SoftCache.CacheEntry<V>
CacheEntry is a cache entry that stores its value as a soft reference.protected static class
SoftCache.SoftCacheEntry<K,V>
protected static class
SoftCache.WeakCacheEntry<K,V>
-
Field Summary
Fields Modifier and Type Field Description protected Map<K,SoftCache.CacheEntry<V>>
cacheEntries
static String
OBJECT_NAME_PREFIX
Deprecated.protected ReferenceQueue<V>
referenceQueue
-
Fields inherited from class com.palantir.util.MBeanCache
mbean
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
canonicalize(Map<K,V> returnVal)
This convenience method takes a map of items returned from the backing store and replaces references loaded from the backing store with items in the cache.void
cleanup()
Iterates through the cache and cleans up any cache references that have been collected by the garbage collector.void
clear()
Clears all entries from the cache.boolean
containsKey(K key)
boolean
containsValue(V val)
protected Map<K,SoftCache.CacheEntry<V>>
createCache(int initialSize)
This method should be over-ridden by subclasses to change the underlying cache implementation and implement features like LRU caches...Collection<V>
filter(Collection<K> request)
This convenience method filters a request by removing all items in the request which are in the cache and returning the corresponding values.V
get(K key)
Gets an object from the cache.int
getMaxCacheSize()
Set<K>
keySet()
protected SoftCache.SoftCacheEntry<K,V>
newSoftCacheEntry(K key, V value)
protected SoftCache.WeakCacheEntry<K,V>
newWeakCacheEntry(K key, V value)
V
put(K key, V value)
Adds an object to the cache.void
putAll(Map<? extends K,? extends V> map)
void
putAllIfAbsent(Map<? extends K,? extends V> map)
void
putAllIfAbsent(Map<? extends K,? extends V> map, long loadTimeInMillis)
V
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value.V
remove(K key)
Removes an object from the cache.Set<V>
removeMatchingKeys(com.google.common.base.Predicate<K> predicate)
void
setMaxCacheSize(int size)
int
size()
String
toString()
-
Methods inherited from class com.palantir.util.MBeanCache
collectStatsLoadTimeForCacheKey, collectStatsLoadTimeForMiss, getName, getStatBeanName, registerMBean, registerMBean, setName
-
-
-
-
Field Detail
-
cacheEntries
protected final Map<K,SoftCache.CacheEntry<V>> cacheEntries
-
OBJECT_NAME_PREFIX
@Deprecated public static final String OBJECT_NAME_PREFIX
Deprecated.- See Also:
- Constant Field Values
-
referenceQueue
protected final ReferenceQueue<V> referenceQueue
-
-
Method Detail
-
getMaxCacheSize
public int getMaxCacheSize()
- Specified by:
getMaxCacheSize
in classMBeanCache<K,V>
-
setMaxCacheSize
public void setMaxCacheSize(int size)
- Specified by:
setMaxCacheSize
in classMBeanCache<K,V>
-
createCache
protected Map<K,SoftCache.CacheEntry<V>> createCache(int initialSize)
This method should be over-ridden by subclasses to change the underlying cache implementation and implement features like LRU caches...
-
containsKey
public boolean containsKey(K key)
-
containsValue
public boolean containsValue(V val)
-
putIfAbsent
public V putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value. This is equivalent to- Returns:
- The value that was in the cache, null if none was there before
-
putAllIfAbsent
public void putAllIfAbsent(Map<? extends K,? extends V> map, long loadTimeInMillis)
-
size
public int size()
- Specified by:
size
in interfaceNonDistributedCache<K,V>
- Specified by:
size
in classMBeanCache<K,V>
-
clear
public void clear()
Clears all entries from the cache.- Specified by:
clear
in interfaceNonDistributedCache<K,V>
- Specified by:
clear
in classMBeanCache<K,V>
-
cleanup
public final void cleanup()
Iterates through the cache and cleans up any cache references that have been collected by the garbage collector.
-
filter
public Collection<V> filter(Collection<K> request)
This convenience method filters a request by removing all items in the request which are in the cache and returning the corresponding values.Synchronization note: this method is not synchronized on the cache. Thus, if replacements are performed during a canonicalization, it is undefined which object is returned. Similarly, this function is not synchronized on the request collection, so if synchronization is required, it must be performed externally.
- Parameters:
request
- The list of items to be fetched from the backing store. This collection must be modifiable.
-
canonicalize
public void canonicalize(Map<K,V> returnVal)
This convenience method takes a map of items returned from the backing store and replaces references loaded from the backing store with items in the cache.A call to canonicalize will typically be followed by a putAll on the returnVal, so that future requests to the cache will return the new items loaded.
Synchronization note: this method is not synchronized on the cache. Thus, if replacements are performed during a canonicalization, it is undefined which object is returned. Similarly, this function is not synchronized on the returnVal map, so if synchronization is required, it must be performed externally.
- Parameters:
returnVal
- The map of items to be canonicalized. This map must be modifiable.
-
newSoftCacheEntry
protected SoftCache.SoftCacheEntry<K,V> newSoftCacheEntry(K key, V value)
-
newWeakCacheEntry
protected SoftCache.WeakCacheEntry<K,V> newWeakCacheEntry(K key, V value)
-
-