Why need Caching?
In some cases, we can accept some latency on backend services.
Which component to use?
As we are already using Http Client, it makes sense to leverage its caching component. Here are the description from apache site:
HttpClient Cache provides an HTTP/1.1-compliant caching layer to be used with HttpClient--the Java equivalent of a browser cache. The implementation follows the Chain of Responsibility design pattern, where the caching HttpClient implementation can serve a drop-in replacement for the default non-caching HttpClient implementation; requests that can be satisfied entirely from the cache will not result in actual origin requests. Stale cache entries are automatically validated with the origin where possible, using conditional GETs and the If-Modified-Since and/or If-None-Match request headers.
How it works
You need to build a Http Client using CachingHttpClients (available after 4.3)
CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000).setMaxObjectSize(64 * 1024).build(); String memcachedHosts = "127.0.0.1:11211 127.0.0.1:11211"; ResourceFactory resourceFactory = new FileResourceFactory(new File("/tmp/httpCache")); HttpCacheStorage storage = new MemcachedHttpCacheStorage(new MemcachedClient( AddrUtil.getAddresses(memcachedHosts)), cacheConfig, new MemcachedCacheEntryFactoryImpl(), new SHA256KeyHashingScheme()); HttpClient hc = CachingHttpClients.custom().setCacheConfig(cacheConfig).setResourceFactory(resourceFactory) .setHttpCacheStorage(storage).setMaxConnPerRoute(100).setMaxConnTotal(1000).build(); |
Like I mentioned in code, we are using a FileResourceFactory to store the resource body and use mem cache to store other cache related information (headers, status code and so on.)
After built the Http Client instance, just use it as normal one, the caching part will work automatically (no need to pass any headers!)
As currently we are using Http Client 4.2.2, we need to use following API instead:
CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setMaxCacheEntries(1000); cacheConfig.setMaxObjectSize(64 * 1024); String memcachedHosts = "127.0.0.1:11211 127.0.0.1:11211"; ResourceFactory resourceFactory = new FileResourceFactory(new File("/tmp/httpCache")); HttpCacheStorage storage = new MemcachedHttpCacheStorage(new MemcachedClient( AddrUtil.getAddresses(memcachedHosts)), cacheConfig, new MemcachedCacheEntryFactoryImpl(), new SHA256KeyHashingScheme()); HttpClient hc = new CachingHttpClient(HttpClientUtil.createClient(null), resourceFactory, storage, cacheConfig); |
To Dos
Upgrade Http Client version to 4.3.3 as there are some issues in current version
Implement org.apache.http.client.cache.HttpCacheStorage and user couch base API
Implement org.apache.http.client.cache.Resource, find a suitable way to do the resource clean up.
Useful Resources
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html