public class Cache<K,V> extends Object implements Map<K,V>
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:
Modifier and Type | Field and Description |
---|---|
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 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 and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(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.
|
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key) |
long |
getCacheHits() |
long |
getCacheMisses() |
int |
getMaxCacheSize() |
long |
getMaxLifetime() |
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> map) |
V |
remove(Object key) |
V |
remove(Object key,
boolean internal) |
void |
setMaxCacheSize(int maxCacheSize) |
void |
setMaxLifetime(long maxLifetime) |
int |
size() |
Collection<V> |
values() |
protected 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.
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.public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public boolean containsValue(Object value)
containsValue
in interface 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()