public class DiskMemTileCache extends Observable implements javax.media.jai.TileCache
The default behaviour is to cache newly added tiles into memory. If the cache needs to free memory to accommodate a tile, it does so by removing lowest priority tiles from memory and caching them to disk. Optionally, the user can specify that newly added tiles are cached to disk immediately.
Unlike the standard JAI TileCache implementation, resident tiles are cached
using strong references. This is to support the use of this class with
DiskMemImage as well as operations that need to
cache tiles that are expensive to create (e.g. output of a time-consuming analysis).
A disadvantage of this design is that when the cache is being used for easily
generated tiles it can end up unnecessarily holding memory that is more urgently
required by other parts of an application. To avoid this happening, the cache can
be set to auto-flush resident tiles at regular intervals.
DiskCachedTile,
TileAccessTimeComparator| Modifier and Type | Field and Description |
|---|---|
static long |
DEFAULT_AUTO_FLUSH_MEMORY_INTERVAL
The default minimum period (2.5 seconds) of cache inactivity that
must elapse before memory-resident tiles are automatically flushed.
|
static long |
DEFAULT_MEMORY_CAPACITY
The default memory capacity in bytes (64 * 2^20 = 64Mb)
|
static float |
DEFAULT_MEMORY_THRESHOLD
The default memory threshold value (0.75)
|
static long |
DEFAULT_TILE_POLLING_INTERVAL
The default interval (2 seconds) for polling each tile to check if
its owning image has been garbage collected.
|
static String |
KEY_ALWAYS_DISK_CACHE
Key for the parameter controlling whether newly added tiles
are immediately cached to disk as well as in memory.
|
static String |
KEY_AUTO_FLUSH_MEMORY_ENABLED
Key for the parameter controlling whether the cache will auto-flush
memory-resident tiles.
|
static String |
KEY_AUTO_FLUSH_MEMORY_INTERVAL
Key for the cache auto-flush interval parameter.
|
static String |
KEY_INITIAL_MEMORY_CAPACITY
Key for the parameter controlling initial memory capacity of the
tile cache.
|
| Constructor and Description |
|---|
DiskMemTileCache()
Creates a new cache with all parameters set to their default values.
|
DiskMemTileCache(Map<String,Object> params)
Creates a new cache.
|
| Modifier and Type | Method and Description |
|---|---|
void |
accept(DiskMemTileCacheVisitor visitor)
Accepts a
DiskMemCacheVisitor object and calls its
visit method for each tile in the cache. |
void |
add(RenderedImage owner,
int tileX,
int tileY,
Raster data)
Adds a tile to the cache if not already present.
|
void |
add(RenderedImage owner,
int tileX,
int tileY,
Raster data,
Object tileCacheMetric)
Adds a tile to the cache if not already present.
|
void |
addTiles(RenderedImage owner,
Point[] tileIndices,
Raster[] tiles,
Object tileCacheMetric)
Adds all tiles for the given image to the cache.
|
boolean |
containsResidentTile(RenderedImage owner,
int tileX,
int tileY)
Checks whether a given tile is in this cache's memory storage.
|
boolean |
containsTile(RenderedImage owner,
int tileX,
int tileY)
Checks whether a given tile is in this cache.
|
void |
flush()
Removes ALL tiles from the cache: all resident tiles will be
removed from memory and all files for disk-cached tiles will
be discarded.
|
void |
flushMemory()
Removes all resident tiles from memory.
|
long |
getAutoFlushMemoryInterval()
Gets the current auto-flush interval.
|
long |
getCurrentMemory()
Gets the amount of memory currently being used for storage of
memory-resident tiles.
|
long |
getMemoryCapacity()
Gets the amount of memory, in bytes, allocated for storage of
resident tiles.
|
float |
getMemoryThreshold()
Returns the memory threshold, which is the fractional amount of cache memory
to retain during tile removal.
|
int |
getNumResidentTiles()
Gets the number of tiles currently residing in the
cache's memory storage.
|
int |
getNumTiles()
Gets the total number of tiles currently in the cache.
|
Raster |
getTile(RenderedImage owner,
int tileX,
int tileY)
Gets the specified tile from the cache if present.
|
int |
getTileCapacity()
Deprecated.
Please do not use this method
|
Comparator |
getTileComparator()
Gets the comparator currently used to assign memory-residence
priority to tiles.
|
long |
getTilePollingInterval()
Sets the interval between polling each tile to check if its owning image
has been garbage collected.
|
Raster[] |
getTiles(RenderedImage owner)
Gets all cached tiles associated with the given image.
|
Raster[] |
getTiles(RenderedImage owner,
Point[] tileIndices)
Gets the specified tiles for the given image.
|
boolean |
isAutoFlushMemoryEnabled()
Checks whether auto-flushing of memory-resident tiles is currently enabled.
|
void |
memoryControl()
Frees memory for resident tiles so that the fraction of memory occupied is
no more than the current value of the mamory threshold.
|
void |
remove(RenderedImage owner,
int tileX,
int tileY)
Removes a tile from the cache.
|
void |
removeTiles(RenderedImage owner)
Removes all tiles that belong to the given image from the cache.
|
void |
setAutoFlushMemoryEnabled(boolean enable)
Enables or disables auto-flushing of memory resident with the
currently set minimum interval.
|
void |
setAutoFlushMemoryInterval(long interval)
Sets the minimum period of cache inactivity, in milliseconds, that must
elapse before automatically flushing memory-resident tiles.
|
void |
setDiagnostics(boolean state)
Enables or disables the publishing of cache messages to Observers.
|
void |
setMemoryCapacity(long newCapacity)
Resets the memory capacity of the cache.
|
void |
setMemoryThreshold(float newThreshold)
Sets the memoryThreshold value to a floating point number that ranges from
0.0 to 1.0.
|
void |
setTileCapacity(int arg0)
Deprecated.
Please do not use this method
|
void |
setTileChanged(RenderedImage owner,
int tileX,
int tileY)
Informs the cache that a tile's data have changed.
|
void |
setTileComparator(Comparator comp)
Sets the comparator to use to assign memory-residence priority to
tiles.
|
void |
setTilePollingInterval(long interval)
Sets the interval between polling each tile to check if its owning image
has been garbage collected.
|
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObserverspublic static final long DEFAULT_MEMORY_CAPACITY
setMemoryCapacity(long),
Constant Field Valuespublic static final float DEFAULT_MEMORY_THRESHOLD
setMemoryThreshold(float),
Constant Field Valuespublic static final long DEFAULT_AUTO_FLUSH_MEMORY_INTERVAL
public static final long DEFAULT_TILE_POLLING_INTERVAL
public static final String KEY_INITIAL_MEMORY_CAPACITY
public static final String KEY_ALWAYS_DISK_CACHE
Boolean.FALSE (the default),
disk caching of tiles is deferred until required (ie. when
memory needs to be freed for other tiles).public static final String KEY_AUTO_FLUSH_MEMORY_ENABLED
Boolean.TRUE, auto-flushing of resident tiles will be enabled
when the cache is created. The default is Boolean.FALSE.public static final String KEY_AUTO_FLUSH_MEMORY_INTERVAL
public DiskMemTileCache()
public void add(RenderedImage owner, int tileX, int tileY, Raster data)
add in interface javax.media.jai.TileCacheowner - the image that this tile belongs totileX - the tile columntileY - the tile rowdata - the tile datapublic void add(RenderedImage owner, int tileX, int tileY, Raster data, Object tileCacheMetric)
add in interface javax.media.jai.TileCacheowner - the image that this tile belongs totileX - the tile columntileY - the tile rowdata - the tile datatileCacheMetric - optional tile cache metric (may be nullpublic void remove(RenderedImage owner, int tileX, int tileY)
remove in interface javax.media.jai.TileCacheowner - the image that this tile belongs totileX - the tile columntileY - the tile rowpublic Raster getTile(RenderedImage owner, int tileX, int tileY)
getTile in interface javax.media.jai.TileCacheowner - the image that the tile belongs totileX - the tile columntileY - the tile rownull if the tile was not cachedpublic Raster[] getTiles(RenderedImage owner)
getTiles in interface javax.media.jai.TileCacheowner - the image for which tiles are requestedpublic void removeTiles(RenderedImage owner)
removeTiles in interface javax.media.jai.TileCacheowner - the image owning the tiles to be removedpublic void setTilePollingInterval(long interval)
interval - interval in milliseconds
(values less than or equal to zero are ignored)public long getTilePollingInterval()
public void addTiles(RenderedImage owner, Point[] tileIndices, Raster[] tiles, Object tileCacheMetric)
addTiles in interface javax.media.jai.TileCacheowner - the image that the tiles belong totileIndices - an array of Points specifying the column-row coordinates
of each tiletiles - tile data in the form of Raster objectstileCacheMetric - optional metric (may be null)public Raster[] getTiles(RenderedImage owner, Point[] tileIndices)
getTiles in interface javax.media.jai.TileCacheowner - the image that the tiles belong totileIndices - an array of Points specifying the column-row coordinates
of each tilepublic void flush()
The update action of each tile will be set to DiskCachedTile#ACTION_REMOVED.
flush in interface javax.media.jai.TileCachepublic void flushMemory()
public void memoryControl()
memoryControl in interface javax.media.jai.TileCachesetMemoryThreshold(float)@Deprecated public void setTileCapacity(int arg0)
setTileCapacity in interface javax.media.jai.TileCachepublic int getTileCapacity()
getTileCapacity in interface javax.media.jai.TileCachepublic void setMemoryCapacity(long newCapacity)
setMemoryCapacity in interface javax.media.jai.TileCachenewCapacity - requested memory capacity for resident tilespublic long getMemoryCapacity()
getMemoryCapacity in interface javax.media.jai.TileCachepublic long getCurrentMemory()
public void setMemoryThreshold(float newThreshold)
setMemoryThreshold in interface javax.media.jai.TileCachenewThreshold - the new memory threshold between 0 and 1IllegalArgumentException - if the memoryThreshold is less than 0.0 or greater than 1.0public float getMemoryThreshold()
Boolean.TRUE.getMemoryThreshold in interface javax.media.jai.TileCachepublic void setTileComparator(Comparator comp)
comp is null the default comparator
(TileAccessTimeComparator) will be used.setTileComparator in interface javax.media.jai.TileCachecomp - the comparator or null for the defaultpublic Comparator getTileComparator()
getTileComparator in interface javax.media.jai.TileCachepublic int getNumTiles()
public int getNumResidentTiles()
public boolean containsTile(RenderedImage owner, int tileX, int tileY)
owner - the owning imagetileX - tile columntileY - tile rowtrue if the cache contains the tile; false otherwisepublic boolean containsResidentTile(RenderedImage owner, int tileX, int tileY)
owner - the owning imagetileX - tile columntileY - tile rowtrue if the tile is in cache memory; false otherwisepublic void setTileChanged(RenderedImage owner, int tileX, int tileY) throws TileNotResidentException, DiskCacheFailedException
getTile
request. If this is the case and the tile was previously written to
disk, then the cache's disk copy of the tile will be refreshed.
If the tile is not resident in memory, for instance
because of memory swapping for other tile accesses, the disk copy
will not be refreshed and a TileNotResidentException is
thrown.
owner - the owning imagetileX - tile columntileY - tile rowTileNotResidentException - if the tile is not residentDiskCacheFailedException - if the tile is cached to disk but its data could
not be updatedpublic final void setAutoFlushMemoryEnabled(boolean enable)
enable - true to enable auto-flushing; false to disablesetAutoFlushMemoryInterval(long)public boolean isAutoFlushMemoryEnabled()
true if the cache is auto-flushing; false otherwisepublic void setAutoFlushMemoryInterval(long interval)
interval - interval in milliseconds
(values less than or equal to zero are ignored)public long getAutoFlushMemoryInterval()
public void setDiagnostics(boolean state)
state - true to publish diagnostic messages; false to suppress thempublic void accept(DiskMemTileCacheVisitor visitor)
DiskMemCacheVisitor object and calls its
visit method for each tile in the cache.visitor - the visitorCopyright © 2009–2018. All rights reserved.