public final class BoundedCache extends Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
BoundedCache.Policy
least_used, fifo
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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() |
public BoundedCache(Log log, String name, BoundedCache.Policy cachePolicy, int bound)
name
- String denoting the name for this objectlogger
- a reference to a Log
cachePolicy
- item removal policy when cache is fullbound
- the upper bound of this cache.public BoundedCache(BoundedCache.Policy cachePolicy, int bound)
public void setBound(int items)
public long getBound()
public Object containsKey(Object key)
public Object get(Object key)
public Object put(Object key, Object val)
public void close()
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.
public boolean isClosed()
public void clear()