6

我正在尝试使用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;
        }
    }

  }

它是一个简单的程序,但我无法弄清楚我哪里出错了。您的帮助将不胜感激。谢谢。

4

2 回答 2

4

带有 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返回此标题:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, no-cache, no-store

并且由于 if 语句中CachedResponseSuitabilityChecker

            if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
                log.trace("Response contained NO CACHE directive, cache was not suitable");
                return false;
            }

不会使用缓存。

祝你好运 ;)

于 2012-04-24T20:22:46.847 回答
0

默认情况下,CachingHttpClient 假定共享缓存,因此如果响应标头包含“Cache-Control:private”(在您的情况下我认为是这种情况),它将忽略存储。见@https ://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html(第6.4节配置)

尝试将其关闭,以便能够在仅客户端模式下使用,即

CacheConfig cacheConfig = new CacheConfig();
cacheConfig.setMaxCacheEntries(1000);
cacheConfig.setMaxObjectSizeBytes(1024 * 1024);
cacheConfig .setSharedCache(false); // Turn it OFF here

这对我有用。祝你好运 !!!

于 2017-03-03T11:31:02.647 回答