Introduction to Http Caching Client

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

  1. Upgrade Http Client version to 4.3.3 as there are some issues in current version

  2. Implement org.apache.http.client.cache.HttpCacheStorage and user couch base API

  3. 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

http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

转载于:https://my.oschina.net/stubhub/blog/325042

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值