मैं कैश HTTP cachingHttpClient का उपयोग कर प्रतिक्रियाओं के लिए कोशिश कर रहा हूँ, लेकिन व्यर्थ। इस डेमो जो मैं इस लिंक की बात कर रहा द्वारा एक साथ रखा, http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.htmlनहीं जावा में cachingHttpClient का उपयोग कर HttpResponse कैश करने के लिए सक्षम?
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;
}
}
}
इसका एक सरल कार्यक्रम है, लेकिन मैं यह पता लगाने की जहां मैं गलत जा रहा हूँ में असमर्थ हूँ। आपकी सहायता का आभार होगा। धन्यवाद।
आपको क्या होने की उम्मीद करते हैं और क्या वास्तव में क्या होता है? – artbristol
@artbristol मैं एक कैश हिट चाहता हूं जब एक ही http अनुरोध दूसरी बार भेजा जाता है। यदि आप इस उदाहरण को चलाते हैं, तो दोनों बार कैश मिस होती है। – FireAndIce