Not able to cache HttpResponse using cachingHttpClient in java?
caching, httpresponse, java
Solution
The GET request with url http://www.mydomain.com/content/ will end up with Http 404 code (Not found). This result can not be most likely cached, so that is why it does not work for you I guess.
UPDATE: There must be certain conditions met in order to serve the response from cache. You should enable logging of apache http client (e.g. http://hc.apache.org/httpclient-3.x/logging.html). Than you can debug what is going on and why there are cache misses for your other URLs. You should probably download also source code of the library and have a look there (http://hc.apache.org/downloads.cgi). Especially you will be interested in `org.apache.http.impl.client.cache.CachedResponseSuitabilityChecker` class. This should help you also in your following development with the library.
Btw. http://muvireviews.com/celebrity/full_view/41/Shahrukh-khan return this header:
`Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, no-cache, no-store`
and because of the if statement in `CachedResponseSuitabilityChecker`:
if (HeaderConstants.CACHE_CONTROL_NO_CACHE.equals(elt.getName())) {
log.trace("Response contained NO CACHE directive, cache was not suitable");
return false;
}
the cache will not be used.
Good luck ;)
Problem
I'm trying to cache HTTP responses using cachingHttpClient, but in vain. This is the demo which I put together by refering to 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; } } } ``` Its a simple program, but I'm unable to figure out where am I going wrong. Your help would be appreciated. Thanks.