Clearspace API (1.10.16) Core Javadocs

com.jivesoftware.community.cache
Class DefaultCache

java.lang.Object
  extended by com.jivesoftware.community.cache.DefaultCache
All Implemented Interfaces:
com.jivesoftware.community.cache.Cache, com.tangosol.util.ObservableMap, java.util.Map

public class DefaultCache
extends java.lang.Object
implements com.jivesoftware.community.cache.Cache, com.tangosol.util.ObservableMap

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>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
protected  com.jivesoftware.util.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  int cacheSize
          Maintains the current size of the cache in bytes.
protected  com.jivesoftware.util.LinkedList lastAccessedList
          Linked list to maintain order that cache objects are accessed in, most used to least used.
protected  com.tangosol.util.Listeners listeners
           
protected  java.util.Map<java.lang.Object,com.jivesoftware.community.cache.CacheObject> map
          The map the keys and values are stored in.
protected  int maxCacheSize
          Maximum size in bytes that the cache can grow to.
protected  long maxLifetime
          Maximum length of time objects can exist in cache before expiring.
protected  java.lang.String name
          The name of the cache.
 
Constructor Summary
DefaultCache(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 addMapListener(com.tangosol.util.MapListener listener)
          Add a map listener.
 void addMapListener(com.tangosol.util.MapListener listener, com.tangosol.util.Filter filter, boolean bLite)
           
 void addMapListener(com.tangosol.util.MapListener listener, java.lang.Object key, boolean bLite)
           
protected  int calculateSize(java.lang.Object object)
          Returns the size of an object in bytes.
 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 entrySet()
           
protected  void fireMapEvent(com.tangosol.util.MapEvent evt, int eventType)
           
 java.lang.Object get(java.lang.Object key)
           
 long getCacheHits()
           
 long getCacheMisses()
           
 int getCacheSize()
           
 int getMaxCacheSize()
           
 long getMaxLifetime()
           
 java.lang.String getName()
           
 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)
           
 java.lang.Object remove(java.lang.Object key, boolean internal)
           
 void removeMapListener(com.tangosol.util.MapListener listener)
          Remove a map listener.
 void removeMapListener(com.tangosol.util.MapListener listener, com.tangosol.util.Filter filter)
           
 void removeMapListener(com.tangosol.util.MapListener listener, java.lang.Object key)
           
 void setMaxCacheSize(int maxCacheSize)
           
 void setMaxLifetime(long maxLifetime)
           
 void setName(java.lang.String name)
           
 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
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

map

protected java.util.Map<java.lang.Object,com.jivesoftware.community.cache.CacheObject> map
The map the keys and values are stored in.


lastAccessedList

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


ageList

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


maxCacheSize

protected int maxCacheSize
Maximum size in bytes that the cache can grow to.


cacheSize

protected int cacheSize
Maintains the current size of the cache in bytes.


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.


name

protected java.lang.String name
The name of the cache.


listeners

protected com.tangosol.util.Listeners listeners
Constructor Detail

DefaultCache

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

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 (in ms) objects can exist in cache before being deleted. -1 means objects never expire.
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map

remove

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

clear

public void clear()
Specified by:
clear in interface java.util.Map

size

public int size()
Specified by:
size in interface java.util.Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map

values

public java.util.Collection values()
Specified by:
values in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map

putAll

public void putAll(java.util.Map map)
Specified by:
putAll in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map

getName

public java.lang.String getName()
Specified by:
getName in interface com.jivesoftware.community.cache.Cache

setName

public void setName(java.lang.String name)
Specified by:
setName in interface com.jivesoftware.community.cache.Cache

keySet

public java.util.Set keySet()
Specified by:
keySet in interface java.util.Map

getCacheHits

public long getCacheHits()
Specified by:
getCacheHits in interface com.jivesoftware.community.cache.Cache

getCacheMisses

public long getCacheMisses()
Specified by:
getCacheMisses in interface com.jivesoftware.community.cache.Cache

getCacheSize

public int getCacheSize()
Specified by:
getCacheSize in interface com.jivesoftware.community.cache.Cache

getMaxCacheSize

public int getMaxCacheSize()
Specified by:
getMaxCacheSize in interface com.jivesoftware.community.cache.Cache

setMaxCacheSize

public void setMaxCacheSize(int maxCacheSize)
Specified by:
setMaxCacheSize in interface com.jivesoftware.community.cache.Cache

getMaxLifetime

public long getMaxLifetime()
Specified by:
getMaxLifetime in interface com.jivesoftware.community.cache.Cache

setMaxLifetime

public void setMaxLifetime(long maxLifetime)
Specified by:
setMaxLifetime in interface com.jivesoftware.community.cache.Cache

addMapListener

public void addMapListener(com.tangosol.util.MapListener listener)
Add a map listener.

Specified by:
addMapListener in interface com.tangosol.util.ObservableMap

addMapListener

public void addMapListener(com.tangosol.util.MapListener listener,
                           java.lang.Object key,
                           boolean bLite)
Specified by:
addMapListener in interface com.tangosol.util.ObservableMap

addMapListener

public void addMapListener(com.tangosol.util.MapListener listener,
                           com.tangosol.util.Filter filter,
                           boolean bLite)
Specified by:
addMapListener in interface com.tangosol.util.ObservableMap

removeMapListener

public void removeMapListener(com.tangosol.util.MapListener listener)
Remove a map listener.

Specified by:
removeMapListener in interface com.tangosol.util.ObservableMap

removeMapListener

public void removeMapListener(com.tangosol.util.MapListener listener,
                              java.lang.Object key)
Specified by:
removeMapListener in interface com.tangosol.util.ObservableMap

removeMapListener

public void removeMapListener(com.tangosol.util.MapListener listener,
                              com.tangosol.util.Filter filter)
Specified by:
removeMapListener in interface com.tangosol.util.ObservableMap

fireMapEvent

protected void fireMapEvent(com.tangosol.util.MapEvent evt,
                            int eventType)

calculateSize

protected int calculateSize(java.lang.Object object)
Returns the size of an object in bytes. Determining size by serialization is only used as a last resort.

Parameters:
object - the object to calculate
Returns:
the size of an object in bytes.

deleteExpiredEntries

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


cullCache

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.


Clearspace Project Page

Copyright © 1999-2007 Jive Software.