- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我尝试使用 cachingHttpClient 来缓存 HTTP 响应,但没有成功。这是我引用此链接整理的演示,http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html
public class CacheDemo {
public static void main(String[] args) {
CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(1000);
cacheConfig.setMaxObjectSizeBytes(1024 * 1024);
HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig);
HttpContext localContext = new BasicHttpContext();
sendRequest(cachingClient, localContext);
CacheResponseStatus responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
sendRequest(cachingClient, localContext);
responseStatus = (CacheResponseStatus) localContext.getAttribute(
CachingHttpClient.CACHE_RESPONSE_STATUS);
checkResponse(responseStatus);
}
static void sendRequest(HttpClient cachingClient, HttpContext localContext) {
HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
HttpResponse response = null;
try {
response = cachingClient.execute(httpget, localContext);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
EntityUtils.consume(entity);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void checkResponse(CacheResponseStatus responseStatus) {
switch (responseStatus) {
case CACHE_HIT:
System.out.println("A response was generated from the cache with no requests "
+ "sent upstream");
break;
case CACHE_MODULE_RESPONSE:
System.out.println("The response was generated directly by the caching module");
break;
case CACHE_MISS:
System.out.println("The response came from an upstream server");
break;
case VALIDATED:
System.out.println("The response was generated from the cache after validating "
+ "the entry with the origin server");
break;
}
}
}
这是一个简单的程序,但我不知道哪里出错了。您的帮助将不胜感激。谢谢。
最佳答案
带有 url http://www.mydomain.com/content/ 的 GET 请求将以 Http 404 代码(未找到)结束。此结果很可能无法缓存,所以我猜这就是它对您不起作用的原因。
更新:必须满足某些条件才能提供来自缓存的响应。您应该启用 apache http 客户端的日志记录(例如 http://hc.apache.org/httpclient-3.x/logging.html )。比您可以调试正在发生的事情以及为什么其他 URL 缓存未命中。您可能还应该下载该库的源代码并在那里查看 (http://hc.apache.org/downloads.cgi)。您尤其会对 org.apache.http.impl.client.cache.CachedResponseSuitabilityChecker
类感兴趣。这也应该有助于您使用库进行后续开发。
顺便说一句。 http://muvireviews.com/celebrity/full_view/41/Shahrukh-khan返回此 header :
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, no-cache, no-store
并且由于 CachedResponseSuitabilityChecker
中的 if 语句:
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
log.trace("Response contained NO CACHE directive, cache was not suitable");
return false;
}
不会使用缓存。
祝你好运;)
关于java - 无法在 Java 中使用 cachingHttpClient 缓存 HttpResponse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296433/
我正在使用 Apache cachingHttpClient 从 Java 代码查询 REST API。 尽管收到导致 cachingHttpClient 不缓存文件的“Cache-Control:
我尝试使用 cachingHttpClient 来缓存 HTTP 响应,但没有成功。这是我引用此链接整理的演示,http://hc.apache.org/httpcomponents-client-g
我是一名优秀的程序员,十分优秀!