public class DefaultCache<K extends Serializable,V extends Serializable> extends Object implements Cache<K,V>
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.
Modifier and Type | Field and Description |
---|---|
protected LinkedList<K> |
ageList
Linked list to maintain time that cache objects were initially added
to the cache, most recently added to oldest added.
|
protected long |
cacheHits
Maintain the number of cache hits and misses.
|
protected long |
cacheMisses
Maintain the number of cache hits and misses.
|
protected LinkedList<K> |
lastAccessedList
Linked list to maintain order that cache objects are accessed
in, most used to least used.
|
protected Map<K,org.jivesoftware.util.cache.DefaultCache.CacheObject<V>> |
map
The map the keys and values are stored in.
|
protected long |
maxLifetime
Maximum length of time objects can exist in cache before expiring.
|
Constructor and Description |
---|
DefaultCache(String name,
long maxSize,
long maxLifetime)
Create a new default cache and specify the maximum size of for the cache in
bytes, and the maximum lifetime of objects.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
protected void |
cullCache()
Removes objects from cache if the cache is too full.
|
protected void |
deleteExpiredEntries()
Clears all entries out of cache where the entries are older than the
maximum defined age.
|
Set<Map.Entry<K,V>> |
entrySet() |
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 milleseconds that any object can live
in cache.
|
String |
getName()
Returns the name of this cache.
|
boolean |
isEmpty() |
Set<K> |
keySet() |
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 milleseconds that any object can live
in cache.
|
void |
setName(String name)
Sets the name of this cache.
|
int |
size() |
Collection<V> |
values() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
protected Map<K extends Serializable,org.jivesoftware.util.cache.DefaultCache.CacheObject<V extends Serializable>> map
protected LinkedList<K extends Serializable> lastAccessedList
protected LinkedList<K extends Serializable> ageList
protected long maxLifetime
protected long cacheHits
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
protected long cacheMisses
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
public DefaultCache(String name, long maxSize, long maxLifetime)
name
- a name for the cache.maxSize
- the maximum size of the cache in bytes. -1 means the cache
has no max size.maxLifetime
- the maximum amount of time objects can exist in
cache before being deleted. -1 means objects never expire.public V put(K key, V value)
put
in interface Map<K extends Serializable,V extends Serializable>
public V get(Object key)
get
in interface Map<K extends Serializable,V extends Serializable>
public V remove(Object key)
remove
in interface Map<K extends Serializable,V extends Serializable>
public void clear()
clear
in interface Map<K extends Serializable,V extends Serializable>
public int size()
size
in interface Map<K extends Serializable,V extends Serializable>
public boolean isEmpty()
isEmpty
in interface Map<K extends Serializable,V extends Serializable>
public Collection<V> values()
values
in interface Map<K extends Serializable,V extends Serializable>
public boolean containsKey(Object key)
containsKey
in interface Map<K extends Serializable,V extends Serializable>
public void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K extends Serializable,V extends Serializable>
public boolean containsValue(Object value)
containsValue
in interface Map<K extends Serializable,V extends Serializable>
public Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K extends Serializable,V extends Serializable>
public Set<K> keySet()
keySet
in interface Map<K extends Serializable,V extends Serializable>
public String getName()
getName
in interface Cache<K extends Serializable,V extends Serializable>
public void setName(String name)
setName
in interface Cache<K extends Serializable,V extends Serializable>
name
- the name of this cache.public long getCacheHits()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
getCacheHits
in interface Cache<K extends Serializable,V extends Serializable>
public long getCacheMisses()
Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
getCacheMisses
in interface Cache<K extends Serializable,V extends Serializable>
public int getCacheSize()
getCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
public long getMaxCacheSize()
getMaxCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
public void setMaxCacheSize(int maxCacheSize)
setMaxCacheSize
in interface Cache<K extends Serializable,V extends Serializable>
maxCacheSize
- the maximum size of this cache (-1 indicates unlimited max size).public long getMaxLifetime()
getMaxLifetime
in interface Cache<K extends Serializable,V extends Serializable>
public void setMaxLifetime(long maxLifetime)
setMaxLifetime
in interface Cache<K extends Serializable,V extends Serializable>
maxLifetime
- the maximum number of milleseconds before objects are expired.protected void deleteExpiredEntries()
protected final void cullCache()
public long getCacheCulls(Duration duration)
Copyright © 2003–2019 Ignite Realtime. All rights reserved.