|
Smack | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.jivesoftware.smack.util.Cache<K,V>
public class Cache<K,V>
A specialized Map that is size-limited (using an LRU algorithm) and has an optional expiration time for cache items. The Map is thread-safe.
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:
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Field Summary | |
---|---|
protected org.jivesoftware.smack.util.Cache.LinkedList |
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 org.jivesoftware.smack.util.Cache.LinkedList |
lastAccessedList
Linked list to maintain order that cache objects are accessed in, most used to least used. |
protected java.util.Map<K,org.jivesoftware.smack.util.Cache.CacheObject<V>> |
map
The map the keys and values are stored in. |
protected int |
maxCacheSize
Maximum number of items the cache will hold. |
protected long |
maxLifetime
Maximum length of time objects can exist in cache before expiring. |
Constructor Summary | |
---|---|
Cache(int maxSize,
long maxLifetime)
Create a new cache and specify the maximum size of for the cache in bytes, and the maximum lifetime of objects. |
Method Summary | |
---|---|
void |
clear()
|
boolean |
containsKey(java.lang.Object key)
|
boolean |
containsValue(java.lang.Object value)
|
protected void |
cullCache()
Removes the least recently used elements if the cache size is greater than or equal to the maximum allowed size until the cache is at least 10% empty. |
protected void |
deleteExpiredEntries()
Clears all entries out of cache where the entries are older than the maximum defined age. |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
|
V |
get(java.lang.Object key)
|
long |
getCacheHits()
|
long |
getCacheMisses()
|
int |
getMaxCacheSize()
|
long |
getMaxLifetime()
|
boolean |
isEmpty()
|
java.util.Set<K> |
keySet()
|
V |
put(K key,
V value)
|
void |
putAll(java.util.Map<? extends K,? extends V> map)
|
V |
remove(java.lang.Object key)
|
V |
remove(java.lang.Object key,
boolean internal)
|
void |
setMaxCacheSize(int maxCacheSize)
|
void |
setMaxLifetime(long maxLifetime)
|
int |
size()
|
java.util.Collection<V> |
values()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.util.Map |
---|
equals, hashCode |
Field Detail |
---|
protected java.util.Map<K,org.jivesoftware.smack.util.Cache.CacheObject<V>> map
protected org.jivesoftware.smack.util.Cache.LinkedList lastAccessedList
protected org.jivesoftware.smack.util.Cache.LinkedList ageList
protected int maxCacheSize
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.
Constructor Detail |
---|
public Cache(int maxSize, long maxLifetime)
maxSize
- the maximum number of objects the cache will hold. -1
means the cache has no max size.maxLifetime
- the maximum amount of time (in ms) objects can exist in
cache before being deleted. -1 means objects never expire.Method Detail |
---|
public V put(K key, V value)
put
in interface java.util.Map<K,V>
public V get(java.lang.Object key)
get
in interface java.util.Map<K,V>
public V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
public V remove(java.lang.Object key, boolean internal)
public void clear()
clear
in interface java.util.Map<K,V>
public int size()
size
in interface java.util.Map<K,V>
public boolean isEmpty()
isEmpty
in interface java.util.Map<K,V>
public java.util.Collection<V> values()
values
in interface java.util.Map<K,V>
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
public void putAll(java.util.Map<? extends K,? extends V> map)
putAll
in interface java.util.Map<K,V>
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map<K,V>
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
entrySet
in interface java.util.Map<K,V>
public java.util.Set<K> keySet()
keySet
in interface java.util.Map<K,V>
public long getCacheHits()
public long getCacheMisses()
public int getMaxCacheSize()
public void setMaxCacheSize(int maxCacheSize)
public long getMaxLifetime()
public void setMaxLifetime(long maxLifetime)
protected void deleteExpiredEntries()
protected void cullCache()
|
Smack | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |