Class ImageCache
java.lang.Object
org.apache.xmlgraphics.image.loader.cache.ImageCache
This class provides a cache for images. The main key into the images is the original URI the
image was accessed with.
Don't use one ImageCache instance in the context of multiple base URIs because relative URIs would not work correctly anymore.
By default, the URIs of inaccessible images are remembered but these entries are discarded after 60 seconds (which causes a retry next time the same URI is requested). This allows to counteract performance loss when accessing invalid or temporarily unavailable images over slow connections.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ImageCacheListenerprivate SoftMapCacheprivate SoftMapCacheprivate ExpirationPolicyprivate Mapprivate longprotected static final org.apache.commons.logging.Logloggerprivate static final longprivate TimeStampProvider -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor with default settings.ImageCache(TimeStampProvider timeStampProvider, ExpirationPolicy invalidURIExpirationPolicy) Constructor for customized behaviour and testing. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the image cache (all ImageInfo and Image objects).private voidvoidTriggers some house-keeping, i.e.private voidgetImage(String uri, ImageFlavor flavor) Returns an image from the cache or null if it wasn't found.getImage(ImageInfo info, ImageFlavor flavor) Returns an image from the cache or null if it wasn't found.protected ImageInfogetImageInfo(String uri) Returns an ImageInfo instance from the cache or null if none is found.booleanisInvalidURI(String uri) Indicates whether a URI has previously been identified as an invalid URI.needImageInfo(String uri, ImageSessionContext session, ImageManager manager) Returns an ImageInfo instance for a given URI.voidRegisters an image with the cache.protected voidputImageInfo(ImageInfo info) Registers an ImageInfo instance with the cache.(package private) voidregisterInvalidURI(String uri) Registers a URI as invalid so getImageInfo can indicate that quickly with no I/O access.private booleanvoidsetCacheListener(ImageCacheListener listener) Sets an ImageCacheListener instance so the events in the image cache can be observed.
-
Field Details
-
log
protected static final org.apache.commons.logging.Log loglogger -
invalidURIs
-
invalidURIExpirationPolicy
-
imageInfos
-
images
-
cacheListener
-
timeStampProvider
-
lastHouseKeeping
private long lastHouseKeeping -
ONE_HOUR
private static final long ONE_HOUR- See Also:
-
-
Constructor Details
-
ImageCache
public ImageCache()Default constructor with default settings. -
ImageCache
Constructor for customized behaviour and testing.- Parameters:
timeStampProvider- the time stamp provider to useinvalidURIExpirationPolicy- the expiration policy for invalid URIs
-
-
Method Details
-
setCacheListener
Sets an ImageCacheListener instance so the events in the image cache can be observed.- Parameters:
listener- the listener instance
-
needImageInfo
public ImageInfo needImageInfo(String uri, ImageSessionContext session, ImageManager manager) throws ImageException, IOException Returns an ImageInfo instance for a given URI.- Parameters:
uri- the image's URIsession- the session contextmanager- the ImageManager handling the images- Returns:
- the ImageInfo instance
- Throws:
ImageException- if an error occurs while parsing image dataIOException- if an I/O error occurs while loading image data
-
isInvalidURI
Indicates whether a URI has previously been identified as an invalid URI.- Parameters:
uri- the image's URI- Returns:
- true if the URI is invalid
-
removeInvalidURIIfExpired
-
getImageInfo
-
putImageInfo
Registers an ImageInfo instance with the cache.- Parameters:
info- the ImageInfo instance
-
registerInvalidURI
Registers a URI as invalid so getImageInfo can indicate that quickly with no I/O access.- Parameters:
uri- the URI of the invalid image
-
getImage
Returns an image from the cache or null if it wasn't found.- Parameters:
info- the ImageInfo instance representing the imageflavor- the requested ImageFlavor for the image- Returns:
- the requested image or null if the image is not in the cache
-
getImage
Returns an image from the cache or null if it wasn't found.- Parameters:
uri- the image's URIflavor- the requested ImageFlavor for the image- Returns:
- the requested image or null if the image is not in the cache
-
putImage
-
clearCache
public void clearCache()Clears the image cache (all ImageInfo and Image objects). -
considerHouseKeeping
private void considerHouseKeeping() -
doHouseKeeping
public void doHouseKeeping()Triggers some house-keeping, i.e. removes stale entries. -
doInvalidURIHouseKeeping
private void doInvalidURIHouseKeeping()
-