|
Jive Forums API (5.5.20.2-oracle) Developer Javadocs | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jivesoftware.util.LRUCache
public class LRUCache
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:
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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
java.util.Map.Entry<K,V> |
Field Summary | |
---|---|
protected 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 LinkedList |
lastAccessedList
Linked list to maintain order that cache objects are accessed in, most used to least used. |
protected java.util.Map |
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 Summary | |
---|---|
LRUCache(java.lang.String name,
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 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. |
java.util.Set |
entrySet()
|
java.lang.Object |
get(java.lang.Object key)
|
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. |
int |
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. |
java.lang.String |
getName()
Returns the name of this cache. |
boolean |
isEmpty()
|
java.util.Set |
keySet()
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
|
void |
putAll(java.util.Map map)
|
java.lang.Object |
remove(java.lang.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(java.lang.String name)
Sets the name of the cache |
int |
size()
|
java.util.Collection |
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 map
protected LinkedList lastAccessedList
protected LinkedList 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.
Constructor Detail |
---|
public LRUCache(java.lang.String name, int 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.Method Detail |
---|
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
put
in interface java.util.Map
public java.lang.Object get(java.lang.Object key)
get
in interface java.util.Map
public java.lang.Object remove(java.lang.Object key)
remove
in interface java.util.Map
public void clear()
clear
in interface java.util.Map
public int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public java.util.Collection values()
values
in interface java.util.Map
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map
public void putAll(java.util.Map map)
putAll
in interface java.util.Map
public boolean containsValue(java.lang.Object value)
containsValue
in interface java.util.Map
public java.util.Set entrySet()
entrySet
in interface java.util.Map
public java.util.Set keySet()
keySet
in interface java.util.Map
public java.lang.String getName()
getName
in interface Cache
public void setName(java.lang.String name)
Cache
setName
in interface Cache
name
- the name of the cachepublic 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
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
public int getCacheSize()
getCacheSize
in interface Cache
public int getMaxCacheSize()
getMaxCacheSize
in interface Cache
public void setMaxCacheSize(int maxCacheSize)
setMaxCacheSize
in interface Cache
maxCacheSize
- the maximum size of this cache (-1 indicates unlimited max size).public long getMaxLifetime()
getMaxLifetime
in interface Cache
public void setMaxLifetime(long maxLifetime)
setMaxLifetime
in interface Cache
maxLifetime
- the maximum number of milleseconds before objects are expired.protected void deleteExpiredEntries()
protected final void cullCache()
|
Jive Forums Project Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |