文档章节

Introduction to Http Caching Client

stubhub
 stubhub
发布于 2014/10/08 19:21
字数 360
阅读 18
收藏 0

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

© 著作权归作者所有

stubhub
粉丝 1
博文 10
码字总数 2015
作品 0
浦东
私信 提问
HTTP头 Pragma:no-cache 缓存来源

[转]今天给同事分享了一下前端性能优化,在介绍了php文件缓存的方法后,发现一个AJAX请求的文件,在请求头中始终有一个:Pragma:no-cache,导致这个文件不能被浏览器缓存。接着发现这个站几乎...

jarly
2013/03/09
10.1K
0
http://spring.oschina.mopaas.com/html_en/

Spring Framework Reference Documentation Next Spring Framework Reference Documentation Authors Rod Johnson , Juergen Hoeller , Keith Donald , Colin Sampaleanu , Rob Harrop , Tho......

梅_95
2016/08/15
9
0
Nginx 1.1.8 开发版发布

Nginx 刚刚发布了 1.1.8 开发版,下载地址: nginx-1.1.8 , nginx/Windows-1.1.8 新版本改进记录: *) Change: the ngx_http_limit_zone_module was renamed to the ngx_http_limit_conn_mod......

红薯
2011/11/15
1K
4
Android 6.0 使用 Apache HttpClient

Android 6.0版本已经已经基本将Apahce Http Client 移除出SDK。 那么问题来了,如果我在以前的项目中使用了Apache HttpClient相关类,怎么办呢? 请看官网给出的答案 Apache HTTP Client Rem...

____追梦人
2016/02/01
107
0
WebSocket Server in C#

Download WebSockets - 47 KB (Relates to this article) Latest async version on Github (Targets .NetStandard 2.0) Set as the startup project. Introduction NEW - Complete refactor ......

Dave Haig
2017/12/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Vue nodejs商城项目-搭建express框架环境

本文转载于:专业的前端网站➯Vue nodejs商城项目-搭建express框架环境 1.express-project 搭建express框架环境 安装express generator生成器 通过生成器自动创建项目 配置分析 安装 cnpm i -...

前端老手
8分钟前
1
0
maven项目A引入maven项目B的jar包

首先打开 项目B 的 pom 文件,加入如下配置 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin<......

嘴角轻扬30
11分钟前
2
0
一个突然出现的诡异bug

突然从昨天的某个时刻开始,程序报错了,作为接管该项目的我只能从找日志开始:这是打印的日志 日志已经很清楚了,说的的验签或者解密失败了! 好吧,我把代码拉下来瞅瞅,这是打印日志的地方...

布袋和尚_爱吃鱼
17分钟前
2
0
按位与(&)运算符应用实践

按位与运算是将数字转换成二进制格式进行位运算。 1、&的运算方法(奇偶性判断) 整数与“1”进行按位与运算,运算结果为“0”表示为偶数,运算结果为“1”表示为奇数。 <?php//将数组中...

尤金
27分钟前
2
0
java常量池

https://blog.csdn.net/qq_41376740/article/details/80338158

南桥北木
31分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部