下面列出了怎么用org.apache.http.client.cache.HttpCacheContext的API类实例代码及写法,或者点击链接到github查看源代码。
private void logCacheResponseStatus(HttpCacheContext httpContext) {
this.oidcRequests++;
switch (httpContext.getCacheResponseStatus()) {
case CACHE_HIT:
this.oidcCacheHits++;
break;
case CACHE_MODULE_RESPONSE:
this.oidcCacheModuleResponses++;
break;
case CACHE_MISS:
this.oidcCacheMisses++;
break;
case VALIDATED:
this.oidcCacheHitsValidated++;
break;
}
long now = System.currentTimeMillis();
if (this.oidcRequests >= 2 && now - lastCacheStatusLog > CACHE_STATUS_LOG_INTERVAL_MS) {
log.info("Cache status for KeySetRetriever:\noidcCacheHits: " + oidcCacheHits + "\noidcCacheHitsValidated: "
+ oidcCacheHitsValidated + "\noidcCacheModuleResponses: " + oidcCacheModuleResponses
+ "\noidcCacheMisses: " + oidcCacheMisses);
lastCacheStatusLog = now;
}
}
public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) {
return new ClientExecChain() {
/**
* Removes client http cache directives like "Cache-control" and "Pragma". Users must not be able to bypass
* the cache just by making a refresh in the browser. Generates X-cache header.
*
*/
@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
HttpClientContext httpClientContext, HttpExecutionAware execAware) throws IOException,
HttpException {
OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext);
// Switch route for the cache to generate the right cache key
CloseableHttpResponse response = wrapped.execute(route, request, context, execAware);
// Remove previously added Cache-control header
if (request.getRequestLine().getMethod().equalsIgnoreCase("GET")
&& (staleWhileRevalidate > 0 || staleIfError > 0)) {
response.removeHeader(response.getLastHeader("Cache-control"));
}
// Add X-cache header
if (xCacheHeader) {
if (context != null) {
CacheResponseStatus cacheResponseStatus =
(CacheResponseStatus) context.getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);
String xCacheString;
if (cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT)) {
xCacheString = "HIT";
} else if (cacheResponseStatus.equals(CacheResponseStatus.VALIDATED)) {
xCacheString = "VALIDATED";
} else {
xCacheString = "MISS";
}
xCacheString += " from " + route.getTargetHost().toHostString();
xCacheString +=
" (" + request.getRequestLine().getMethod() + " " + request.getRequestLine().getUri()
+ ")";
response.addHeader("X-Cache", xCacheString);
}
}
// Remove Via header
if (!viaHeader && response.containsHeader("Via")) {
response.removeHeaders("Via");
}
return response;
}
};
}
@Override boolean isRemote(HttpContext context, Span span) {
boolean cacheHit = CacheResponseStatus.CACHE_HIT.equals(
context.getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS));
if (cacheHit) span.tag("http.cache_hit", "");
return !cacheHit;
}