Class ForcedDiskCacheStorage

java.lang.Object
io.github.jeremylong.openvulnerability.client.ForcedDiskCacheStorage
All Implemented Interfaces:
org.apache.hc.client5.http.cache.HttpCacheStorage

public class ForcedDiskCacheStorage extends Object implements org.apache.hc.client5.http.cache.HttpCacheStorage
Forced persistent cache storage. The implementation will cache the response to disk and consider it valid for a configurable duration, the default is 20 hours.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Duration
    Default time-to-live duration for cache entries (20 hours).
    private static final org.slf4j.Logger
     
    private final org.apache.hc.client5.http.impl.cache.HttpByteArrayCacheEntrySerializer
     
    private final Path
     
    private final Duration
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new ForcedDiskCacheStorage with the default time-to-live.
    ForcedDiskCacheStorage(String storagePath, Duration timeToLive)
    Constructs a new ForcedDiskCacheStorage with a custom time-to-live.
  • Method Summary

    Modifier and Type
    Method
    Description
    Map<String,org.apache.hc.client5.http.cache.HttpCacheEntry>
    Retrieves multiple cache entries from disk.
    org.apache.hc.client5.http.cache.HttpCacheEntry
    Retrieves a cache entry from disk if it exists and is still fresh.
    private boolean
    isFresh(org.apache.hc.client5.http.cache.HttpCacheEntry entry)
    Checks if a cache entry is still fresh based on its age and the configured time-to-live.
    void
    putEntry(String key, org.apache.hc.client5.http.cache.HttpCacheEntry entry)
    Stores a cache entry to disk.
    void
    Removes a cache entry from disk.
    private String
    Sanitizes a cache key by replacing non-alphanumeric characters with underscores.
    void
    updateEntry(String key, org.apache.hc.client5.http.cache.HttpCacheCASOperation casOperation)
    Updates a cache entry using a compare-and-swap operation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • storageDirectory

      private final Path storageDirectory
    • timeToLive

      private final Duration timeToLive
    • serializer

      private final org.apache.hc.client5.http.impl.cache.HttpByteArrayCacheEntrySerializer serializer
    • DEFAULT_TTL

      public static final Duration DEFAULT_TTL
      Default time-to-live duration for cache entries (20 hours).
    • LOG

      private static final org.slf4j.Logger LOG
  • Constructor Details

    • ForcedDiskCacheStorage

      public ForcedDiskCacheStorage(String storagePath) throws IOException
      Constructs a new ForcedDiskCacheStorage with the default time-to-live.
      Parameters:
      storagePath - the directory path where cache entries will be stored
      Throws:
      IOException - if the storage directory cannot be created
    • ForcedDiskCacheStorage

      public ForcedDiskCacheStorage(String storagePath, Duration timeToLive) throws IOException
      Constructs a new ForcedDiskCacheStorage with a custom time-to-live.
      Parameters:
      storagePath - the directory path where cache entries will be stored
      timeToLive - the duration for which cache entries are considered valid
      Throws:
      IOException - if the storage directory cannot be created
  • Method Details

    • putEntry

      public void putEntry(String key, org.apache.hc.client5.http.cache.HttpCacheEntry entry) throws org.apache.hc.client5.http.cache.ResourceIOException
      Stores a cache entry to disk.
      Specified by:
      putEntry in interface org.apache.hc.client5.http.cache.HttpCacheStorage
      Parameters:
      key - the cache key
      entry - the cache entry to store
      Throws:
      org.apache.hc.client5.http.cache.ResourceIOException - if writing the cache entry fails
    • getEntry

      public org.apache.hc.client5.http.cache.HttpCacheEntry getEntry(String key) throws org.apache.hc.client5.http.cache.ResourceIOException
      Retrieves a cache entry from disk if it exists and is still fresh.
      Specified by:
      getEntry in interface org.apache.hc.client5.http.cache.HttpCacheStorage
      Parameters:
      key - the cache key
      Returns:
      the cached entry if found and fresh, otherwise null
      Throws:
      org.apache.hc.client5.http.cache.ResourceIOException - if reading the cache entry fails
    • removeEntry

      public void removeEntry(String key) throws org.apache.hc.client5.http.cache.ResourceIOException
      Removes a cache entry from disk.
      Specified by:
      removeEntry in interface org.apache.hc.client5.http.cache.HttpCacheStorage
      Parameters:
      key - the cache key
      Throws:
      org.apache.hc.client5.http.cache.ResourceIOException - if deleting the cache entry fails
    • updateEntry

      public void updateEntry(String key, org.apache.hc.client5.http.cache.HttpCacheCASOperation casOperation) throws org.apache.hc.client5.http.cache.ResourceIOException
      Updates a cache entry using a compare-and-swap operation.
      Specified by:
      updateEntry in interface org.apache.hc.client5.http.cache.HttpCacheStorage
      Parameters:
      key - the cache key
      casOperation - the compare-and-swap operation to execute
      Throws:
      org.apache.hc.client5.http.cache.ResourceIOException - if updating the cache entry fails
    • getEntries

      public Map<String,org.apache.hc.client5.http.cache.HttpCacheEntry> getEntries(Collection<String> keys) throws org.apache.hc.client5.http.cache.ResourceIOException
      Retrieves multiple cache entries from disk.
      Specified by:
      getEntries in interface org.apache.hc.client5.http.cache.HttpCacheStorage
      Parameters:
      keys - the collection of cache keys
      Returns:
      a map of cache keys to entries for all found and fresh entries
      Throws:
      org.apache.hc.client5.http.cache.ResourceIOException - if reading cache entries fails
    • isFresh

      private boolean isFresh(org.apache.hc.client5.http.cache.HttpCacheEntry entry)
      Checks if a cache entry is still fresh based on its age and the configured time-to-live.
      Parameters:
      entry - the cache entry to check
      Returns:
      true if the entry is fresh, false otherwise
    • sanitizeKey

      private String sanitizeKey(String key)
      Sanitizes a cache key by replacing non-alphanumeric characters with underscores.
      Parameters:
      key - the cache key to sanitize
      Returns:
      the sanitized cache key