Package gov.nasa.worldwind.cache
Class BasicMemoryCache
- java.lang.Object
-
- gov.nasa.worldwind.cache.BasicMemoryCache
-
- All Implemented Interfaces:
MemoryCache
- Direct Known Subclasses:
BasicRasterServerCache
public class BasicMemoryCache extends java.lang.Object implements MemoryCache
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classBasicMemoryCache.CacheEntry-
Nested classes/interfaces inherited from interface gov.nasa.worldwind.cache.MemoryCache
MemoryCache.CacheListener
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicLongcapacityprotected java.util.concurrent.atomic.AtomicLongcurrentUsedCapacityprotected java.util.concurrent.ConcurrentHashMap<java.lang.Object,BasicMemoryCache.CacheEntry>entriesprotected java.util.concurrent.CopyOnWriteArrayList<MemoryCache.CacheListener>listenersprotected java.lang.Objectlockprotected java.lang.LonglowWaterprotected java.lang.Stringname
-
Constructor Summary
Constructors Constructor Description BasicMemoryCache(long loWater, long capacity)Constructs a new cache usingcapacityfor maximum size, andloWaterfor the low water.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(java.lang.Object key, Cacheable clientObject)Attempts to add theCacheableobject referenced by the key.booleanadd(java.lang.Object key, java.lang.Object clientObject, long clientObjectSize)Adds an object to the cache.voidaddCacheListener(MemoryCache.CacheListener listener)Adds a cache listener, MemoryCache listeners are used to notify classes when an item is removed from the cache.voidclear()Empties the cache.booleancontains(java.lang.Object key)Returns true if the cache contains the item referenced by key.longgetCapacity()Retrieves the maximum size of the cache.longgetFreeCapacity()Retrieves the available space for storing new items.longgetLowWater()Returns the low water level in cache units.java.lang.StringgetName()intgetNumObjects()Retrieve the number of items stored in theMemoryCache.java.lang.ObjectgetObject(java.lang.Object key)Obtain the object referenced by key without removing it.longgetUsedCapacity()Retrieves the amount of usedMemoryCachespace.voidremove(java.lang.Object key)Remove the object reference by key from the cache.voidremoveCacheListener(MemoryCache.CacheListener listener)Removes a cache listener, objects using this listener will no longer receive notification of cache events.protected voidremoveEntry(BasicMemoryCache.CacheEntry entry)Removesentryfrom the cache.voidsetCapacity(long newCapacity)Sets the new capacity for the cache.voidsetLowWater(long loWater)Sets the new low water level in cache units, which controls how aggresively the cache discards items.voidsetName(java.lang.String name)java.lang.StringtoString()aStringrepresentation of this object is returned. This representation consists of maximum size, current used capacity and number of currently cached items.
-
-
-
Field Detail
-
entries
protected java.util.concurrent.ConcurrentHashMap<java.lang.Object,BasicMemoryCache.CacheEntry> entries
-
listeners
protected java.util.concurrent.CopyOnWriteArrayList<MemoryCache.CacheListener> listeners
-
capacity
protected java.util.concurrent.atomic.AtomicLong capacity
-
currentUsedCapacity
protected java.util.concurrent.atomic.AtomicLong currentUsedCapacity
-
lowWater
protected java.lang.Long lowWater
-
name
protected java.lang.String name
-
lock
protected final java.lang.Object lock
-
-
Method Detail
-
getNumObjects
public int getNumObjects()
Description copied from interface:MemoryCacheRetrieve the number of items stored in theMemoryCache.- Specified by:
getNumObjectsin interfaceMemoryCache- Returns:
- the number of objects currently stored in this cache.
-
getCapacity
public long getCapacity()
Description copied from interface:MemoryCacheRetrieves the maximum size of the cache.- Specified by:
getCapacityin interfaceMemoryCache- Returns:
- the capacity of the cache.
-
getUsedCapacity
public long getUsedCapacity()
Description copied from interface:MemoryCacheRetrieves the amount of usedMemoryCachespace. The value returned is in cache units.- Specified by:
getUsedCapacityin interfaceMemoryCache- Returns:
- the number of cache units that the cache currently holds.
-
getFreeCapacity
public long getFreeCapacity()
Description copied from interface:MemoryCacheRetrieves the available space for storing new items.- Specified by:
getFreeCapacityin interfaceMemoryCache- Returns:
- the amount of free space left in the cache (in cache units).
-
setName
public void setName(java.lang.String name)
- Specified by:
setNamein interfaceMemoryCache
-
getName
public java.lang.String getName()
- Specified by:
getNamein interfaceMemoryCache
-
addCacheListener
public void addCacheListener(MemoryCache.CacheListener listener)
Adds a cache listener, MemoryCache listeners are used to notify classes when an item is removed from the cache.- Specified by:
addCacheListenerin interfaceMemoryCache- Parameters:
listener- The newCacheListener.- Throws:
java.lang.IllegalArgumentException- islisteneris null.
-
removeCacheListener
public void removeCacheListener(MemoryCache.CacheListener listener)
Removes a cache listener, objects using this listener will no longer receive notification of cache events.- Specified by:
removeCacheListenerin interfaceMemoryCache- Parameters:
listener- TheCacheListenerto remove.- Throws:
java.lang.IllegalArgumentException- iflisteneris null.
-
setCapacity
public void setCapacity(long newCapacity)
Sets the new capacity for the cache. When decreasing cache size, it is recommended to check that the lowWater variable is suitable. If the capacity infringes on items stored in the cache, these items are removed. Setting a new low water is up to the user, that is, it remains unchanged and may be higher than the maximum capacity. When the low water level is higher than or equal to the maximum capacity, it is ignored, which can lead to poor performance when adding entries.- Specified by:
setCapacityin interfaceMemoryCache- Parameters:
newCapacity- the new capacity of the cache.
-
setLowWater
public void setLowWater(long loWater)
Sets the new low water level in cache units, which controls how aggresively the cache discards items.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.
- Specified by:
setLowWaterin interfaceMemoryCache- Parameters:
loWater- the new low water level.
-
getLowWater
public long getLowWater()
Returns the low water level in cache units. When the cache fills, it removes items until it reaches the low water level.- Specified by:
getLowWaterin interfaceMemoryCache- Returns:
- the low water level.
-
contains
public boolean contains(java.lang.Object key)
Returns true if the cache contains the item referenced by key. No guarantee is made as to whether or not the item will remain in the cache for any period of time.This function does not cause the object referenced by the key to be marked as accessed.
getObject()should be used for that purpose.- Specified by:
containsin interfaceMemoryCache- Parameters:
key- The key of a specific object.- Returns:
- true if the cache holds the item referenced by key.
- Throws:
java.lang.IllegalArgumentException- ifkeyis null.
-
add
public boolean add(java.lang.Object key, java.lang.Object clientObject, long clientObjectSize)Adds an object to the cache. The add fails if the object or key is null, or if the size is zero, negative or greater than the maximmum capacity.- Specified by:
addin interfaceMemoryCache- Parameters:
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.- Returns:
- returns true if clientObject was added, false otherwise.
-
add
public boolean add(java.lang.Object key, Cacheable clientObject)Description copied from interface:MemoryCacheAttempts to add theCacheableobject 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
synchronizedwhen it is implemented.- Specified by:
addin interfaceMemoryCache- Parameters:
key- an object used to reference the cached item.clientObject- the item to be cached.- Returns:
- true if object was added, false otherwise.
- See Also:
Cacheable
-
remove
public void remove(java.lang.Object key)
Remove the object reference by key from the cache. If no object with the corresponding key is found, this method returns immediately.- Specified by:
removein interfaceMemoryCache- Parameters:
key- the key of the object to be removed.- Throws:
java.lang.IllegalArgumentException- ifkeyis null.
-
getObject
public java.lang.Object getObject(java.lang.Object key)
Obtain the object referenced by key without removing it. Apart from adding an object, this is the only way to mark an object as recently used.- Specified by:
getObjectin interfaceMemoryCache- Parameters:
key- The key for the object to be found.- Returns:
- the object referenced by key if it is present, null otherwise.
- Throws:
java.lang.IllegalArgumentException- ifkeyis null.
-
clear
public void clear()
Empties the cache.- Specified by:
clearin interfaceMemoryCache
-
removeEntry
protected void removeEntry(BasicMemoryCache.CacheEntry entry)
Removesentryfrom the cache. To remove an entry using its key, useremove().- Parameters:
entry- The entry (as opposed to key) of the item to be removed.
-
toString
public java.lang.String toString()
aStringrepresentation of this object is returned. This representation consists of maximum size, current used capacity and number of currently cached items.- Overrides:
toStringin classjava.lang.Object- Returns:
- a
Stringrepresentation of this object.
-
-