net.sourceforge.jtds.jdbc.cache

Class SimpleLRUCache


public class SimpleLRUCache
extends HashMap

Simple LRU cache for any type of object. Implemented as an extended HashMap with a maximum size and an aggregated List as LRU queue.
Version:
$Id: SimpleLRUCache.java,v 1.1 2005/04/25 11:46:56 alin_sinpalean Exp $
Author:
Brett Wooldridge

Field Summary

private LinkedList
list
LRU list.
private int
maxCacheSize
Maximum cache size.

Constructor Summary

SimpleLRUCache(int maxCacheSize)
Constructs a new LRU cache instance.

Method Summary

void
clear()
Overrides clear() to also clear the LRU list.
private void
freshenKey(Object key)
Moves the specified value to the top of the LRU list (the bottom of the list is where least recently used items live).
Object
get(Object key)
Overrides get() so that it also updates the LRU list.
Object
put(Object key, Object value)
Overrides put() so that it also updates the LRU list.
Object
remove(Object key)

Field Details

list

private final LinkedList list
LRU list.

maxCacheSize

private final int maxCacheSize
Maximum cache size.

Constructor Details

SimpleLRUCache

public SimpleLRUCache(int maxCacheSize)
Constructs a new LRU cache instance.
Parameters:
maxCacheSize - the maximum number of entries in this cache before entries are aged off

Method Details

clear

public void clear()
Overrides clear() to also clear the LRU list.

freshenKey

private void freshenKey(Object key)
Moves the specified value to the top of the LRU list (the bottom of the list is where least recently used items live).
Parameters:
key - key of the value to move to the top of the list

get

public Object get(Object key)
Overrides get() so that it also updates the LRU list.
Parameters:
key - key with which the expected value is associated
Returns:
the value to which the cache maps the specified key, or null if the map contains no mapping for this key

put

public Object put(Object key,
                  Object value)
Overrides put() so that it also updates the LRU list.
Parameters:
key - key with which the specified value is to be associated
value - value to be associated with the key
Returns:
previous value associated with key or null if there was no mapping for key; a null return can also indicate that the cache previously associated null with the specified key
See Also:
java.util.Map.put(Object, Object)

remove

public Object remove(Object key)
See Also:
java.util.Map.remove(Object)

Generated on September 18 2009