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 -
Constructor Summary
ConstructorsConstructorDescriptionForcedDiskCacheStorage(String storagePath) 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 TypeMethodDescriptiongetEntries(Collection<String> keys) Retrieves multiple cache entries from disk.org.apache.hc.client5.http.cache.HttpCacheEntryRetrieves a cache entry from disk if it exists and is still fresh.private booleanisFresh(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.voidStores a cache entry to disk.voidremoveEntry(String key) Removes a cache entry from disk.private StringsanitizeKey(String key) Sanitizes a cache key by replacing non-alphanumeric characters with underscores.voidupdateEntry(String key, org.apache.hc.client5.http.cache.HttpCacheCASOperation casOperation) Updates a cache entry using a compare-and-swap operation.
-
Field Details
-
storageDirectory
-
timeToLive
-
serializer
private final org.apache.hc.client5.http.impl.cache.HttpByteArrayCacheEntrySerializer serializer -
DEFAULT_TTL
Default time-to-live duration for cache entries (20 hours). -
LOG
private static final org.slf4j.Logger LOG
-
-
Constructor Details
-
ForcedDiskCacheStorage
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
Constructs a new ForcedDiskCacheStorage with a custom time-to-live.- Parameters:
storagePath- the directory path where cache entries will be storedtimeToLive- 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:
putEntryin interfaceorg.apache.hc.client5.http.cache.HttpCacheStorage- Parameters:
key- the cache keyentry- 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:
getEntryin interfaceorg.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
Removes a cache entry from disk.- Specified by:
removeEntryin interfaceorg.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:
updateEntryin interfaceorg.apache.hc.client5.http.cache.HttpCacheStorage- Parameters:
key- the cache keycasOperation- 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:
getEntriesin interfaceorg.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
Sanitizes a cache key by replacing non-alphanumeric characters with underscores.- Parameters:
key- the cache key to sanitize- Returns:
- the sanitized cache key
-