Class HeapCache
- All Implemented Interfaces:
Cache
,InfoCache
,VariantCache
Heap-based LRU cache.
This implementation can be size-limited in addition to time-limited. When
the target size (Key.HEAPCACHE_TARGET_SIZE
) has been
exceeded, the minimum number of least-recently-accessed items are purged
that will reduce it back down to this size. (The configured target size may
be safely changed while the application is running.)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
static class
Cached item key.static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
evict
(Identifier identifier) Deletes all cached content (source image, variant image(s), and info) corresponding to the image with the given identifier.void
evict
(OperationList opList) Deletes the cached image corresponding to the given operation list.void
Deletes all cached infos.void
Evicts expired cache entries.fetchInfo
(Identifier identifier) Reads the cached image information corresponding to the given identifier.long
long
void
Implementations should perform all necessary initialization in this method rather than a constructor or static initializer.map()
newVariantImageInputStream
(OperationList opList, StatResult statResult) Returns an input stream corresponding to the given operation list, ornull
if a valid image corresponding to the given operation list does not exist in the cache.Returns an output stream for writing an image to the cache.void
purge()
Deletes the entire cache contents.void
put
(Identifier identifier, Info info) Synchronously adds image information to the cache.void
put
(Identifier identifier, String info) Alternative toInfoCache.put(Identifier, Info)
that adds a raw UTF-8 string to the cache, trusting that it is a serializedInfo
instance that is deserializable byInfoCache.fetchInfo(Identifier)
.void
shutdown()
Shuts down the instance, freeing any resource handles, stopping any worker threads, etc.long
size()
Methods inherited from class is.galia.cache.AbstractCache
addObserver, getAllObservers
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface is.galia.cache.Cache
addObserver, cleanUp, onCacheWorker
Methods inherited from interface is.galia.cache.VariantCache
exists, newVariantImageInputStream
-
Constructor Details
-
HeapCache
public HeapCache()
-
-
Method Details
-
evict
Description copied from interface:Cache
Deletes all cached content (source image, variant image(s), and info) corresponding to the image with the given identifier. -
initialize
public void initialize()Description copied from interface:Cache
Implementations should perform all necessary initialization in this method rather than a constructor or static initializer.
The default implementation does nothing.
- Specified by:
initialize
in interfaceCache
-
purge
-
evictInvalid
public void evictInvalid()Evicts expired cache entries.- Specified by:
evictInvalid
in interfaceCache
-
shutdown
-
evictInfos
public void evictInfos()Description copied from interface:InfoCache
Deletes all cached infos.- Specified by:
evictInfos
in interfaceInfoCache
-
fetchInfo
Description copied from interface:InfoCache
Reads the cached image information corresponding to the given identifier.
If invalid image information exists in the cache, implementations should delete it—ideally asynchronously.
- Specified by:
fetchInfo
in interfaceInfoCache
- Parameters:
identifier
- Image identifier for which to retrieve information.- Returns:
- Info corresponding to the given identifier, or
Optional.empty()
if no such instance exists. - Throws:
IOException
-
put
Description copied from interface:InfoCache
Synchronously adds image information to the cache.
If the information corresponding to the given identifier already exists, it will be overwritten.
- Specified by:
put
in interfaceInfoCache
- Parameters:
identifier
- Image identifier.info
- Information about the image corresponding with the given identifier.- Throws:
IOException
-
put
Description copied from interface:InfoCache
Alternative to
InfoCache.put(Identifier, Info)
that adds a raw UTF-8 string to the cache, trusting that it is a serializedInfo
instance that is deserializable byInfoCache.fetchInfo(Identifier)
.This method is used for testing.
InfoCache.put(Identifier, Info)
should normally be used instead.- Specified by:
put
in interfaceInfoCache
- Parameters:
identifier
- Image identifier.info
- JSON-encoded information about the image corresponding with the given identifier, obtained (for example) fromInfo.toJSON()
.- Throws:
IOException
-
evict
Description copied from interface:VariantCache
Deletes the cached image corresponding to the given operation list.
If no such image exists, nothing is done.
- Specified by:
evict
in interfaceVariantCache
- Parameters:
opList
-
-
newVariantImageInputStream
Description copied from interface:VariantCache
Returns an input stream corresponding to the given operation list, or
null
if a valid image corresponding to the given operation list does not exist in the cache.If an invalid image corresponding to the given operation list exists in the cache, implementations should delete it (ideally asynchronously) and return
null
.- Specified by:
newVariantImageInputStream
in interfaceVariantCache
- Parameters:
opList
- Operation list for which to retrieve an input stream for reading from the cache.statResult
- May be populated with metadata about the cached image if one is available and if the implementation supports it.- Returns:
- Stream corresponding to the given operation list, or
null
if a valid image does not exist in the cache.
-
newVariantImageOutputStream
Description copied from interface:VariantCache
Returns an output stream for writing an image to the cache.
If an image corresponding to the given identifier already exists, the stream should overwrite it. Implementations may choose to allow multiple streams to write data to the same target concurrently (assuming this is safe), or else allow only one stream to write to a particular target at a time, with other clients writing to no-op streams.
Important notes about the
OutputStream.close()
implementation:- It must check the return value of
CompletableOutputStream.isComplete()
before committing any data to the cache. If it returnsfalse
, any written data should be discarded. - It must
notify all observers
when it completes.
- Specified by:
newVariantImageOutputStream
in interfaceVariantCache
- Parameters:
opList
- Operation list describing the target image in the cache.- Returns:
- Output stream to which an image corresponding to the given operation list can be written.
- See Also:
- It must check the return value of
-
map
-
getNumVariantImages
public long getNumVariantImages() -
getNumInfos
public long getNumInfos() -
size
public long size()- Returns:
- Number of cached items of any kind.
-