bboss mvc控制器实现etag和last modify两种http缓存机制

bboss mvc控制器实现etag和last modify两种http缓存机制(本文参考《[url=http://jinnianshilongnian.iteye.com/blog/2319573]聊聊高并发系统之HTTP缓存[/url]》编写)

[size=large][b]1.缓存控制器实现[/b][/size]
缓存控制器的类文件:
[url=https://github.com/bbossgroups/bestpractice/blob/master/mvc/src/org/frameworkset/mvc/HttpCache.java]HttpCache.java[/url]
实现etag和last modify两种http缓存机制方法分别如下:

[b]last modify[/b]
public ResponseEntity<String> cache( 
//为了方便测试,此处传入文档最后修改时间
@RequestParam(name="millis") long lastModifiedMillis,
//浏览器验证文档内容是否修改时传入的Last-Modified If-Modified-Since
@RequestHeader (name = "If-Modified-Since", required = false,dateformat="EEE, d MMM yyyy HH:mm:ss.SSS 'GMT'",locale = "en_US") Date ifModifiedSince) {

//当前系统时间
long now = System.currentTimeMillis();
//文档可以在浏览器端/proxy上缓存多久
long maxAge = 20;

//判断内容是否修改了,此处使用等值判断
if(ifModifiedSince != null && ifModifiedSince.getTime() == lastModifiedMillis) {
return new ResponseEntity<String>(HttpStatus.NOT_MODIFIED);
}

DateFormat gmtDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss.SSS 'GMT'", Locale.US);
System.out.println(System.currentTimeMillis());
String body = "<a href=''>点击访问当前链接</a>";
MultiValueMap<String, String> headers = new HttpHeaders();

//文档修改时间
headers.add("Last-Modified", gmtDateFormat.format(new Date(lastModifiedMillis)));
//当前系统时间
headers.add("Date", gmtDateFormat.format(new Date(now)));
//过期时间 http 1.0支持
headers.add("Expires", gmtDateFormat.format(new Date(now + maxAge)));
//文档生存时间 http 1.1支持
headers.add("Cache-Control", "max-age=" + maxAge);
return new ResponseEntity<String>(body, headers, HttpStatus.OK,"String");
}

[b]etag[/b]
public ResponseEntity<String> etgcache( 
//浏览器验证文档内容的实体 If-None-Match
@RequestHeader (name = "If-None-Match", required = false) String ifNoneMatch) {

//当前系统时间
long now = System.currentTimeMillis();
//文档可以在浏览器端/proxy上缓存多久
long maxAge = 10;

String body = "<a href=''>点击访问当前链接</a>";

//弱实体
String etag = "W/\"" + DigestUtils.md5DigestAsHex(body.getBytes()) + "\"";

if(ifNoneMatch != null && ifNoneMatch.equals(etag)) {
return new ResponseEntity<String>(HttpStatus.NOT_MODIFIED);
}

DateFormat gmtDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
MultiValueMap<String, String> headers = new HttpHeaders();

//ETag http 1.1支持
headers.add("ETag", etag);
//当前系统时间
headers.add("Date", gmtDateFormat.format(new Date(now)));
//文档生存时间 http 1.1支持
headers.add("Cache-Control", "max-age=" + maxAge);
return new ResponseEntity<String>(body, headers, HttpStatus.OK);
}


[size=large][b]运行和访问实例[/b][/size]
启动示例应用之前,必须先安装好gradle并配置好gradle环境变量,至少[url=https://gradle.org/gradle-download/]gradle 3.0+ (Complete distribution)[/url]版本
从github下载源码工程:
git clone -b master --depth 1 https://github.com/bbossgroups/bestpractice.git
切换到在命令行,进入源码目录
cd bestpractice

执行gradle指令
gradle :mvc:tomcatStart

启动成功后访问示例:
etag
[url]http://localhost:8080/mvc/caches/etgcache.page[/url]
Last-Modified
[url]http://localhost:8080/mvc/caches/cache.page?millis=1473172518546[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值