Package org.microproxy.cache
Class HttpCache
java.lang.Object
org.microproxy.cache.HttpCache
- All Implemented Interfaces:
HttpFiltersSource
A shared HTTP cache (RFC 9111) that can also keep a site usable offline.
HttpCache cache = HttpCache.builder()
.store(new DiskCacheStore(Path.of("cache"), 1L << 30))
.build();
MicroProxy.bootstrap().withHttpCache(cache).start();
What it does:
- Storing: complete responses to
GET, up toHttpCache.Builder.maxEntrySize, that are storable for a shared cache: notno-storeorprivate, not to requests withAuthorization(unlesspublic,s-maxageormust-revalidate), notVary: *, and not setting cookies unlesspublic. - Freshness: from
s-maxage,max-age,Expires, or 10% of the time sinceLast-Modified(at most a day); ages follow the RFC's calculation, includingAgeand response delay. Request directivesmax-age,min-fresh,max-stale,no-cache,no-storeandonly-if-cachedare honoured. - Validation: stale entries with an
ETagorLast-Modifiedare revalidated with a conditional request; a304refreshes the entry and the client gets the stored response. - Variants:
Varyis matched on normalized request field values; each URL may have several stored variants.HEADis answered from a storedGET. - Invalidation: a successful unsafe request (
POST,PUT, ...) removes the stored responses for its URL and for same-originLocation/Content-Location. - Unreachable servers: when the server cannot be reached (the proxy would answer
502or504) a stale entry is served instead, unless it saysmust-revalidate,proxy-revalidate,no-cacheors-maxage. A server error (5xx) is replaced by a stale entry only within itsstale-if-errorwindow. Intercepted HTTPS keeps working: aCONNECTto an unreachable server is still intercepted so cached pages can be served inside it. - Offline mode (
HttpCache.Builder.offline): requests are answered only from the cache, whatever their age and directives; anything not stored gets504.
Responses carry a Cache-Status field (RFC 9211), e.g. MicroProxy; hit or
MicroProxy; fwd=miss; stored. Not implemented: stale-while-revalidate, partial
content (Range requests bypass the cache), and caching of POST responses.
Install it with HttpProxyServerBootstrap.withHttpCache(org.microproxy.cache.HttpCache), which runs it
after all other filters: they see requests before the cache answers them, and the cache stores
responses as they leave the other filters.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classA response made by the cache: a stored response, or its own504.static final classOptions forHttpCache.static final recordCounters since the cache was created. -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpCache.Builderbuilder()Starts a builder with the default settings.filterRequest(HttpRequest originalRequest, FlowContext flowContext) Returns the filters for a request, ornullto proxy it unfiltered.booleanReports whether the cache operates without contacting servers.stats()Returns a snapshot of the cache counters.store()Returns the backing response store.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.microproxy.HttpFiltersSource
getMaximumRequestBufferSizeInBytes, getMaximumResponseBufferSizeInBytes
-
Method Details
-
builder
Starts a builder with the default settings.- Returns:
- a new builder with default settings
-
stats
Returns a snapshot of the cache counters.- Returns:
- a snapshot of the cache counters
-
store
Returns the backing response store.- Returns:
- the backing cache store
-
isOffline
public boolean isOffline()Reports whether the cache operates without contacting servers.- Returns:
- whether upstream requests are disabled
-
filterRequest
Description copied from interface:HttpFiltersSourceReturns the filters for a request, ornullto proxy it unfiltered.- Specified by:
filterRequestin interfaceHttpFiltersSource- Parameters:
originalRequest- a copy of the request as received from the clientflowContext- the client connection the request arrived on- Returns:
- the per-request filters, or
nullto leave the request unfiltered
-