Bali Share 1.1.18

oracle.bali.share.collection
Class LRUCache

java.lang.Object
  |
  +--oracle.bali.share.collection.LRUCache

public class LRUCache
extends java.lang.Object

Acts like a hashtable with a limited amount of space. After the capacity has been reached, the least-gotten item is chucked. This is a "least-recently-used" scheme. The LRUCache is initialized with a certain cache size. The cache size determines the maximum number of items that can be cached. When an object is accessed by key through one of the get() methods, it is regarded as having been "used", and is put at the head of the list of "used" objects in the cache. When an object further down the list is accessed through a get() method, or by being added through one of the put() methods, the initial object is bumped one slot down the "used" list. The "used" list is not allowed to grow longer than the number of unique objects in the cache. Once the object is pushed to the last spot in the "used" list, then it may be pushed off the list by any put() of a different object. When an object is "pushed off the list", it means that a get() call on the cache with the object's key will return null. For example:

import oracle.bali.share.collection.LRUCache; public class Test { public static void main(String[] argc) { LRUCache c = new LRUCache(5); int i; for(i=0;i<5;i++) { c.put(i,""+i); } c.get(1); c.put(5,"5"); c.put(6,"6"); System.out.println("c.get(1)="+c.get(1)); System.out.println("c.get(0)="+c.get(0)); } }

Running the above example should give you:

c.get(1)=1 c.get(0)=null

See Also:
Hashtable

Constructor Summary
LRUCache(int size)
          Constructor.
 
Method Summary
 void clear()
          Clears the cache.
 java.lang.Object get(int key)
          Convenience method for getting values where the key is an integer.
 java.lang.Object get(java.lang.Object key)
          Gets the value for the given key.
 void put(int key, java.lang.Object value)
          Convenience method for putting values where the key is an integer.
 void put(java.lang.Object key, java.lang.Object value)
          Puts a key, value pair in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache(int size)
Constructor. The size passed in indicates the number of key,value pairs that should be kept before items are thrown away.
Parameters:
size - the number of items kept before least-recently-used ones are discarded.
Method Detail

get

public java.lang.Object get(int key)
Convenience method for getting values where the key is an integer. This method insures that Integer key objects are only allocated once per unique integer.
Parameters:
key - the integer key
Returns:
the value for the provided key, or null if the value is not in the cache, or if the value has been removed from the cache because it hasn't been used lately.

put

public void put(int key,
                java.lang.Object value)
Convenience method for putting values where the key is an integer. This method insures that Integer key objects are only allocated once per unique integer.
Parameters:
key - the integer key
value - the value

get

public java.lang.Object get(java.lang.Object key)
Gets the value for the given key. If the value for the provided key has not been accessed in the last n unique gets, where n is the size of the cache, this will return null.
Parameters:
key - the key
Returns:
the value, if present in the cache

put

public void put(java.lang.Object key,
                java.lang.Object value)
Puts a key, value pair in the cache.
Parameters:
key - the key
value - the value

clear

public void clear()
Clears the cache.

Bali Share 1.1.18