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
Modifier and TypeClassDescriptionstatic final class
A builder ofHttpCaches
.static interface
A listener to request/response & read/write events within the cache.static interface
Statistics of anHttpCache
.static interface
Strategy for recodingHttpCache
statistics. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from this cache.void
close()
Closes this cache.Returns the directory used by this cache if entries are being cached on disk.void
dispose()
Atomically clears and closes this cache.executor()
Returns anOptional
containing this cache's executor if one is explicitly set.void
flush()
void
Deprecated, 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.listener()
Returns theHttpCache.Listener
set by the user.long
maxSize()
Returns this cache's max size in bytes.static HttpCache.Builder
Returns a newHttpCache.Builder
.boolean
remove
(HttpRequest request) Removes the entry associated with the given request if one is present.boolean
Removes the entry associated with the given URI if one is present.long
size()
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
.uris()
Returns an iterator over theURIs
of responses known to this cache.
-
Method Details
-
directory
Returns the directory used by this cache if entries are being cached on disk. -
maxSize
public long maxSize()Returns this cache's max size in bytes. -
executor
Returns anOptional
containing this cache's executor if one is explicitly set. -
listener
Returns theHttpCache.Listener
set 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. For background initialization, please useHttpCache.Builder.buildAsync()
.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.- Throws:
IOException
-
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. For background initialization, please useHttpCache.Builder.buildAsync()
.Asynchronouslyinitializes
this cache -
uris
Returns an iterator over theURIs
of 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
-
dispose
Atomically clears and closes this cache.- Throws:
IllegalStateException
- if closedIOException
-
flush
- Specified by:
flush
in 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:
close
in interfaceAutoCloseable
- Throws:
IOException
-
newBuilder
Returns a newHttpCache.Builder
.
-
1.8.0
, a cache is always initialized when created.