Openfire 3.6.1 Javadoc

org.jivesoftware.util.cache
Class DefaultCache<K,V>

java.lang.Object
  extended by org.jivesoftware.util.cache.DefaultCache<K,V>
All Implemented Interfaces:
Map<K,V>, Cache<K,V>

public class DefaultCache<K,V>
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:

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

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
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  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 Summary
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.
 
Method Summary
 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 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()
           
 
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

map

protected Map<K,org.jivesoftware.util.cache.DefaultCache.CacheObject<V>> map
The map the keys and values are stored in.


lastAccessedList

protected LinkedList lastAccessedList
Linked list to maintain order that cache objects are accessed in, most used to least used.


ageList

protected LinkedList ageList
Linked list to maintain time that cache objects were initially added to the cache, most recently added to oldest added.


maxLifetime

protected long maxLifetime
Maximum length of time objects can exist in cache before expiring.


cacheHits

protected long cacheHits
Maintain the number of cache hits and misses. A cache hit occurs every time the get method is called and the cache contains the requested object. A cache miss represents the opposite occurence.

Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.


cacheMisses

protected long cacheMisses
Maintain the number of cache hits and misses. A cache hit occurs every time the get method is called and the cache contains the requested object. A cache miss represents the opposite occurence.

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

DefaultCache

public 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.

Parameters:
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

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> map)
Specified by:
putAll in interface Map<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

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 interface Cache<K,V>
Returns:
the name of this cache.

setName

public void setName(String name)
Sets the name of this cache.

Specified by:
setName in interface Cache<K,V>
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 interface Cache<K,V>
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 interface Cache<K,V>
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 interface Cache<K,V>
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 interface Cache<K,V>
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 interface Cache<K,V>
Parameters:
maxCacheSize - the maximum size of this cache (-1 indicates unlimited max size).

getMaxLifetime

public long getMaxLifetime()
Returns the maximum number of milleseconds that any object can live in cache. Once the specified number of milleseconds passes, the object will be automatically expried from cache. If the max lifetime is set to -1, then objects never expire.

Specified by:
getMaxLifetime in interface Cache<K,V>
Returns:
the maximum number of milleseconds before objects are expired.

setMaxLifetime

public void setMaxLifetime(long maxLifetime)
Sets the maximum number of milleseconds that any object can live in cache. Once the specified number of milleseconds passes, the object will be automatically expried from cache. If the max lifetime is set to -1, then objects never expire.

Specified by:
setMaxLifetime in interface Cache<K,V>
Parameters:
maxLifetime - the maximum number of milleseconds before objects are expired.

deleteExpiredEntries

protected void deleteExpiredEntries()
Clears all entries out of cache where the entries are older than the maximum defined age.


cullCache

protected final void cullCache()
Removes objects from cache if the cache is too full. "Too full" is defined as within 3% of the maximum cache size. Whenever the cache is is too big, the least frequently used elements are deleted until the cache is at least 10% empty.


Openfire 3.6.1 Javadoc

Copyright © 2003-2008 Jive Software.