|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectto.etc.domui.caches.images.ImageCache
public class ImageCache
This cache handles images and transformations of images.
Image originals can come from many sources: database tables, file system files etc. The source never needs to be
"duplicated" (in long-term cache like the database) but can be obtained in some way. This code generalizes the retrieval
process for an original image into the IImageRetriever
interface using a factory pattern. The factory (IIMageRetriever) to
use is defined by the retriever's name which is a small string. The image retriever, when asked, must deliver some basic information
on the image and a stream source to accesss it's content.
The Image Retriever retrieves a specific image using a retriever-specific key string called the retriever key. This
key string's value is only meaningful to the specific retriever it is meant for. The retriever decodes the string value into
a primary key, filename or whatnot and uses that to access the actual image. The combination of an IImageRetriever and a
retriever key uniquely identifies an original image. Internally this identity is maintained in ImageKey
. For external
purposes the retriever itself can be specified as a string too: the retriever's name. This combination of retriever name and retriever
key can be used in URL's to access a given original image. This is the task of the CachedImagePart
part.
Getting an original image is nice but it's often not really needed - we usually need to have some specific transformed version of the original, like:
We cache images for better performance for often-used images like thumbnail of the current set of properties that are available for rent. We also cache metadata on images (format, size) to allow code to properly generate whatever HTML is needed to access the image.
Since many transformations on an image are very expensive we cache the result of those transformations too. These transformations are cached in database tables if so defined. They are always cached on the file system.
We distinguish between retrieving image specifications and the image data. The image specifications are things like it's mime type, the number of pages (if paged) and it's size. These are all the result of the identification phase of the original image or of an actual transformation process.
We use a multi-level lock using the standard mechanism. The fast lock is the ImageCache instance itself; it protects the registered factory list and the map from ImageKey to ImageRoot. It also handles LRU processing for the individual image instances.
The 2nd level lock is a lock on ImageRoot which is locked for any access to whatever permutation of that image. This second-level lock is a slow lock meaning that any time-consuming operation on any of the derivatives of the image will keep this lock closed.
Access to metadata of the image can be done by just requesting it. The data is cached but will never change, and if the cache entry disappears while you are using that metadata nothing special happens - the data will be garbage collected as soon as you release your reference to it.
This is not the case for image data. This data is cached in memory as a set of buffers, an optional extra file and data related to all this. While someone is using this data it cannot be released (the file cannot be deleted). To prevent this image data has a use count. This use count is at least 1 as long as the data is accessible in the cache and gets incremented for every reference to the data that is returned to the user. The user must release this data explicitly with a call to close which decreases the use count. If the use count becomes zero then the resources for image data will be released.
Method Summary | |
---|---|
void |
addRetriever(IImageRetriever r)
Add a new image factory. |
ImageKey |
createImageKey(java.lang.String retrieverkey,
java.lang.String instancekey)
|
IImageRetriever |
findRetriever(java.lang.String key)
|
FullImage |
getFullImage(ImageKey k,
java.util.List<IImageConversionSpecifier> convlist)
Get full image data: both the data source AND it's info. |
IImageStreamSource |
getImageData(ImageKey k,
java.util.List<IImageConversionSpecifier> convlist)
|
ImageInfo |
getImageInfo(ImageKey k,
java.util.List<IImageConversionSpecifier> convlist)
|
static ImageCache |
getInstance()
Get the singleton image cache. |
IImageStreamSource |
getOriginalData(ImageKey k)
Return a data reference to the original image's data. |
ImageInfo |
getOriginalInfo(ImageKey k)
Return a data reference to the original image's info. |
static void |
initialize(long maxsize,
long maxfilesize,
java.io.File cacheDir)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static ImageCache getInstance()
public static void initialize(long maxsize, long maxfilesize, java.io.File cacheDir) throws java.lang.Exception
java.lang.Exception
public void addRetriever(IImageRetriever r)
r
- public IImageRetriever findRetriever(java.lang.String key)
public ImageKey createImageKey(java.lang.String retrieverkey, java.lang.String instancekey)
public IImageStreamSource getOriginalData(ImageKey k) throws java.lang.Exception
k
-
java.lang.Exception
public ImageInfo getOriginalInfo(ImageKey k) throws java.lang.Exception
k
-
java.lang.Exception
public IImageStreamSource getImageData(ImageKey k, java.util.List<IImageConversionSpecifier> convlist) throws java.lang.Exception
k
- convlist
-
java.lang.Exception
public ImageInfo getImageInfo(ImageKey k, java.util.List<IImageConversionSpecifier> convlist) throws java.lang.Exception
java.lang.Exception
public FullImage getFullImage(ImageKey k, java.util.List<IImageConversionSpecifier> convlist) throws java.lang.Exception
k
- convlist
-
java.lang.Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |