Class BoundedCache

java.lang.Object
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

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    least_used, fifo
  • Constructor Summary

    Constructors
    Constructor
    Description
    BoundedCache(Log log, String name, BoundedCache.Policy cachePolicy, int bound)
    Instantiates this class with the specified name, logger, policy and cache size.
    BoundedCache(BoundedCache.Policy cachePolicy, int bound)
    Creates a memory cache with a system-assigned name, logger and the specified policy and bound.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    Closes this cache, which makes all items in the cache unavailable.
    Returns the item for the specified key or null if the item does not exist.
    void
    Removes the object specified by the key.
    get(Object key)
    Returns the item for the specified key or null if the item does not exist.
    Returns the underlying storage read-only map for this cache.
    long
    Gets the current bound of this cache.
    boolean
    Returns true if this cache has been closed, false otherwise.
    static void
    main(String[] args)
     
    put(Object key, Object val)
    Puts the item for the specified key.
    void
    setBound(int items)
    Sets the upper bound of this cache.
     

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • 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
      cachePolicy - item removal policy when cache is full
      bound - the upper bound of this cache.
      logger - a reference to a Log
    • 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 Details

    • 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)