|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.sape.carbon.services.cache.mru.AbstractMRUCache
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
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 |
private org.apache.commons.logging.Log log
protected int capacity
protected long expirationInterval
protected Map map
protected TreeSet ordering
protected Map keyMap
protected String name
protected long cacheHits
protected long cacheMisses
public static final long DEFAULT_ELEMENT_EXPIRATION_INTERVAL
public static final long MAX_EXPIRATION_TIME
Constructor Detail |
public AbstractMRUCache()
Method Detail |
public void configure(ComponentConfiguration configuration)
Configurable
configure
in interface Configurable
configuration
- the configuration for this componentConfigurable.configure(ComponentConfiguration)
public void initialize(Component thisComponent)
Initializable
initialize
in interface Initializable
thisComponent
- the reference to the component that this object
is a part of. Store this referece within your Functional Implementation
for future use.Initializable.initialize(org.sape.carbon.core.component.Component)
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.
refreshAll
in interface Cache
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.
clear
in interface Map
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.
containsKey
in interface Map
key
- key for the desired cache entry
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.
containsValue
in interface Map
value
- value whose presence in this map is to be
tested.
public Set entrySet()
Returns an unmodifable view of the mappings contained in this cache. Each element in the returned set is a Map.Entry.
entrySet
in interface Map
public boolean isEmpty()
Returns true if this map contains no key-value mappings.
isEmpty
in interface Map
public Set keySet()
Returns an unmodifable set of the keys of the cache.
Note: This method does not check the key's expiration.
keySet
in interface Map
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.
put
in interface Map
key
- The key for the entry.value
- The value for the entry.
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.
putAll
in interface Map
t
- map of objects to place into this cachepublic Object remove(Object key)
Removes the mapping for this key from this cache.
remove
in interface Map
key
- the key remove the object for
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.
size
in interface Map
public Collection values()
Returns a collection view of the values contained in this cache.
values
in interface Map
public Long getHits()
getHits
in interface MRUCache
public Long getMisses()
getMisses
in interface MRUCache
public Float getHitPercentage()
getHitPercentage
in interface MRUCache
public Integer getCapacity()
public void setCapacity(Integer capacity)
capacity
- the capacity of the cachepublic Long getExpirationInterval()
public void setExpirationInterval(Long newInterval)
newInterval
- the new expiration intervalpublic Long getSize()
getSize
in interface MRUCache
public String toString()
toString
in class Object
public void runScheduledTask()
runScheduledTask
in interface Schedulable
Schedulable.runScheduledTask()
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.
key
- The key object with which to lookup the value
protected void removeLast()
Removes the oldest item in this cache.
protected long getCacheHits()
Protected method used by the testHarness.
protected long getCacheMisses()
Protected method used by the testHarness.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |