com.arsdigita.util
Class Cache

java.lang.Object
  |
  +--com.arsdigita.util.Cache

public class Cache
extends java.lang.Object

data structure for a fixed-size cache table. Note that Cache is not thread-safe; it is up to the caller to synchronize if a cache is shared across multiple threads. Also includes a static global cache, whose methods are threadsafe.


Constructor Summary
Cache(long size)
          Create a new Cache of a fixed size.
Cache(long size, long maxAge)
          Create a new Cache of a fixed size.
 
Method Summary
 java.lang.Object get(java.lang.Object key)
          Returns an object from the cache, if it exists and hasn't expired.
static java.lang.Object getGlobal(java.lang.Object key)
          Returns an object from the global cache, if it exists and hasn't expired.
static void main(java.lang.String[] args)
           
 void put(java.lang.Object key, java.lang.Object value)
          Puts a new key/value pair into the cache with default lifetime (this.maxAge).
 void put(java.lang.Object key, java.lang.Object value, long maxAge)
          Puts a new key/value pair into the cache.
static void putGlobal(java.lang.Object key, java.lang.Object value)
          Puts a new key/value pair into the static global cache with default lifetime.
static void putGlobal(java.lang.Object key, java.lang.Object value, long maxAge)
          Puts a new key/value pair into the static global cache with specified lifetime.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(long size)
Create a new Cache of a fixed size. If more elements are put into the cache than it can hold, the least-recently used item will be evicted.
Parameters:
size - the number of items to allow before eviction

Cache

public Cache(long size,
             long maxAge)
Create a new Cache of a fixed size. If more elements are put into the cache than it can hold, the least-recently used item will be evicted.

Also allows an expiration time to be set; items in the cache that are older than that time will be evicted.

Parameters:
size - the number of items to allow before eviction
maxAge - the longest period of time, in milliseconds, that an element may stay in the cache before it is evicted, by default. (may be overriden by put).
Method Detail

put

public void put(java.lang.Object key,
                java.lang.Object value)
Puts a new key/value pair into the cache with default lifetime (this.maxAge).
Parameters:
key - the key
value - the value

put

public void put(java.lang.Object key,
                java.lang.Object value,
                long maxAge)
Puts a new key/value pair into the cache.
Parameters:
key - the key
value - the value
maxAge - the maximum lifetime of this cache entry, in milliseconds

get

public java.lang.Object get(java.lang.Object key)
Returns an object from the cache, if it exists and hasn't expired. Returns null otherwise.
Parameters:
key - the key to look up
Returns:
the object mapped by key, or null

putGlobal

public static void putGlobal(java.lang.Object key,
                             java.lang.Object value)
Puts a new key/value pair into the static global cache with default lifetime.
Parameters:
key - the key
value - the value

putGlobal

public static void putGlobal(java.lang.Object key,
                             java.lang.Object value,
                             long maxAge)
Puts a new key/value pair into the static global cache with specified lifetime.
Parameters:
key - the key
value - the value
maxAge - the lifetime of this cache entry in ms

getGlobal

public static java.lang.Object getGlobal(java.lang.Object key)
Returns an object from the global cache, if it exists and hasn't expired. Returns null otherwise.
Parameters:
key - the key to look up
Returns:
the object mapped by key, or null

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception