- java.lang.Object
-
- com.github.mizosoft.methanol.HttpCache
-
- All Implemented Interfaces:
java.io.Flushable,java.lang.AutoCloseable
public final class HttpCache extends java.lang.Object implements java.lang.AutoCloseable, java.io.FlushableAn HTTP cache that resides between aMethanolclient and its backendHttpClient.An
HttpCacheinstance is utilized by configuring it withMethanol.Builder.cache(HttpCache). The cache operates by inserting itself as anInterceptorthat can short-circuit requests by serving responses from a specified storage.- See Also:
- Caching with Methanol
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHttpCache.BuilderA builder ofHttpCaches.static interfaceHttpCache.ListenerA listener to request/response & read/write events within the cache.static interfaceHttpCache.StatsStatistics of anHttpCache.static interfaceHttpCache.StatsRecorderStrategy for recodingHttpCachestatistics.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclear()Removes all entries from this cache.voidclose()Closes this cache.java.util.Optional<java.nio.file.Path>directory()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.java.util.Optional<java.util.concurrent.Executor>executor()Returns anOptionalcontaining this cache's executor if one is explicitly set.voidflush()voidinitialize()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.java.util.concurrent.CompletableFuture<java.lang.Void>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.booleanisEnabled()Returns whether this cache isenabled.java.util.Optional<HttpCache.Listener>listener()Returns theHttpCache.Listenerset by the user.longmaxSize()Returns this cache's max size in bytes.static HttpCache.BuildernewBuilder()Returns a newHttpCache.Builder.booleanremove(java.net.http.HttpRequest request)Removes the entry associated with the given request if one is present.booleanremove(java.net.URI uri)Removes the entry associated with the given URI if one is present.longsize()Returns the size this cache occupies in bytes.HttpCache.Statsstats()Returns a snapshot of statistics accumulated so far.HttpCache.Statsstats(java.net.URI uri)Returns a snapshot of statistics accumulated so far for the givenURI.java.lang.StringtoString()java.util.Iterator<java.net.URI>uris()Returns an iterator over theURIsof responses known to this cache.
-
-
-
Method Detail
-
directory
public java.util.Optional<java.nio.file.Path> 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
public java.util.Optional<java.util.concurrent.Executor> executor()
Returns anOptionalcontaining this cache's executor if one is explicitly set.
-
listener
public java.util.Optional<HttpCache.Listener> listener()
Returns theHttpCache.Listenerset by the user.
-
size
public long size() throws java.io.IOExceptionReturns the size this cache occupies in bytes.- Throws:
java.io.IOException
-
stats
public HttpCache.Stats stats()
Returns a snapshot of statistics accumulated so far.
-
stats
public HttpCache.Stats stats(java.net.URI uri)
Returns a snapshot of statistics accumulated so far for the givenURI.
-
initialize
@Deprecated(since="1.8.0", forRemoval=true) public void 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(since="1.8.0", forRemoval=true) public java.util.concurrent.CompletableFuture<java.lang.Void> 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
public java.util.Iterator<java.net.URI> uris() throws java.io.IOExceptionReturns an iterator over theURIsof responses known to this cache. The returned iterator supports removal.- Throws:
java.io.IOException
-
clear
public void clear() throws java.io.IOExceptionRemoves all entries from this cache.- Throws:
java.lang.IllegalStateException- if closedjava.io.IOException
-
remove
public boolean remove(java.net.URI uri) throws java.io.IOExceptionRemoves the entry associated with the given URI if one is present.- Throws:
java.lang.IllegalStateException- if closedjava.io.IOException
-
remove
public boolean remove(java.net.http.HttpRequest request) throws java.io.IOExceptionRemoves the entry associated with the given request if one is present.- Throws:
java.lang.IllegalStateException- if closedjava.io.IOException
-
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
public void dispose() throws java.io.IOExceptionAtomically clears and closes this cache.- Throws:
java.lang.IllegalStateException- if closedjava.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses 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 interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
newBuilder
public static HttpCache.Builder newBuilder()
Returns a newHttpCache.Builder.
-
-