com.arsdigita.util
Class Cache

java.lang.Object
  extended bycom.arsdigita.util.Cache

public class Cache
extends 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.

Version:
$Revision: #8 $, $Date: 2004/04/07 $
Author:
Bill Schneider

Field Summary
static String versionId
           
 
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
 void clear()
          Removes all mapping from this map
 Object get(Object key)
          Returns an object from the cache, if it exists and hasn't expired.
static Object getGlobal(Object key)
          Returns an object from the global cache, if it exists and hasn't expired.
 void put(Object key, Object value)
          Puts a new key/value pair into the cache with default lifetime (this.maxAge).
 void put(Object key, Object value, long maxAge)
          Puts a new key/value pair into the cache.
static void putGlobal(Object key, Object value)
          Puts a new key/value pair into the static global cache with default lifetime.
static void putGlobal(Object key, Object value, long maxAge)
          Puts a new key/value pair into the static global cache with specified lifetime.
 void remove(Object key)
          Removes the mapping for this key if it exists
static void removeGlobal(Object key)
          Removes the mapping for this key if it exists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

versionId

public static final String versionId
See Also:
Constant Field Values
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(Object key,
                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(Object key,
                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 Object get(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

remove

public void remove(Object key)
Removes the mapping for this key if it exists

Parameters:
key - key whose mapping is to be removed

clear

public void clear()
Removes all mapping from this map


putGlobal

public static void putGlobal(Object key,
                             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(Object key,
                             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

removeGlobal

public static void removeGlobal(Object key)
Removes the mapping for this key if it exists

Parameters:
key - key whose mapping is to be removed

getGlobal

public static Object getGlobal(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


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 21 2004:2337 UTC