org.exolab.core.foundation
Class LruObjectCache

java.lang.Object
  extended byorg.exolab.core.foundation.LruObjectCache

public class LruObjectCache
extends java.lang.Object

This class is a global object cache for database objects. It is sycn' ed with the data store and is used to improve the performance of the overall system by minimizing the number of times an object is stremed in and out of the data store.

The algorithm is quite simplistic implementation of an LRU cache whose capacity is specified during object construction time.The cache has basically two operations 'insert' and 'remove'. When inserting objects into the cache, and the cache is full, then the 'least used object' will be evicted.

The LRU algorithm is implemented as a doubly linked list with references to the first and last elements in the list. The element at the end of the list is the least used element and the element at the top of a list is the most used element.

Version:
$Revision: 1.1 $ $Date: 2000/06/07 09:17:28 $
Author:
Jim Alateras

Constructor Summary
LruObjectCache(int size)
          Instantiate an instance of this cache with the specified number of entries.
 
Method Summary
 void clear()
          Clear all the entries in the cache
 java.util.Enumeration elements()
          Return an enumeration of all the elements in the cache, Most recently used ones first.
 java.lang.Object get(java.lang.Object key)
          Return the object with the specified key from the cache.
 int getFreeSlots()
          Return the number of free slots inthe cache
 int getUsedSlots()
          Return the number of entries in the cache
 double hitMissRatio()
          Return the hit:miss ratio.
 void insert(java.lang.Object key, java.lang.Object object)
          Insert the object with the following key into the cache.
 void remove(java.lang.Object key)
          Remove the object with the specified key from the cache.
protected  org.exolab.core.foundation.CacheEntry removeFromCache(org.exolab.core.foundation.CacheEntry entry)
          Remove the specified entry from the cache and return it to the client.
protected  org.exolab.core.foundation.CacheEntry removeFromCache(java.lang.Object key)
          Remove the element with the specified key from the cache and return it to the client.If the element does not exist then return null
protected  void touchCacheEntry(java.lang.Object key, java.lang.Object object)
          Return an enumeration of entries in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LruObjectCache

public LruObjectCache(int size)
Instantiate an instance of this cache with the specified number of entries. When the number of entriesis reached the eviction algorithm (in this case LRU) determines which element to evict.

Parameters:
size - size of the cache
Method Detail

insert

public void insert(java.lang.Object key,
                   java.lang.Object object)
Insert the object with the following key into the cache. If the key already exists then update, the associated object

Parameters:
key - object key
object - object to cache

remove

public void remove(java.lang.Object key)
Remove the object with the specified key from the cache. If the key does not exist then fail silently.

Parameters:
key - object key

get

public java.lang.Object get(java.lang.Object key)
Return the object with the specified key from the cache. If the object does not exist then return null. If the object exists then increment the hit count

Parameters:
key - object key
Returns:
Object object value

clear

public void clear()
Clear all the entries in the cache


hitMissRatio

public double hitMissRatio()
Return the hit:miss ratio. This is calculate by dividing the number successful retrievals from the cache by the number of requested retrievals.

Can be used to monitor the effectiveness of the cache

Returns:
double

getUsedSlots

public int getUsedSlots()
Return the number of entries in the cache

Returns:
int number of entries in cache

getFreeSlots

public int getFreeSlots()
Return the number of free slots inthe cache

Returns:
int number of free entries in cache

elements

public java.util.Enumeration elements()
Return an enumeration of all the elements in the cache, Most recently used ones first.

Returns:
Enumeration

touchCacheEntry

protected void touchCacheEntry(java.lang.Object key,
                               java.lang.Object object)
Return an enumeration of entries in the cache. Iterating this enumeration is expensive /** Add the specified key and object to the cache to the end of the LRU queue

Parameters:
key - object key
object - object value

removeFromCache

protected org.exolab.core.foundation.CacheEntry removeFromCache(org.exolab.core.foundation.CacheEntry entry)
Remove the specified entry from the cache and return it to the client. Adjust all the pointers and remove the entry from the keyMap

Parameters:
entry - entry to remove
Returns:
CacheEntry removed cache entry

removeFromCache

protected org.exolab.core.foundation.CacheEntry removeFromCache(java.lang.Object key)
Remove the element with the specified key from the cache and return it to the client.If the element does not exist then return null

Parameters:
key - key of object to remove
Returns:
CacheEntry removed cache entry


Copyright © 1999-2005 The Exolab Group. All Rights Reserved.