public class BasicMemoryCache extends java.lang.Object implements MemoryCache
| Modifier and Type | Class and Description |
|---|---|
protected static class |
BasicMemoryCache.CacheEntry |
MemoryCache.CacheListener| Modifier and Type | Field and Description |
|---|---|
protected java.util.concurrent.atomic.AtomicLong |
capacity |
protected java.util.concurrent.atomic.AtomicLong |
currentUsedCapacity |
protected java.util.concurrent.ConcurrentHashMap<java.lang.Object,BasicMemoryCache.CacheEntry> |
entries |
protected java.util.concurrent.CopyOnWriteArrayList<MemoryCache.CacheListener> |
listeners |
protected java.lang.Object |
lock |
protected java.lang.Long |
lowWater |
protected java.lang.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(java.lang.Object key,
Cacheable clientObject)
Attempts to add the
Cacheable object referenced by the key. |
boolean |
add(java.lang.Object key,
java.lang.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(java.lang.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.
|
java.lang.String |
getName() |
int |
getNumObjects()
Retrieve the number of items stored in the
MemoryCache. |
java.lang.Object |
getObject(java.lang.Object key)
Obtain the object referenced by key without removing it.
|
long |
getUsedCapacity()
Retrieves the amount of used
MemoryCache space. |
void |
remove(java.lang.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(java.lang.String name) |
java.lang.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 java.util.concurrent.atomic.AtomicLong capacity
protected java.util.concurrent.atomic.AtomicLong currentUsedCapacity
protected java.util.concurrent.ConcurrentHashMap<java.lang.Object,BasicMemoryCache.CacheEntry> entries
protected java.util.concurrent.CopyOnWriteArrayList<MemoryCache.CacheListener> listeners
protected final java.lang.Object lock
protected java.lang.Long lowWater
protected java.lang.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(java.lang.Object key,
Cacheable clientObject)
MemoryCacheCacheable 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 MemoryCachekey - an object used to reference the cached item.clientObject - the item to be cached.Cacheablepublic boolean add(java.lang.Object key,
java.lang.Object clientObject,
long clientObjectSize)
add in interface MemoryCachekey - 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 MemoryCachelistener - The new CacheListener.java.lang.IllegalArgumentException - is listener is null.public void clear()
clear in interface MemoryCachepublic boolean contains(java.lang.Object key)
This function does not cause the object referenced by the key to be marked as accessed. getObject()
should be used for that purpose.
contains in interface MemoryCachekey - The key of a specific object.java.lang.IllegalArgumentException - if key is null.protected void finalize()
throws java.lang.Throwable
finalize in class java.lang.Objectjava.lang.Throwablepublic long getCapacity()
MemoryCachegetCapacity in interface MemoryCachepublic long getFreeCapacity()
MemoryCachegetFreeCapacity in interface MemoryCachepublic long getLowWater()
getLowWater in interface MemoryCachepublic java.lang.String getName()
getName in interface MemoryCachepublic int getNumObjects()
MemoryCacheMemoryCache.getNumObjects in interface MemoryCachepublic java.lang.Object getObject(java.lang.Object key)
getObject in interface MemoryCachekey - The key for the object to be found.java.lang.IllegalArgumentException - if key is null.public long getUsedCapacity()
MemoryCacheMemoryCache space. The value returned is in cache units.getUsedCapacity in interface MemoryCachepublic void remove(java.lang.Object key)
remove in interface MemoryCachekey - the key of the object to be removed.java.lang.IllegalArgumentException - if key is null.public void removeCacheListener(MemoryCache.CacheListener listener)
removeCacheListener in interface MemoryCachelistener - The CacheListener to remove.java.lang.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 MemoryCachenewCapacity - the new capacity of the cache.public void setLowWater(long loWater)
When the cache fills, it removes items until it reaches the low water level.
Setting a high loWater level will increase cache misses, but decrease average add time, but setting a low loWater will do the opposite.
setLowWater in interface MemoryCacheloWater - the new low water level.public void setName(java.lang.String name)
setName in interface MemoryCachepublic java.lang.String toString()
String representation of this object is returned. This representation consists of maximum
size, current used capacity and number of currently cached items.toString in class java.lang.ObjectString representation of this object.