How do I implement client side http caching like a browser?

天大地大妈咪最大 提交于 2019-12-06 04:32:24

问题


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

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.

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.

I've come across 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?


回答1:


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);



回答2:


The situation with client side HTTP caches in Java is not particularly good. It is a non-trivial problem that has not been attacked by most of the HTTP client library developers.

I think that is changing slowly, but I cannot provide a definite pointer. A good way to start is to look at the various implementations of JAX-RS that come with a client side API such as Jersey (this has no client side cache). It might be that Restlet has one or Restfulie, please check.

Here is something I found via Google:

http://xircles.codehaus.org/projects/httpcache4j

You can also try to roll your own but you have to be careful to understand the caching headers (including Vary:) to get it right.




回答3:


RestEasy features a client side caching mechanism which is trivial to get up and running if you are using such client.

RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
YourService proxy = ProxyFactory.create(YourService.class, url);

LightweightBrowserCache cache = CacheFactory.makeCacheable(proxy);

You first create a client proxy instance, then wrap it around the cache. That's it.



来源:https://stackoverflow.com/questions/7618619/how-do-i-implement-client-side-http-caching-like-a-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!