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 classstatic classstatic classCached item key.static class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidevict(Identifier identifier) Deletes all cached content (source image, variant image(s), and info) corresponding to the image with the given identifier.voidevict(OperationList opList) Deletes the cached image corresponding to the given operation list.voidDeletes all cached infos.voidEvicts expired cache entries.fetchInfo(Identifier identifier) Reads the cached image information corresponding to the given identifier.longlongvoidImplementations 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, ornullif 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.voidpurge()Deletes the entire cache contents.voidput(Identifier identifier, Info info) Synchronously adds image information to the cache.voidput(Identifier identifier, String info) Alternative toInfoCache.put(Identifier, Info)that adds a raw UTF-8 string to the cache, trusting that it is a serializedInfoinstance that is deserializable byInfoCache.fetchInfo(Identifier).voidshutdown()Shuts down the instance, freeing any resource handles, stopping any worker threads, etc.longsize()Methods inherited from class is.galia.cache.AbstractCache
addObserver, getAllObserversMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface is.galia.cache.Cache
addObserver, cleanUp, onCacheWorkerMethods inherited from interface is.galia.cache.VariantCache
exists, newVariantImageInputStream
-
Constructor Details
-
HeapCache
public HeapCache()
-
-
Method Details
-
evict
Description copied from interface:CacheDeletes 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:CacheImplementations should perform all necessary initialization in this method rather than a constructor or static initializer.
The default implementation does nothing.
- Specified by:
initializein interfaceCache
-
purge
-
evictInvalid
public void evictInvalid()Evicts expired cache entries.- Specified by:
evictInvalidin interfaceCache
-
shutdown
-
evictInfos
public void evictInfos()Description copied from interface:InfoCacheDeletes all cached infos.- Specified by:
evictInfosin interfaceInfoCache
-
fetchInfo
Description copied from interface:InfoCacheReads 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:
fetchInfoin 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:InfoCacheSynchronously adds image information to the cache.
If the information corresponding to the given identifier already exists, it will be overwritten.
- Specified by:
putin interfaceInfoCache- Parameters:
identifier- Image identifier.info- Information about the image corresponding with the given identifier.- Throws:
IOException
-
put
Description copied from interface:InfoCacheAlternative to
InfoCache.put(Identifier, Info)that adds a raw UTF-8 string to the cache, trusting that it is a serializedInfoinstance that is deserializable byInfoCache.fetchInfo(Identifier).This method is used for testing.
InfoCache.put(Identifier, Info)should normally be used instead.- Specified by:
putin 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:VariantCacheDeletes the cached image corresponding to the given operation list.
If no such image exists, nothing is done.
- Specified by:
evictin interfaceVariantCache- Parameters:
opList-
-
newVariantImageInputStream
Description copied from interface:VariantCacheReturns an input stream corresponding to the given operation list, or
nullif 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:
newVariantImageInputStreamin 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
nullif a valid image does not exist in the cache.
-
newVariantImageOutputStream
Description copied from interface:VariantCacheReturns 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 observerswhen it completes.
- Specified by:
newVariantImageOutputStreamin 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.
-