Class ImCache
Core class which eases communication between the request system (ImRequest
) and the caching system (ImgCache
).
A single instance of this class should cover most use cases, that's why it offers a 'default' instance via
instance()
; however, the no-args constructor is left public in case one needs separate caches.
A request is generated starting from the source URL
for a certain resource, which is the most generic way to
represent a resource; it can be a network resource, a file or something else. Once a request's execution successfully
terminates, it asks the associated ImCache
instance to save the result, see store(ImRequest, ImImage, ImImage)
.
While removals
are delegated directly to the Cache
, get
operations should be done by creating a new request.
A resource may or may not be cached anymore at a given time. ImRequest
uses ImRequest.RequestState.CACHE_HIT
to indicate
that the resource was found in the cache, and ImRequest.RequestState.SUCCEEDED
to indicate that the resource was loaded from
its URL
.
Of course, since ImCache
allows getting the ImgCache
instance with storage()
, you can perform operations directly
on the cache data structure, although it's not recommended unless you really need it for your use case.
The last core concept is Transforms. ImCache
does not provide only a way to load and cache images, but
also a way to modify them via the Transform.transform(BufferedImage)
API. Transformations are applied by ImRequest
after the original resource is loaded. At that point we can store the results, but this raises a question: which one
do you want to save? ImCache
allows you to control this behavior by setting the ImCache.StoreStrategy
,
see store(ImRequest, ImImage, ImImage)
. /// The reason the system doesn't allow caching both is to save space and for simplicity
Most of the time you probably want to cache the original resource, as transform operations are relatively fast.
Defaults & Config
- By default, the store strategy is set to
ImCache.StoreStrategy.SAVE_ORIGINAL
, can be changed via
invalid reference
#storeStrategy(StoreStrategy)
- By default, this uses an in-memory cache (
MemoryCache
), can be changed viacacheConfig(Supplier)
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncacheConfig
(Supplier<ImgCache<?>> config) Replaces the current cache with a new one generated by the given config supplier.clear()
Delegates toImgCache.clear()
.static ImCache
instance()
boolean
Delegates toremove(WithID)
.boolean
Delegates toImgCache.remove(String)
.Delegates torequest(File)
.setStoreStrategy
(ImCache.StoreStrategy storeStrategy) Sets theImCache.StoreStrategy
to be used when saving the result of a request.ImgCache
<?> storage()
protected void
Stores a completed request in the cache by itsImRequest.id()
.
-
Constructor Details
-
ImCache
public ImCache()
-
-
Method Details
-
instance
-
request
-
request
-
request
-
request
Delegates torequest(File)
. -
store
Stores a completed request in the cache by its
ImRequest.id()
. The image to be saved is either the original or the transformed one, depending on theImCache.StoreStrategy
.Note that if no transforms were applied, the
src
and theout
parameters will be the same. In other words, no matter the strategy, the original will be saved.If the image to be saved is
null
, nothing happens. -
remove
Delegates toImgCache.remove(String)
. -
remove
Delegates toremove(WithID)
. -
clear
Delegates toImgCache.clear()
. -
cacheConfig
-
setStoreStrategy
Sets theImCache.StoreStrategy
to be used when saving the result of a request.- See Also:
-
storage
-
getSaveStrategy
- Returns:
- the
ImCache.StoreStrategy
used by thisImCache
to determine which image to save when a request completes successfully - See Also:
-