org.apache.jcs.engine.memory
Class AbstractDoulbeLinkedListMemoryCache

java.lang.Object
  extended by org.apache.jcs.engine.memory.AbstractMemoryCache
      extended by org.apache.jcs.engine.memory.AbstractDoulbeLinkedListMemoryCache
All Implemented Interfaces:
java.io.Serializable, IMemoryCache, MemoryCache
Direct Known Subclasses:
FIFOMemoryCache, LRUMemoryCache, MRUMemoryCache

public abstract class AbstractDoulbeLinkedListMemoryCache
extends AbstractMemoryCache

This class contains methods that are common to memory caches using the double linked list, such as the LRU, MRU, FIFO, and LIFO caches.

Children can control the expiration algorithm by controlling the update and get. The last item in the list will be the one removed when the list fills. For instance LRU should more items to the front as they are used. FIFO should simply add new items to the front of the list.

See Also:
Serialized Form

Nested Class Summary
 class AbstractDoulbeLinkedListMemoryCache.IteratorWrapper
          iteration aid
 class AbstractDoulbeLinkedListMemoryCache.MapEntryWrapper
           
 
Field Summary
protected  int hitCnt
          number of hits
protected  DoubleLinkedList list
          thread-safe double linked list for lru
protected  int missCnt
          number of misses
 
Fields inherited from class org.apache.jcs.engine.memory.AbstractMemoryCache
attr, cache, cacheName, cattr, chunkSize, map, status
 
Constructor Summary
AbstractDoulbeLinkedListMemoryCache()
           
 
Method Summary
protected  MemoryElementDescriptor addFirst(ICacheElement ce)
          Adds a new node to the start of the link list.
protected  MemoryElementDescriptor addLast(ICacheElement ce)
          Adds a new node to the end of the link list.
protected abstract  void adjustListForGet(MemoryElementDescriptor me)
          Adjust the list as needed for a get.
protected abstract  MemoryElementDescriptor adjustListForUpdate(ICacheElement ce)
          Children implement this to control the cache expiration algorithm
 java.util.Map createMap()
          This is called by super initialize.
 void dumpCacheEntries()
          Dump the cache entries from first to list for debugging.
protected  int dumpCacheSize()
          Returns the size of the list.
 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 If the item is found, it is removed from the list and added first.
 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
 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)
          Calls the abstract method updateList.
protected  void verifyCache()
          Checks to see if all the items that should be in the cache are.
 
Methods inherited from class org.apache.jcs.engine.memory.AbstractMemoryCache
dispose, getCacheAttributes, getCacheName, getCompositeCache, getGroupKeys, getMultiple, getQuiet, getSize, getStatus, setCacheAttributes, waterfal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

protected DoubleLinkedList list
thread-safe double linked list for lru


hitCnt

protected int hitCnt
number of hits


missCnt

protected int missCnt
number of misses

Constructor Detail

AbstractDoulbeLinkedListMemoryCache

public AbstractDoulbeLinkedListMemoryCache()
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 -

createMap

public java.util.Map createMap()
This is called by super initialize.

Specified by:
createMap in class AbstractMemoryCache
Returns:
new Hashtable()

update

public final void update(ICacheElement ce)
                  throws java.io.IOException
Calls the abstract method updateList.

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

adjustListForUpdate

protected abstract MemoryElementDescriptor adjustListForUpdate(ICacheElement ce)
                                                        throws java.io.IOException
Children implement this to control the cache expiration algorithm

Parameters:
ce -
Returns:
MemoryElementDescriptor the new node
Throws:
java.io.IOException

get

public final ICacheElement get(java.io.Serializable key)
                        throws java.io.IOException
Get an item from the cache If the item is found, it is removed from the list and added first.

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

adjustListForGet

protected abstract void adjustListForGet(MemoryElementDescriptor me)
Adjust the list as needed for a get. This allows childred to control the algorithm

Parameters:
me -

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

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

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

addFirst

protected MemoryElementDescriptor addFirst(ICacheElement ce)
Adds a new node to the start of the link list.

Parameters:
ce - The feature to be added to the First
Returns:
MemoryElementDescriptor

addLast

protected MemoryElementDescriptor addLast(ICacheElement ce)
Adds a new node to the end of the link list.

Parameters:
ce - The feature to be added to the First
Returns:
MemoryElementDescriptor

dumpMap

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


dumpCacheEntries

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


dumpCacheSize

protected int dumpCacheSize()
Returns the size of the list.

Returns:
the number of items in the map.

verifyCache

protected void verifyCache()
Checks to see if all the items that should be in the cache are. Checks consistency between List and map.


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[]

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 about the cache
See Also:
IMemoryCache.getStatistics()


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