org.sourceforge.beanindex.cache
Class LRUCache

java.lang.Object
  extended byorg.sourceforge.beanindex.cache.LRUCache
Direct Known Subclasses:
RAMLRUCache, SoftReferenceLRUCache

public abstract class LRUCache
extends java.lang.Object

A simple LRU Cache abstract class for implementation. It uses int array as a buffer to record the hit timestamp. Also it does not use the System time as the timestamp. To save memory it generates a timestamp of int type. each hit increments the timestamp by one.

Author:
Sakthivel Muthusamy

Field Summary
protected  int cacheSize
           
protected  int lastRemoved
           
protected static int LRU_BUFFER_INIT_CAPACITY
           
protected  int[] LRUBuffer
           
protected  java.util.Map map
           
protected  int timeStamp
           
 
Constructor Summary
LRUCache()
           
 
Method Summary
protected  void extendLRUBuffer(int location)
           
abstract  java.lang.Object get(int key)
          gets the bean out of cache if exists.
protected  int getLRUCanditatePosition()
          finds the LRU candidate position by traversing the LRUBuffer Could not think of any other better way at this point.It is a leaner search.
protected  int getTimeStamp()
          A custom timestamp to be used for LRUbuffer.
abstract  void put(int key, java.lang.Object obj)
          put the key, value pair in to cache.
protected  void refreshLRUbufferTimeStamp()
          This method refreshes the LRU Timestamp buffer to the min value in the buffer
protected  void removeLRUCandidate()
          This method removes the LRU Candidate from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LRU_BUFFER_INIT_CAPACITY

protected static final int LRU_BUFFER_INIT_CAPACITY
See Also:
Constant Field Values

LRUBuffer

protected int[] LRUBuffer

cacheSize

protected int cacheSize

timeStamp

protected int timeStamp

lastRemoved

protected int lastRemoved

map

protected java.util.Map map
Constructor Detail

LRUCache

public LRUCache()
Method Detail

get

public abstract java.lang.Object get(int key)
gets the bean out of cache if exists. It updates the LRUbuffer with the recent timestamp if the bean exists in cache.

Parameters:
key -
Returns:
Object

put

public abstract void put(int key,
                         java.lang.Object obj)
put the key, value pair in to cache. Note the id is a primitivie type. This helps to index the LRUbuffer easily.

Parameters:
key -
obj -

getTimeStamp

protected int getTimeStamp()
A custom timestamp to be used for LRUbuffer. Using System time will be wasting 4 more bytes for each bean. Now it is only int array.

Returns:
int

removeLRUCandidate

protected void removeLRUCandidate()
This method removes the LRU Candidate from the cache.


getLRUCanditatePosition

protected int getLRUCanditatePosition()
finds the LRU candidate position by traversing the LRUBuffer Could not think of any other better way at this point.It is a leaner search. But one good thing, It extra overhead is only for the cache hit miss only.

Returns:
int

refreshLRUbufferTimeStamp

protected void refreshLRUbufferTimeStamp()
This method refreshes the LRU Timestamp buffer to the min value in the buffer


extendLRUBuffer

protected void extendLRUBuffer(int location)