org.apache.jcs.engine.memory
Class AbstractDoubleLinkedListMemoryCache<K extends Serializable,V extends Serializable>

java.lang.Object
  extended by org.apache.jcs.engine.memory.AbstractMemoryCache<K,V>
      extended by org.apache.jcs.engine.memory.AbstractDoubleLinkedListMemoryCache<K,V>
All Implemented Interfaces:
Serializable, IRequireScheduler, IMemoryCache<K,V>
Direct Known Subclasses:
FIFOMemoryCache, LRUMemoryCache, MRUMemoryCache

public abstract class AbstractDoubleLinkedListMemoryCache<K extends Serializable,V extends Serializable>
extends AbstractMemoryCache<K,V>

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
static class AbstractDoubleLinkedListMemoryCache.IteratorWrapper<K extends Serializable,V extends Serializable>
          iteration aid
static class AbstractDoubleLinkedListMemoryCache.MapEntryWrapper<K extends Serializable,V extends Serializable>
           
 
Field Summary
protected  int hitCnt
          number of hits
protected  DoubleLinkedList<MemoryElementDescriptor<K,V>> list
          thread-safe double linked list for lru
protected  int missCnt
          number of misses
 
Fields inherited from class org.apache.jcs.engine.memory.AbstractMemoryCache
cache, cacheAttributes, cacheName, chunkSize, elementAttributes, map, status
 
Constructor Summary
AbstractDoubleLinkedListMemoryCache()
           
 
Method Summary
protected  MemoryElementDescriptor<K,V> addFirst(ICacheElement<K,V> ce)
          Adds a new node to the start of the link list.
protected  MemoryElementDescriptor<K,V> addLast(ICacheElement<K,V> ce)
          Adds a new node to the end of the link list.
protected abstract  void adjustListForGet(MemoryElementDescriptor<K,V> me)
          Adjust the list as needed for a get.
protected abstract  MemoryElementDescriptor<K,V> adjustListForUpdate(ICacheElement<K,V> ce)
          Children implement this to control the cache expiration algorithm
 Map<K,MemoryElementDescriptor<K,V>> 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.
 int freeElements(int numberToFree)
          This instructs the memory cache to remove the numberToFree according to its eviction policy.
 ICacheElement<K,V> get(K key)
          Get an item from the cache If the item is found, it is removed from the list and added first.
 Iterator<Map.Entry<K,MemoryElementDescriptor<K,V>>> getIterator()
          Gets the iterator attribute of the LRUMemoryCache object
 Set<K> getKeySet()
          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<K,V> hub)
          For post reflection creation initialization.
 boolean remove(K 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<K,V> spoolLastElement()
          This spools the last element in the LRU, if one exists.
 void update(ICacheElement<K,V> 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, dumpMap, getCacheAttributes, getCacheName, getCompositeCache, getGroupKeys, getGroupNames, getMultiple, getQuiet, getSize, getStatus, setCacheAttributes, setScheduledExecutorService, waterfal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

protected DoubleLinkedList<MemoryElementDescriptor<K extends Serializable,V extends Serializable>> list
thread-safe double linked list for lru


hitCnt

protected int hitCnt
number of hits


missCnt

protected int missCnt
number of misses

Constructor Detail

AbstractDoubleLinkedListMemoryCache

public AbstractDoubleLinkedListMemoryCache()
Method Detail

initialize

public void initialize(CompositeCache<K,V> hub)
For post reflection creation initialization.

Specified by:
initialize in interface IMemoryCache<K extends Serializable,V extends Serializable>
Overrides:
initialize in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Parameters:
hub -

createMap

public Map<K,MemoryElementDescriptor<K,V>> createMap()
This is called by super initialize.

Specified by:
createMap in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Returns:
new Hashtable()

update

public final void update(ICacheElement<K,V> ce)
                  throws 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<K extends Serializable,V extends Serializable>
Specified by:
update in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Parameters:
ce - The cache element, or entry wrapper
Throws:
IOException

adjustListForUpdate

protected abstract MemoryElementDescriptor<K,V> adjustListForUpdate(ICacheElement<K,V> ce)
                                                                                                       throws IOException
Children implement this to control the cache expiration algorithm

Parameters:
ce -
Returns:
MemoryElementDescriptor the new node
Throws:
IOException

get

public final ICacheElement<K,V> get(K key)
                                                                       throws 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<K extends Serializable,V extends Serializable>
Specified by:
get in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Parameters:
key - Identifies item to find
Returns:
ICacheElement if found, else null
Throws:
IOException

adjustListForGet

protected abstract void adjustListForGet(MemoryElementDescriptor<K,V> me)
Adjust the list as needed for a get. This allows children to control the algorithm

Parameters:
me -

freeElements

public int freeElements(int numberToFree)
                 throws 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:
IOException

spoolLastElement

protected ICacheElement<K,V> spoolLastElement()
                                                                                 throws Error
This spools the last element in the LRU, if one exists.

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

remove

public boolean remove(K key)
               throws 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<K extends Serializable,V extends Serializable>
Specified by:
remove in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Parameters:
key -
Returns:
true if the removal was successful
Throws:
IOException

removeAll

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

Specified by:
removeAll in interface IMemoryCache<K extends Serializable,V extends Serializable>
Overrides:
removeAll in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Throws:
IOException

addFirst

protected MemoryElementDescriptor<K,V> addFirst(ICacheElement<K,V> 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<K,V> addLast(ICacheElement<K,V> ce)
Adds a new node to the end of the link list.

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

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 Iterator<Map.Entry<K,MemoryElementDescriptor<K,V>>> getIterator()
Gets the iterator attribute of the LRUMemoryCache object

Specified by:
getIterator in interface IMemoryCache<K extends Serializable,V extends Serializable>
Overrides:
getIterator in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Returns:
The iterator value

getKeySet

public Set<K> getKeySet()
Get an Array of the keys for all elements in the memory cache

Specified by:
getKeySet in interface IMemoryCache<K extends Serializable,V extends Serializable>
Specified by:
getKeySet in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
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<K extends Serializable,V extends Serializable>
Overrides:
getStatistics in class AbstractMemoryCache<K extends Serializable,V extends Serializable>
Returns:
statistics about the cache
See Also:
org.apache.jcs.engine.memory.IMemoryCache#getStatistics()


Copyright © 2002-2013 The Apache Software Foundation. All Rights Reserved.