Class HttpCache
java.lang.Object
com.github.mizosoft.methanol.HttpCache
- All Implemented Interfaces:
Flushable, AutoCloseable
An HTTP cache that
resides between a
Methanol client and its backend HttpClient.
An HttpCache instance is utilized by configuring it with Methanol.Builder.cache(HttpCache). The cache operates by inserting itself as an
Interceptor that can short-circuit requests by serving responses from a specified storage.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA builder ofHttpCaches.static interfaceA listener to request/response & read/write events within the cache.static interfaceStatistics of anHttpCache.static interfaceStrategy for recodingHttpCachestatistics. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from this cache.voidclose()Closes this cache.Returns the directory used by this cache if entries are being cached on disk.voiddispose()Atomically clears and closes this cache.voidenable(boolean on) Sets whether this cache is enabled.executor()Returns anOptionalcontaining this cache's executor if one is explicitly set.voidflush()voidDeprecated, for removal: This API element is subject to removal in a future version.Deprecated, for removal: This API element is subject to removal in a future version.As of1.8.0, a cache is always initialized when created.booleanReturns whether this cache isenabled.listener()Returns theHttpCache.Listenerset by the user.longmaxSize()Returns this cache's max size in bytes.static HttpCache.BuilderReturns a newHttpCache.Builder.booleanremove(HttpRequest request) Removes the entry associated with the given request if one is present.booleanRemoves the entry associated with the given URI if one is present.longsize()Returns the size this cache occupies in bytes.stats()Returns a snapshot of statistics accumulated so far.Returns a snapshot of statistics accumulated so far for the givenURI.toString()uris()Returns an iterator over theURIsof responses known to this cache.
-
Method Details
-
directory
-
maxSize
public long maxSize()Returns this cache's max size in bytes. -
executor
-
listener
Returns theHttpCache.Listenerset by the user. -
size
Returns the size this cache occupies in bytes.- Throws:
IOException
-
stats
Returns a snapshot of statistics accumulated so far. -
stats
Returns a snapshot of statistics accumulated so far for the givenURI. -
initialize
Deprecated, for removal: This API element is subject to removal in a future version.As of1.8.0, a cache is always initialized when created.Initializes this cache. A cache that operates on disk needs to initialize its in-memory data structures before usage to restore indexing data from previous sessions. Initialization entails reading index files, iterating over entries available on cache's directory and possibly creating new index files.The cache initializes itself automatically on first use. An application might choose to call this method (or
initializeAsync()) during its startup sequence to allow the cache to operate directly when it's first used. -
initializeAsync
Deprecated, for removal: This API element is subject to removal in a future version.As of1.8.0, a cache is always initialized when created.Asynchronouslyinitializesthis cache -
uris
Returns an iterator over theURIsof responses known to this cache. The returned iterator supports removal.- Throws:
IOException
-
clear
Removes all entries from this cache.- Throws:
IllegalStateException- if closedIOException
-
remove
Removes the entry associated with the given URI if one is present.- Throws:
IllegalStateException- if closedIOException
-
remove
Removes the entry associated with the given request if one is present.- Throws:
IllegalStateException- if closedIOException
-
enable
public void enable(boolean on) Sets whether this cache is enabled. A disabled cache neither reads from nor writes to underlying storage, just as if it was never installed. Caches start enabled, and can be later disabled and re-enabled any number of times.This function is intended to be used to disable the cache when underlying storage disconnects or becomes faulty, and consequently reading from or writing to the cache always fails. This can be detected when too many
writeand/orreadfailures occur. -
isEnabled
public boolean isEnabled()Returns whether this cache isenabled. -
dispose
Atomically clears and closes this cache.- Throws:
IllegalStateException- if closedIOException
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
Closes this cache. Attempting to operate on a closed cache either directly (e.g. removing an entry) or indirectly (e.g. sending requests over a client that uses this cache) will likely cause anIllegalStateException(or perhaps some other exception) to be thrown.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
toString
-
newBuilder
Returns a newHttpCache.Builder.
-
1.8.0, a cache is always initialized when created.