public class ImageCache
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
private ImageCacheListener |
cacheListener |
private SoftMapCache |
imageInfos |
private SoftMapCache |
images |
private ExpirationPolicy |
invalidURIExpirationPolicy |
private java.util.Map |
invalidURIs |
private long |
lastHouseKeeping |
protected static org.apache.commons.logging.Log |
log
logger
|
private static long |
ONE_HOUR |
private TimeStampProvider |
timeStampProvider |
Constructor and Description |
---|
ImageCache()
Default constructor with default settings.
|
ImageCache(TimeStampProvider timeStampProvider,
ExpirationPolicy invalidURIExpirationPolicy)
Constructor for customized behaviour and testing.
|
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clears the image cache (all ImageInfo and Image objects).
|
private void |
considerHouseKeeping() |
void |
doHouseKeeping()
Triggers some house-keeping, i.e.
|
private void |
doInvalidURIHouseKeeping() |
Image |
getImage(ImageInfo info,
ImageFlavor flavor)
Returns an image from the cache or null if it wasn't found.
|
Image |
getImage(java.lang.String uri,
ImageFlavor flavor)
Returns an image from the cache or null if it wasn't found.
|
protected ImageInfo |
getImageInfo(java.lang.String uri)
Returns an ImageInfo instance from the cache or null if none is found.
|
boolean |
isInvalidURI(java.lang.String uri)
Indicates whether a URI has previously been identified as an invalid URI.
|
ImageInfo |
needImageInfo(java.lang.String uri,
ImageSessionContext session,
ImageManager manager)
Returns an ImageInfo instance for a given URI.
|
void |
putImage(Image img)
Registers an image with the cache.
|
protected void |
putImageInfo(ImageInfo info)
Registers an ImageInfo instance with the cache.
|
(package private) void |
registerInvalidURI(java.lang.String uri)
Registers a URI as invalid so getImageInfo can indicate that quickly with no I/O access.
|
private boolean |
removeInvalidURIIfExpired(java.lang.String uri) |
void |
setCacheListener(ImageCacheListener listener)
Sets an ImageCacheListener instance so the events in the image cache can be observed.
|
protected static final org.apache.commons.logging.Log log
private java.util.Map invalidURIs
private ExpirationPolicy invalidURIExpirationPolicy
private SoftMapCache imageInfos
private SoftMapCache images
private ImageCacheListener cacheListener
private TimeStampProvider timeStampProvider
private long lastHouseKeeping
private static final long ONE_HOUR
public ImageCache()
public ImageCache(TimeStampProvider timeStampProvider, ExpirationPolicy invalidURIExpirationPolicy)
timeStampProvider
- the time stamp provider to useinvalidURIExpirationPolicy
- the expiration policy for invalid URIspublic void setCacheListener(ImageCacheListener listener)
listener
- the listener instancepublic ImageInfo needImageInfo(java.lang.String uri, ImageSessionContext session, ImageManager manager) throws ImageException, java.io.IOException
uri
- the image's URIsession
- the session contextmanager
- the ImageManager handling the imagesImageException
- if an error occurs while parsing image datajava.io.IOException
- if an I/O error occurs while loading image datapublic boolean isInvalidURI(java.lang.String uri)
uri
- the image's URIprivate boolean removeInvalidURIIfExpired(java.lang.String uri)
protected ImageInfo getImageInfo(java.lang.String uri)
uri
- the image's URIprotected void putImageInfo(ImageInfo info)
info
- the ImageInfo instancevoid registerInvalidURI(java.lang.String uri)
uri
- the URI of the invalid imagepublic Image getImage(ImageInfo info, ImageFlavor flavor)
info
- the ImageInfo instance representing the imageflavor
- the requested ImageFlavor for the imagepublic Image getImage(java.lang.String uri, ImageFlavor flavor)
uri
- the image's URIflavor
- the requested ImageFlavor for the imagepublic void putImage(Image img)
img
- the imagepublic void clearCache()
private void considerHouseKeeping()
public void doHouseKeeping()
private void doInvalidURIHouseKeeping()