org.apache.jcs.engine.memory.lru
Class LRUMemoryCache

java.lang.Object
  extended by org.apache.jcs.engine.memory.AbstractMemoryCache
      extended by org.apache.jcs.engine.memory.lru.LRUMemoryCache
All Implemented Interfaces:
java.io.Serializable, IMemoryCache, MemoryCache

public class LRUMemoryCache
extends AbstractMemoryCache

A fast reference management system. The least recently used items move to the end of the list and get spooled to disk if the cache hub is configured to use a disk cache. Most of the cache bottelnecks are in IO. There are no io bottlenecks here, it's all about processing power.

Even though there are only a few adjustments necessary to maintain the double linked list, we might want to find a more efficient memory manager for large cache regions.

The LRUMemoryCache is most efficient when the first element is selected. The smaller the region, the better the chance that this will be the case. < .04 ms per put, p3 866, 1/10 of that per get

Version:
$Id: LRUMemoryCache.java 536904 2007-05-10 16:03:42Z tv $
See Also:
Serialized Form

Nested Class Summary
 class LRUMemoryCache.IteratorWrapper
          iteration aid
 class LRUMemoryCache.MapEntryWrapper
           
 
Field Summary
 
Fields inherited from class org.apache.jcs.engine.memory.AbstractMemoryCache
attr, cache, cacheName, cattr, chunkSize, map, status
 
Constructor Summary
LRUMemoryCache()
           
 
Method Summary
protected  void addLast(CacheElement ce)
          Adds a new node to the end of the link list.
 void dumpCacheEntries()
          Dump the cache entries from first to list for debugging.
 void dumpMap()
          Dump the cache map for debugging.
 int freeElements(int numberToFree)
          This instructs the memory cache to remove the numberToFree according to its eviction policy.
 ICacheElement get(java.io.Serializable key)
          Get an item from the cache
 java.util.Iterator getIterator()
          Gets the iterator attribute of the LRUMemoryCache object
 java.lang.Object[] getKeyArray()
          Get an Array of the keys for all elements in the memory cache
 ICacheElement getQuiet(java.io.Serializable key)
          Get an item from the cache without affecting its last access time or position.
 IStats getStatistics()
          This returns semi-structured information on the memory cache, such as the size, put count, hit count, and miss count.
 void initialize(CompositeCache hub)
          For post reflection creation initialization.
 boolean remove(java.io.Serializable key)
          Removes an item from the cache.
 void removeAll()
          Remove all of the elements from both the Map and the linked list implementation.
protected  ICacheElement spoolLastElement()
          This spools the last element in the LRU, if one exists.
 void update(ICacheElement ce)
          Puts an item to the cache.
 
Methods inherited from class org.apache.jcs.engine.memory.AbstractMemoryCache
dispose, getCacheAttributes, getCacheName, getCompositeCache, getGroupKeys, getSize, getStatus, setCacheAttributes, waterfal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LRUMemoryCache

public LRUMemoryCache()
Method Detail

initialize

public void initialize(CompositeCache hub)
For post reflection creation initialization.

Specified by:
initialize in interface IMemoryCache
Overrides:
initialize in class AbstractMemoryCache
Parameters:
hub -

update

public void update(ICacheElement ce)
            throws java.io.IOException
Puts an item to the cache. Removes any pre-existing entries of the same key from the linked list and adds this one first.

If the max size is reached, an element will be put to disk.

Specified by:
update in interface IMemoryCache
Specified by:
update in class AbstractMemoryCache
Parameters:
ce - The cache element, or entry wrapper
Throws:
java.io.IOException

spoolLastElement

protected ICacheElement spoolLastElement()
                                  throws java.lang.Error
This spools the last element in the LRU, if one exists.

Returns:
ICacheElement if there was a last element, else null.
Throws:
java.lang.Error

freeElements

public int freeElements(int numberToFree)
                 throws java.io.IOException
This instructs the memory cache to remove the numberToFree according to its eviction policy. For example, the LRUMemoryCache will remove the numberToFree least recently used items. These will be spooled to disk if a disk auxiliary is available.

Parameters:
numberToFree -
Returns:
the number that were removed. if you ask to free 5, but there are only 3, you will get 3.
Throws:
java.io.IOException

getQuiet

public ICacheElement getQuiet(java.io.Serializable key)
                       throws java.io.IOException
Get an item from the cache without affecting its last access time or position.

Specified by:
getQuiet in interface IMemoryCache
Specified by:
getQuiet in class AbstractMemoryCache
Parameters:
key - Identifies item to find
Returns:
Element matching key if found, or null
Throws:
java.io.IOException

get

public ICacheElement get(java.io.Serializable key)
                  throws java.io.IOException
Get an item from the cache

Specified by:
get in interface IMemoryCache
Specified by:
get in class AbstractMemoryCache
Parameters:
key - Identifies item to find
Returns:
ICacheElement if found, else null
Throws:
java.io.IOException

remove

public boolean remove(java.io.Serializable key)
               throws java.io.IOException
Removes an item from the cache. This method handles hierarchical removal. If the key is a String and ends with the CacheConstants.NAME_COMPONENT_DELIMITER, then all items with keys starting with the argument String will be removed.

Specified by:
remove in interface IMemoryCache
Specified by:
remove in class AbstractMemoryCache
Parameters:
key -
Returns:
true if the removal was successful
Throws:
java.io.IOException

removeAll

public void removeAll()
               throws java.io.IOException
Remove all of the elements from both the Map and the linked list implementation. Overrides base class.

Specified by:
removeAll in interface IMemoryCache
Overrides:
removeAll in class AbstractMemoryCache
Throws:
java.io.IOException

getIterator

public java.util.Iterator getIterator()
Gets the iterator attribute of the LRUMemoryCache object

Specified by:
getIterator in interface IMemoryCache
Overrides:
getIterator in class AbstractMemoryCache
Returns:
The iterator value

getKeyArray

public java.lang.Object[] getKeyArray()
Get an Array of the keys for all elements in the memory cache

Specified by:
getKeyArray in interface IMemoryCache
Specified by:
getKeyArray in class AbstractMemoryCache
Returns:
An Object[]

addLast

protected void addLast(CacheElement ce)
Adds a new node to the end of the link list. Currently not used.

Parameters:
ce - The feature to be added to the Last

dumpMap

public void dumpMap()
Dump the cache map for debugging.


dumpCacheEntries

public void dumpCacheEntries()
Dump the cache entries from first to list for debugging.


getStatistics

public IStats getStatistics()
This returns semi-structured information on the memory cache, such as the size, put count, hit count, and miss count.

Specified by:
getStatistics in interface IMemoryCache
Overrides:
getStatistics in class AbstractMemoryCache
Returns:
Statistics and Infor for the Memory Cache.
See Also:
IMemoryCache.getStatistics()


Copyright © 2002-2007 Apache Software Foundation. All Rights Reserved.