org.jasig.portal.concurrency.caching
Class LRUCache

java.lang.Object
  extended by java.util.AbstractMap
      extended by java.util.HashMap
          extended by org.jasig.portal.concurrency.caching.LRUCache
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class LRUCache
extends java.util.HashMap

A rewrite of SmartCache that uses a moderate LRU algorithm: entries are purged from the cache via periodic sweeps rather than in response to specific cache additions. Note that sweeps have to be kicked off externally, e.g.,

int MAX_CACHE_SIZE = 1000;
int MAX_UNUSED_TIME_MILLIS = 30*60*1000;
LRUCache cache = new LRUCache(MAX_CACHE_SIZE, MAX_UNUSED_TIME_MILLIS);
// ... put stuff in ...
cache.sweepCache()
// ... put more stuff in ...

At the end of the sweep, the cache will have no more (and possibly less) than maxSize entries, though the sweep may have to reduce maxUnusedTimeMillis in order to get there.

Version:
$Revision: 1.7 $
Author:
Ken Weiner, Dan Ellentuck
See Also:
SmartCache, Serialized Form

Nested Class Summary
private  class LRUCache.ValueWrapper
           
 
Field Summary
protected static int DEFAULT_MAX_SIZE
           
protected static int DEFAULT_MAX_UNUSED_TIME_MILLIS
           
protected  int maxSize
           
protected  int maxUnusedTimeMillis
           
 
Constructor Summary
LRUCache()
           
LRUCache(int size)
           
LRUCache(int size, int maxUnusedAge)
           
 
Method Summary
 void clear()
          Synchronizes removal of ALL entries from the cache.
 java.lang.Object get(java.lang.Object key)
          Get the object from the cache and reset the timestamp.
private  java.lang.Object[] getKeySetArray()
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Add a new value to the cache.
 java.lang.Object remove(java.lang.Object key)
          Synchronizes removal of an entry from the cache.
 void sweepCache()
          Sweep the cache until it gets back under maxSize.
 
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

DEFAULT_MAX_SIZE

protected static int DEFAULT_MAX_SIZE

maxSize

protected int maxSize

DEFAULT_MAX_UNUSED_TIME_MILLIS

protected static int DEFAULT_MAX_UNUSED_TIME_MILLIS

maxUnusedTimeMillis

protected int maxUnusedTimeMillis
Constructor Detail

LRUCache

public LRUCache()

LRUCache

public LRUCache(int size)

LRUCache

public LRUCache(int size,
                int maxUnusedAge)
Parameters:
size - int
maxUnusedAge - int
Method Detail

clear

public void clear()
Synchronizes removal of ALL entries from the cache.

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

get

public java.lang.Object get(java.lang.Object key)
Get the object from the cache and reset the timestamp.

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.HashMap
Parameters:
key - the key, typically a String
Returns:
the value to which the key is mapped in this cache; null if the key is not mapped to any value in this cache.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Add a new value to the cache.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
Parameters:
key - the key, typically a String
value - the value
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.

remove

public java.lang.Object remove(java.lang.Object key)
Synchronizes removal of an entry from the cache.

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.HashMap
Parameters:
key - the key, typically a String
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.

sweepCache

public void sweepCache()
Sweep the cache until it gets back under maxSize.


getKeySetArray

private java.lang.Object[] getKeySetArray()