Package org.jivesoftware.util.cache
Class DefaultCache<K extends Serializable,V extends Serializable>
- java.lang.Object
-
- org.jivesoftware.util.cache.DefaultCache<K,V>
-
public class DefaultCache<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
Default, non-distributed implementation of the Cache interface. The algorithm for cache is as follows: a HashMap is maintained for fast object lookup. Two linked lists are maintained: one keeps objects in the order they are accessed from cache, the other keeps objects in the order they were originally added to cache. When objects are added to cache, they are first wrapped by a CacheObject which maintains the following pieces of information:- The size of the object (in bytes).
- A pointer to the node in the linked list that maintains accessed order for the object. Keeping a reference to the node lets us avoid linear scans of the linked list.
- A pointer to the node in the linked list that maintains the age of the object in cache. Keeping a reference to the node lets us avoid linear scans of the linked list.
To get an object from cache, a hash lookup is performed to get a reference to the CacheObject that wraps the real object we are looking for. The object is subsequently moved to the front of the accessed linked list and any necessary cache cleanups are performed. Cache deletion and expiration is performed as needed.
- Author:
- Matt Tucker
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.V
get(Object key)
long
getCacheCulls(Duration duration)
long
getCacheHits()
Returns the number of cache hits.long
getCacheMisses()
Returns the number of cache misses.int
getCacheSize()
Returns the size of the cache contents in bytes.long
getMaxCacheSize()
Returns the maximum size of the cache (in bytes).long
getMaxLifetime()
Returns the maximum number of milliseconds that any object can live in cache.String
getName()
Returns the name of this cache.boolean
isEmpty()
Set<K>
keySet()
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> map)
V
remove(Object key)
void
setMaxCacheSize(int maxCacheSize)
Sets the maximum size of the cache.void
setMaxLifetime(long maxLifetime)
Sets the maximum number of milliseconds that any object can live in cache.void
setName(String name)
Sets the name of this cache.int
size()
Collection<V>
values()
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jivesoftware.util.cache.Cache
getLock, isKeySecret, isValueSecret, setSecretKey, setSecretValue
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
map
protected Map<K extends Serializable,org.jivesoftware.util.cache.DefaultCache.CacheObject<V extends Serializable>> map
The map the keys and values are stored in.
-
-
Method Detail
-
put
public V put(K key, V value)
- Specified by:
put
in interfaceMap<K extends Serializable,V extends Serializable>
-
get
public V get(Object key)
- Specified by:
get
in interfaceMap<K extends Serializable,V extends Serializable>
-
remove
public V remove(Object key)
- Specified by:
remove
in interfaceMap<K extends Serializable,V extends Serializable>
-
clear
public void clear()
- Specified by:
clear
in interfaceMap<K extends Serializable,V extends Serializable>
-
size
public int size()
- Specified by:
size
in interfaceMap<K extends Serializable,V extends Serializable>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceMap<K extends Serializable,V extends Serializable>
-
values
public Collection<V> values()
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.- Specified by:
values
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
values
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable collection view of the values contained in this map
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K extends Serializable,V extends Serializable>
-
putAll
public void putAll(Map<? extends K,? extends V> map)
- Specified by:
putAll
in interfaceMap<K extends Serializable,V extends Serializable>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K extends Serializable,V extends Serializable>
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.- Specified by:
entrySet
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
entrySet
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable set view of the mappings contained in this map
-
keySet
public Set<K> keySet()
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.- Specified by:
keySet
in interfaceCache<K extends Serializable,V extends Serializable>
- Specified by:
keySet
in interfaceMap<K extends Serializable,V extends Serializable>
- Returns:
- an unmodifiable set view of the keys contained in this map
-
getName
public String getName()
Returns the name of this cache. The name is completely arbitrary and used only for display to administrators.- Specified by:
getName
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the name of this cache.
-
setName
public void setName(String name)
Sets the name of this cache.- Specified by:
setName
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
name
- the name of this cache.
-
getCacheHits
public long getCacheHits()
Returns the number of cache hits. A cache hit occurs every time the get method is called and the cache contains the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheHits
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the number of cache hits.
-
getCacheMisses
public long getCacheMisses()
Returns the number of cache misses. A cache miss occurs every time the get method is called and the cache does not contain the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheMisses
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the number of cache hits.
-
getCacheSize
public int getCacheSize()
Returns the size of the cache contents in bytes. This value is only a rough approximation, so cache users should expect that actual VM memory used by the cache could be significantly higher than the value reported by this method.- Specified by:
getCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the size of the cache contents in bytes.
-
getMaxCacheSize
public long getMaxCacheSize()
Returns the maximum size of the cache (in bytes). If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.- Specified by:
getMaxCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the maximum size of the cache (-1 indicates unlimited max size).
-
setMaxCacheSize
public void setMaxCacheSize(int maxCacheSize)
Sets the maximum size of the cache. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.- Specified by:
setMaxCacheSize
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
maxCacheSize
- the maximum size of this cache (-1 indicates unlimited max size).
-
getMaxLifetime
public long getMaxLifetime()
Returns the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.- Specified by:
getMaxLifetime
in interfaceCache<K extends Serializable,V extends Serializable>
- Returns:
- the maximum number of milliseconds before objects are expired.
-
setMaxLifetime
public void setMaxLifetime(long maxLifetime)
Sets the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.- Specified by:
setMaxLifetime
in interfaceCache<K extends Serializable,V extends Serializable>
- Parameters:
maxLifetime
- the maximum number of milliseconds before objects are expired.
-
getCacheCulls
public long getCacheCulls(Duration duration)
-
-