org.jivesoftware.smack.util
Class Cache<K,V>

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

public class Cache<K,V>
extends java.lang.Object
implements java.util.Map<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:

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
java.util.Map.Entry<K,V>
 
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)
           
 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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

Cache

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

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

put

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

get

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

remove

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

remove

public V remove(java.lang.Object key,
                boolean internal)

clear

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

size

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

isEmpty

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

values

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

containsKey

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

putAll

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

containsValue

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

entrySet

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

keySet

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

getCacheHits

public long getCacheHits()

getCacheMisses

public long getCacheMisses()

getMaxCacheSize

public int getMaxCacheSize()

setMaxCacheSize

public void setMaxCacheSize(int maxCacheSize)

getMaxLifetime

public long getMaxLifetime()

setMaxLifetime

public void setMaxLifetime(long maxLifetime)