Class HeapCache

All Implemented Interfaces:
Cache, InfoCache, VariantCache

public final class HeapCache extends AbstractCache implements 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.)

  • Constructor Details

    • HeapCache

      public HeapCache()
  • Method Details

    • evict

      public void evict(Identifier identifier)
      Description copied from interface: Cache
      Deletes all cached content (source image, variant image(s), and info) corresponding to the image with the given identifier.
      Specified by:
      evict in interface Cache
      Parameters:
      identifier - Identifier of the image whose content to evict.
    • 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 interface Cache
    • purge

      public void purge()
      Description copied from interface: Cache
      Deletes the entire cache contents.
      Specified by:
      purge in interface Cache
    • evictInvalid

      public void evictInvalid()
      Evicts expired cache entries.
      Specified by:
      evictInvalid in interface Cache
    • shutdown

      public void shutdown()
      Description copied from interface: Cache

      Shuts down the instance, freeing any resource handles, stopping any worker threads, etc.

      The default implementation does nothing.

      Specified by:
      shutdown in interface Cache
      See Also:
    • evictInfos

      public void evictInfos()
      Description copied from interface: InfoCache
      Deletes all cached infos.
      Specified by:
      evictInfos in interface InfoCache
    • fetchInfo

      public Optional<Info> fetchInfo(Identifier identifier) throws IOException
      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 interface InfoCache
      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

      public void put(Identifier identifier, Info info) throws IOException
      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 interface InfoCache
      Parameters:
      identifier - Image identifier.
      info - Information about the image corresponding with the given identifier.
      Throws:
      IOException
    • put

      public void put(Identifier identifier, String info) throws IOException
      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 serialized Info instance that is deserializable by InfoCache.fetchInfo(Identifier).

      This method is used for testing. InfoCache.put(Identifier, Info) should normally be used instead.

      Specified by:
      put in interface InfoCache
      Parameters:
      identifier - Image identifier.
      info - JSON-encoded information about the image corresponding with the given identifier, obtained (for example) from Info.toJSON().
      Throws:
      IOException
    • evict

      public void evict(OperationList opList)
      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 interface VariantCache
      Parameters:
      opList -
    • newVariantImageInputStream

      public InputStream newVariantImageInputStream(OperationList opList, StatResult statResult)
      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 interface VariantCache
      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

      public CompletableOutputStream newVariantImageOutputStream(OperationList opList)
      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:

      Specified by:
      newVariantImageOutputStream in interface VariantCache
      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:
    • map

    • getNumVariantImages

      public long getNumVariantImages()
    • getNumInfos

      public long getNumInfos()
    • size

      public long size()
      Returns:
      Number of cached items of any kind.