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 class
BasicMemoryCache.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.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 Summary
Constructors Constructor Description BasicMemoryCache(long loWater, long capacity)
Constructs a new cache usingcapacity
for maximum size, andloWater
for the low water.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(java.lang.Object key, Cacheable clientObject)
Attempts to add theCacheable
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.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 theMemoryCache
.java.lang.Object
getObject(java.lang.Object key)
Obtain the object referenced by key without removing it.long
getUsedCapacity()
Retrieves the amount of usedMemoryCache
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)
Removesentry
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()
aString
representation 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:MemoryCache
Retrieve the number of items stored in theMemoryCache
.- Specified by:
getNumObjects
in interfaceMemoryCache
- Returns:
- the number of objects currently stored in this cache.
-
getCapacity
public long getCapacity()
Description copied from interface:MemoryCache
Retrieves the maximum size of the cache.- Specified by:
getCapacity
in interfaceMemoryCache
- Returns:
- the capacity of the cache.
-
getUsedCapacity
public long getUsedCapacity()
Description copied from interface:MemoryCache
Retrieves the amount of usedMemoryCache
space. The value returned is in cache units.- Specified by:
getUsedCapacity
in interfaceMemoryCache
- Returns:
- the number of cache units that the cache currently holds.
-
getFreeCapacity
public long getFreeCapacity()
Description copied from interface:MemoryCache
Retrieves the available space for storing new items.- Specified by:
getFreeCapacity
in interfaceMemoryCache
- Returns:
- the amount of free space left in the cache (in cache units).
-
setName
public void setName(java.lang.String name)
- Specified by:
setName
in interfaceMemoryCache
-
getName
public java.lang.String getName()
- Specified by:
getName
in 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:
addCacheListener
in interfaceMemoryCache
- Parameters:
listener
- The newCacheListener
.- Throws:
java.lang.IllegalArgumentException
- islistener
is 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:
removeCacheListener
in interfaceMemoryCache
- Parameters:
listener
- TheCacheListener
to remove.- Throws:
java.lang.IllegalArgumentException
- iflistener
is 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:
setCapacity
in 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:
setLowWater
in 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:
getLowWater
in 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:
contains
in interfaceMemoryCache
- Parameters:
key
- The key of a specific object.- Returns:
- true if the cache holds the item referenced by key.
- Throws:
java.lang.IllegalArgumentException
- ifkey
is 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:
add
in 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:MemoryCache
Attempts to add theCacheable
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.- Specified by:
add
in 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:
remove
in interfaceMemoryCache
- Parameters:
key
- the key of the object to be removed.- Throws:
java.lang.IllegalArgumentException
- ifkey
is 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:
getObject
in 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
- ifkey
is null.
-
clear
public void clear()
Empties the cache.- Specified by:
clear
in interfaceMemoryCache
-
removeEntry
protected void removeEntry(BasicMemoryCache.CacheEntry entry)
Removesentry
from 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()
aString
representation of this object is returned. This representation consists of maximum size, current used capacity and number of currently cached items.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a
String
representation of this object.
-
-