public class BasicMemoryCache extends Object implements MemoryCache
Modifier and Type | Class and Description |
---|---|
protected static class |
BasicMemoryCache.CacheEntry |
MemoryCache.CacheListener
Modifier and Type | Field and Description |
---|---|
protected AtomicLong |
capacity |
protected AtomicLong |
currentUsedCapacity |
protected ConcurrentHashMap<Object,BasicMemoryCache.CacheEntry> |
entries |
protected CopyOnWriteArrayList<MemoryCache.CacheListener> |
listeners |
protected Object |
lock |
protected Long |
lowWater |
protected String |
name |
Constructor and Description |
---|
BasicMemoryCache(long loWater,
long capacity)
Constructs a new cache using
capacity for maximum size, and loWater for the low water. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object key,
Cacheable clientObject)
Attempts to add the
Cacheable object referenced by the key. |
boolean |
add(Object key,
Object clientObject,
long clientObjectSize)
Adds an object to the cache.
|
void |
addCacheListener(MemoryCache.CacheListener listener)
Adds a cache listener, MemoryCache listeners are used to notify classes when an item is removed from the cache.
|
void |
clear()
Empties the cache.
|
boolean |
contains(Object key)
Returns true if the cache contains the item referenced by key.
|
protected void |
finalize() |
long |
getCapacity()
Retrieves the maximum size of the cache.
|
long |
getFreeCapacity()
Retrieves the available space for storing new items.
|
long |
getLowWater()
Returns the low water level in cache units.
|
String |
getName() |
int |
getNumObjects()
Retrieve the number of items stored in the
MemoryCache . |
Object |
getObject(Object key)
Obtain the object referenced by key without removing it.
|
long |
getUsedCapacity()
Retrieves the amount of used
MemoryCache space. |
void |
remove(Object key)
Remove the object reference by key from the cache.
|
void |
removeCacheListener(MemoryCache.CacheListener listener)
Removes a cache listener, objects using this listener will no longer receive notification of cache events.
|
protected void |
removeEntry(BasicMemoryCache.CacheEntry entry)
Removes
entry from the cache. |
void |
setCapacity(long newCapacity)
Sets the new capacity for the cache.
|
void |
setLowWater(long loWater)
Sets the new low water level in cache units, which controls how aggresively the cache discards items.
|
void |
setName(String name) |
String |
toString()
a
String representation of this object is returned. This representation consists of maximum
size, current used capacity and number of currently cached items. |
protected AtomicLong capacity
protected AtomicLong currentUsedCapacity
protected ConcurrentHashMap<Object,BasicMemoryCache.CacheEntry> entries
protected CopyOnWriteArrayList<MemoryCache.CacheListener> listeners
protected final Object lock
protected Long lowWater
protected String name
public BasicMemoryCache(long loWater, long capacity)
capacity
for maximum size, and loWater
for the low water.loWater
- the low water level.capacity
- the maximum capacity.public boolean add(Object key, Cacheable clientObject)
MemoryCache
Cacheable
object referenced by the key. No explicit size value is required as
this method queries the Cacheable to discover the size.
This method should be declared synchronized
when it is implemented.add
in interface MemoryCache
key
- an object used to reference the cached item.clientObject
- the item to be cached.Cacheable
public boolean add(Object key, Object clientObject, long clientObjectSize)
add
in interface MemoryCache
key
- The unique reference key that identifies this object.clientObject
- The actual object to be cached.clientObjectSize
- The size of the object in cache units.public void addCacheListener(MemoryCache.CacheListener listener)
addCacheListener
in interface MemoryCache
listener
- The new CacheListener
.IllegalArgumentException
- is listener
is null.public void clear()
clear
in interface MemoryCache
public boolean contains(Object key)
getObject()
should be used for that purpose.contains
in interface MemoryCache
key
- The key of a specific object.IllegalArgumentException
- if key
is null.protected void finalize() throws Throwable
public long getCapacity()
MemoryCache
getCapacity
in interface MemoryCache
public long getFreeCapacity()
MemoryCache
getFreeCapacity
in interface MemoryCache
public long getLowWater()
getLowWater
in interface MemoryCache
public String getName()
getName
in interface MemoryCache
public int getNumObjects()
MemoryCache
MemoryCache
.getNumObjects
in interface MemoryCache
public Object getObject(Object key)
getObject
in interface MemoryCache
key
- The key for the object to be found.IllegalArgumentException
- if key
is null.public long getUsedCapacity()
MemoryCache
MemoryCache
space. The value returned is in cache units.getUsedCapacity
in interface MemoryCache
public void remove(Object key)
remove
in interface MemoryCache
key
- the key of the object to be removed.IllegalArgumentException
- if key
is null.public void removeCacheListener(MemoryCache.CacheListener listener)
removeCacheListener
in interface MemoryCache
listener
- The CacheListener
to remove.IllegalArgumentException
- if listener
is null.protected void removeEntry(BasicMemoryCache.CacheEntry entry)
entry
from the cache. To remove an entry using its key, use remove()
.entry
- The entry (as opposed to key) of the item to be removed.public void setCapacity(long newCapacity)
setCapacity
in interface MemoryCache
newCapacity
- the new capacity of the cache.public void setLowWater(long loWater)
setLowWater
in interface MemoryCache
loWater
- the new low water level.public void setName(String name)
setName
in interface MemoryCache