net.sourceforge.jtds.jdbc.cache
Class SimpleLRUCache

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by net.sourceforge.jtds.jdbc.cache.SimpleLRUCache
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class SimpleLRUCache
extends java.util.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
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
private  java.util.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(java.lang.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).
 java.lang.Object get(java.lang.Object key)
          Overrides get() so that it also updates the LRU list.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Overrides put() so that it also updates the LRU list.
 java.lang.Object remove(java.lang.Object key)
           
 
Methods inherited from class java.util.HashMap
clone, containsKey, containsValue, entrySet, isEmpty, keySet, putAll, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

maxCacheSize

private final int maxCacheSize
Maximum cache size.


list

private final java.util.LinkedList list
LRU list.

Constructor Detail

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 Detail

clear

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

Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.HashMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Overrides put() so that it also updates the LRU list.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
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:
Map.put(Object, Object)

get

public java.lang.Object get(java.lang.Object key)
Overrides get() so that it also updates the LRU list.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.HashMap
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

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.HashMap
See Also:
Map.remove(Object)

freshenKey

private void freshenKey(java.lang.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


Generated on February 18 2010