如何像浏览器一样实现客户端http缓存? [英] How do I implement client side http caching like a browser?

查看:182
本文介绍了如何像浏览器一样实现客户端http缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RESTFul服务作为我的前端的后端。该服务在其响应上设置了expires / etag / lastmodified标头。

I use a RESTFul service as a backend to my frontend. The service sets expires/etag/lastmodified headers on it's responses.

我正在寻找的是一个客户端(有利的java)库,可以从中获取数据服务并将其缓存在可插入的缓存后端(如ehcache)中。

What I'm looking for is a client-side(favorably java) library which can fetch data from the service and cache it in a pluggable caching backend like ehcache.

我还希望能够做的是在输入后立即使用后台工作线程自动填充缓存失效。此外,做条件GET应该很聪明。

What I also want to be able to do is automatically prime the cache using background worker threads as soon as an entry is invalidated. Also, it should be smart to do conditional GETs.

我遇到过
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html

有没有人知道其他图书馆?这不是一个相当常见的问题吗?

Is there any other library anyone knows about? Isn't this a fairly common problem?

推荐答案

Apache HttpComponents库的4.0+版本附带HTTP 1.1缓存支持。您可以使用Spring RestTemplate restful client进行如下操作:

The 4.0+ version of the Apache HttpComponents library comes with HTTP 1.1 cache support. You can use this with the Spring RestTemplate restful client as follows:

    CacheConfig cacheConfig = new CacheConfig();  
    cacheConfig.setMaxCacheEntries(1000);
    cacheConfig.setMaxObjectSize(8192);

    HttpClient cachingClient = new CachingHttpClient(new DefaultHttpClient(), cacheConfig);

    ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(cachingClient);
    RestTemplate rest = new RestTemplate(requestFactory);

这篇关于如何像浏览器一样实现客户端http缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆