fc.util.cache
Class BoundedCache

java.lang.Object
  extended by fc.util.cache.BoundedCache

public final class BoundedCache
extends Object

In memory cache with the ability to specify an item bound/cache-limit functionality. By default,the upper bound is Integer.MAX_VALUE.

Existing items are not automatically expired. They are only removed when a new item is added and the bound size is crossed. Which item is removed is according to BoundedCache.POLICY.

ThreadSafety: This class is thread safe and can be used by multiple threads concurrently.

Version:
1.0 7/16/2010

Nested Class Summary
static class BoundedCache.Policy
          least_used, fifo
 
Constructor Summary
BoundedCache(BoundedCache.Policy cachePolicy, int bound)
          Creates a memory cache with a system-assigned name, logger and the specified policy and bound.
BoundedCache(Log log, String name, BoundedCache.Policy cachePolicy, int bound)
          Instantiates this class with the specified name, logger, policy and cache size.
 
Method Summary
 void clear()
           
 void close()
          Closes this cache, which makes all items in the cache unavailable.
 Object containsKey(Object key)
          Returns the item for the specified key or null if the item does not exist.
 void expire(Object key)
          Removes the object specified by the key.
 Object get(Object key)
          Returns the item for the specified key or null if the item does not exist.
 Map getAll()
          Returns the underlying storage read-only map for this cache.
 long getBound()
          Gets the current bound of this cache.
 boolean isClosed()
          Returns true if this cache has been closed, false otherwise.
static void main(String[] args)
           
 Object put(Object key, Object val)
          Puts the item for the specified key.
 void setBound(int items)
          Sets the upper bound of this cache.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BoundedCache

public BoundedCache(Log log,
                    String name,
                    BoundedCache.Policy cachePolicy,
                    int bound)
Instantiates this class with the specified name, logger, policy and cache size.

Parameters:
name - String denoting the name for this object
logger - a reference to a Log
cachePolicy - item removal policy when cache is full
bound - the upper bound of this cache.

BoundedCache

public BoundedCache(BoundedCache.Policy cachePolicy,
                    int bound)
Creates a memory cache with a system-assigned name, logger and the specified policy and bound.

Method Detail

setBound

public void setBound(int items)
Sets the upper bound of this cache.


getBound

public long getBound()
Gets the current bound of this cache.


expire

public void expire(Object key)
Removes the object specified by the key.


containsKey

public Object containsKey(Object key)
Returns the item for the specified key or null if the item does not exist.


get

public Object get(Object key)
Returns the item for the specified key or null if the item does not exist.


getAll

public Map getAll()
Returns the underlying storage read-only map for this cache.


put

public Object put(Object key,
                  Object val)
Puts the item for the specified key. Returns the previous item for this key or null if no previous item existed.


close

public void close()
Closes this cache, which makes all items in the cache unavailable. Any items needed for later should be taken out of the cache before closing it.

Note:, it is a good idea to close the cache once it's not needed, because it releases resources, including any internal threads spawned and used by this implementation.


isClosed

public boolean isClosed()
Returns true if this cache has been closed, false otherwise.


clear

public void clear()

toString

public String toString()
Overrides:
toString in class Object

main

public static void main(String[] args)