org.sape.carbon.services.cache.mru
Class AbstractMRUCache

java.lang.Object
  |
  +--org.sape.carbon.services.cache.mru.AbstractMRUCache
All Implemented Interfaces:
Cache, Configurable, FunctionalInterface, Initializable, Map, MRUCache, Schedulable
Direct Known Subclasses:
DefaultMRUCacheImpl, MultiGetMRUCache

public abstract class AbstractMRUCache
extends Object
implements MRUCache, Configurable, Initializable

Class which provides a generic "Most Recently Used" cache. The cache uses a dataloader to provide it with the capability to retrieve items to encache. Cached items are then retrieved from memory, while uncached items are retrieved from the dataloader and placed into the cache.

The cache operates in what is known alternatly as a Most Recently Used (MRU) or Least Recently Used (LRU) manner. What this means is that the most recently requested items are kept in the cache, while the least recently used are cached only briefly, and tend to be fetched from the dataloader on each request. An MRU cache is very useful for data which it is even slightly expensive to retrieve, and which conforms to the 80/20 rule - 20% of the data is used 80% of the time.

The internal implementation of the MRUCache uses a pair of Hashtables to store the cache information and a TreeSet to store the ordering of keys in order to know which objects should be removed from the cache as it fills up. As a result, most operations on the cache are guaranteed log(n), where n is the size of the cache.

It should be noted that all methods on the MRUCache are synchronized, and that the underlying collections, which are occasionally exposed to the user through methods such as entrySet(), are also synchronized. The reason for this is the nature of an MRUCache. All write operations, and all read operations have the potential of modifying the cache - because reads may request items not found in the cache.

It should further be noted that all keys to the cache must implement the java.lang.Comparable interface. This is already implemented by all primitive wrapper classes, as well as File, String, Date, BigInteger and BigDecimal, making implementations for custom classes trivial in most cases. The use of Comparable for keys is what allows guaranteed log(n) operations on the cache, while maintaining cache integrity.

Elements stored in the cache, have an configurable expiration time. Reason being certain data might become stale after a period of time. You can say that I want elements only to exist in the cache for a certain period of time. For example, if you have a cache of stock quotes, you might want to mandate that stock quote information is only valuable if the quote data is less than 5 minutes old.

This operation of expiring elements happens passively. The get method will determine if the data is stale, if so it will get a fresh element.

The MRUCache makes use of the framework logging facilities to dump statistical information regarding the cache. Whenever the cache is cleared either manually, by calling clear(), or when refreshAll() is called by the cache manager, statistics are logged at DEBUG level. When a call to get(Object key) results in a cache miss statistics are logged at INFO level. This latter option should not be used in production as it severely degrades performance. When this level is not logged, no degredation is caused.

Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.17 $($Author: ghinkl $ / $Date: 2003/07/03 18:47:48 $)
Author:
Doug Voet, April 2002
See Also:
Comparable, TreeSet

Field Summary
protected  long cacheHits
          Tracks the number of hits to the cache.
protected  long cacheMisses
          Tracks the number of misses to the cache.
protected  int capacity
          Holds the capacity of the cache.
static long DEFAULT_ELEMENT_EXPIRATION_INTERVAL
          The default time out interval for MRUCache element expiration
protected  long expirationInterval
          Holds the expiration interval of objects in the cache.
protected  Map keyMap
          Holds all of the keys in the cache.
private  org.apache.commons.logging.Log log
          The handle to Apache-commons logger
protected  Map map
          Map of values in the cache.
static long MAX_EXPIRATION_TIME
          The default time for MRUCache element expiration, if no iterval hava been selected
protected  String name
          Holds the name of the configuration.
protected  TreeSet ordering
          A ordered set by the KeyInfo allows this.
 
Constructor Summary
AbstractMRUCache()
           
 
Method Summary
 void clear()
          Removes all mappings from this cache.
 void configure(ComponentConfiguration configuration)
          Configure the component.
 boolean containsKey(Object key)
          For a given key value, return a boolean indicating if the cache contains a value for the key.
 boolean containsValue(Object value)
          Returns true if this cache maps one or more keys to the specified value.
 Set entrySet()
          Returns an unmodifable view of the mappings contained in this cache.
protected  long getCacheHits()
          Protected method used by the testHarness.
protected  long getCacheMisses()
          Protected method used by the testHarness.
 Integer getCapacity()
          Gets the capacity of the cache.
 Long getExpirationInterval()
          Gets the current expiration interval of the cache.
 Float getHitPercentage()
          Gets the hit percentage of the cache.
 Long getHits()
          Gets the number of hits to the cache.
 Long getMisses()
          Gets the number of misses to the cache.
protected  Object getObject(Object key)
          This method retrieves an object from the internal maps and updates all of the normal key information.
 Long getSize()
          Gets the size of the cache.
 void initialize(Component thisComponent)
          Initialize the component.
 boolean isEmpty()
          Returns true if this map contains no key-value mappings.
 Set keySet()
          Returns an unmodifable set of the keys of the cache.
 Object put(Object key, Object value)
          Put the Object value into the cache referenced by the Object key.
 void putAll(Map t)
          Copies all of the mappings from the specified map to this cache.
 void refreshAll()
          Clears the cache in this implementation of the cache, allowing for periodic expiration of all data in the cache.
 Object remove(Object key)
          Removes the mapping for this key from this cache.
protected  void removeLast()
          Removes the oldest item in this cache.
 void runScheduledTask()
          Refreshes the cache
 void setCapacity(Integer capacity)
          Sets the capacity of the cache.
 void setExpirationInterval(Long newInterval)
          Sets the expiration interval for the cache.
 int size()
          Returns the number of key-value mappings in this map.
 String toString()
          Returns a string representation of the object included basic statistics like capacity, size, requests, total fetches, and hit rate.
 Collection values()
          Returns a collection view of the values contained in this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, get, hashCode
 

Field Detail

log

private org.apache.commons.logging.Log log
The handle to Apache-commons logger


capacity

protected int capacity
Holds the capacity of the cache.


expirationInterval

protected long expirationInterval
Holds the expiration interval of objects in the cache.


map

protected Map map
Map of values in the cache.


ordering

protected TreeSet ordering
A ordered set by the KeyInfo allows this. Used hold the MRU information in order of last access.


keyMap

protected Map keyMap
Holds all of the keys in the cache.


name

protected String name
Holds the name of the configuration.


cacheHits

protected long cacheHits
Tracks the number of hits to the cache.


cacheMisses

protected long cacheMisses
Tracks the number of misses to the cache.


DEFAULT_ELEMENT_EXPIRATION_INTERVAL

public static final long DEFAULT_ELEMENT_EXPIRATION_INTERVAL
The default time out interval for MRUCache element expiration

See Also:
Constant Field Values

MAX_EXPIRATION_TIME

public static final long MAX_EXPIRATION_TIME
The default time for MRUCache element expiration, if no iterval hava been selected

See Also:
Constant Field Values
Constructor Detail

AbstractMRUCache

public AbstractMRUCache()
Method Detail

configure

public void configure(ComponentConfiguration configuration)
Description copied from interface: Configurable
Configure the component. This is preceded and followed by the suspend and resume operations if they are available on the component.

Specified by:
configure in interface Configurable
Parameters:
configuration - the configuration for this component
See Also:
Configurable.configure(ComponentConfiguration)

initialize

public void initialize(Component thisComponent)
Description copied from interface: Initializable
Initialize the component. Called immediately after the Component's constructor. On return, the container may start the component.

Specified by:
initialize in interface Initializable
Parameters:
thisComponent - the reference to the component that this object is a part of. Store this referece within your Functional Implementation for future use.
See Also:
Initializable.initialize(org.sape.carbon.core.component.Component)

refreshAll

public void refreshAll()

Clears the cache in this implementation of the cache, allowing for periodic expiration of all data in the cache. All data in the cache is removed regardless of whether it has been in the cache since the last refresh, or it was just entered into the cache.

Specified by:
refreshAll in interface Cache

clear

public void clear()

Removes all mappings from this cache. This entails removing everything from the map which forms the real cache, removing the entries from the keyMap, and removing all of the KeyInfo objects from the ordering.

Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object key)

For a given key value, return a boolean indicating if the cache contains a value for the key.

Note: This method does not check the key's expiration.

Specified by:
containsKey in interface Map
Parameters:
key - key for the desired cache entry
Returns:
boolean true if the cache contains a mapping for the specified key

containsValue

public boolean containsValue(Object value)

Returns true if this cache maps one or more keys to the specified value. More formally, returns true if and only if this cache contains at least one mapping to a value v such that
(value==null ? v==null : value.equals(v)). This operation will require time linear in the cache size.

Note: This method does not check the key's expiration.

Specified by:
containsValue in interface Map
Parameters:
value - value whose presence in this map is to be tested.
Returns:
boolean true if this map maps one or more keys to the specified value.

entrySet

public Set entrySet()

Returns an unmodifable view of the mappings contained in this cache. Each element in the returned set is a Map.Entry.

Specified by:
entrySet in interface Map
Returns:
Set an unmodifable set of mappings in this cache

isEmpty

public boolean isEmpty()

Returns true if this map contains no key-value mappings.

Specified by:
isEmpty in interface Map
Returns:
boolean true if this map contains no key-value mappings

keySet

public Set keySet()

Returns an unmodifable set of the keys of the cache.

Note: This method does not check the key's expiration.

Specified by:
keySet in interface Map
Returns:
Set The an unmodifable set of all keys in the cache.

put

public Object put(Object key,
                  Object value)

Put the Object value into the cache referenced by the Object key. This will cause older items to be flushed from the cache when the cache is operating at its capacity.

Specified by:
put in interface Map
Parameters:
key - The key for the entry.
value - The value for the entry.
Returns:
the previous entry that was stored for the given key or null if there was no previous entry

putAll

public void putAll(Map t)

Copies all of the mappings from the specified map to this cache. These mappings will replace any mappings that this cache had for any of the keys currently in the specified cache.

Note that based upon the size limit of the cache, all mappings in the Map t may not be extent in the cache after this operation. All items will be put into the cache, but some may be flushed out as the least recently used before this operation completes.

Specified by:
putAll in interface Map
Parameters:
t - map of objects to place into this cache

remove

public Object remove(Object key)

Removes the mapping for this key from this cache.

Specified by:
remove in interface Map
Parameters:
key - the key remove the object for
Returns:
Object previous value associated with specified key, or null if there was no mapping for key.

size

public int size()

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Note: This method does not check the key's expiration.

Specified by:
size in interface Map
Returns:
int The number of key-value mappings in this map.

values

public Collection values()

Returns a collection view of the values contained in this cache.

Specified by:
values in interface Map
Returns:
Collection an unmodifiable view of the values contained in this cache

getHits

public Long getHits()
Gets the number of hits to the cache.

Specified by:
getHits in interface MRUCache
Returns:
the number of hits to the cache

getMisses

public Long getMisses()
Gets the number of misses to the cache.

Specified by:
getMisses in interface MRUCache
Returns:
the number of misses to the cache

getHitPercentage

public Float getHitPercentage()
Gets the hit percentage of the cache.

Specified by:
getHitPercentage in interface MRUCache
Returns:
the hit percentage of the cache

getCapacity

public Integer getCapacity()
Gets the capacity of the cache.

Returns:
the capacity of the cache

setCapacity

public void setCapacity(Integer capacity)
Sets the capacity of the cache.

Parameters:
capacity - the capacity of the cache

getExpirationInterval

public Long getExpirationInterval()
Gets the current expiration interval of the cache.

Returns:
the expiration interval

setExpirationInterval

public void setExpirationInterval(Long newInterval)
Sets the expiration interval for the cache.

Parameters:
newInterval - the new expiration interval

getSize

public Long getSize()
Gets the size of the cache.

Specified by:
getSize in interface MRUCache
Returns:
the size of the cache

toString

public String toString()
Returns a string representation of the object included basic statistics like capacity, size, requests, total fetches, and hit rate.

Overrides:
toString in class Object
Returns:
a string representation of the object

runScheduledTask

public void runScheduledTask()
Refreshes the cache

Specified by:
runScheduledTask in interface Schedulable
See Also:
Schedulable.runScheduledTask()

getObject

protected Object getObject(Object key)

This method retrieves an object from the internal maps and updates all of the normal key information. This will not however cause the dataloader to retrieve the value if it is not found. This has been done to allow the getMultiple interface to be able to pass a full list of not found values to the data loader.

Parameters:
key - The key object with which to lookup the value
Returns:
an object from the internal map matching the key

removeLast

protected void removeLast()

Removes the oldest item in this cache.


getCacheHits

protected long getCacheHits()

Protected method used by the testHarness.

Returns:
long number of hits

getCacheMisses

protected long getCacheMisses()

Protected method used by the testHarness.

Returns:
long number of misses


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.