Failed to cache HttpResponse using HttpClient caching in java?

I am trying to cache HTTP responses using HTTPClient caching, but in vain. This is the demo that I have collected by referencing this link, 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;
        }
    }

  }

This is a simple program, but I cannot figure out where I am going wrong. Your help will be greatly appreciated. Thank.

+5
source share
2 answers

A GET request from the URL http://www.mydomain.com/content/ will contain an Http 404 code (not found). This result cannot be, most likely, cached, so it does not work for you, I think.

UPDATE: . 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. .

Btw. 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;
            }

.

;)

+4

CachingHttpClient , , "Cache-Control: private" ( , ). . @https://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html ( № 6.4 )

, i.e.

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

. !!!

0

All Articles