HttpClient学习研究---第六章:HTTP缓存

来源:互联网 发布:ppt 数据展示 编辑:程序博客网 时间:2024/05/21 16:14

第六章。HTTP CachingHTTP缓存

6.1.6.1。General Concepts一般概念

HttpClient Cache provides an HTTP/1.1-compliant caching layer to be used with HttpClient--the Java equivalent of a browser cache.HttpClient缓存提供了一个HTTP / 1.1兼容的缓存层可以使用HttpClient——Java相当于一个浏览器缓存。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.实现遵循责任链设计模式,实现服务的缓存HttpClient顺便替代了默认非缓存HttpClient实施;请求,可以满足完全从缓存将不会导致实际来源请求。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.过期的缓存条目自动验证的原产地在可能的情况下,使用条件会和如果以后修改和/或没有请求头。

HTTP/1.1 caching in general is designed to beHTTP / 1.1��存通常被设计成semantically transparent语义透明; that is, a cache should not change the meaning of the request-response exchange between client and server.,即,一个缓存应该没有改变的意思-响应客户机和服务器之间交换。As such, it should be safe to drop a caching HttpClient into an existing compliant client-server relationship.因此,它应该是安全滴一个缓存HttpClient到现有的客户机-服务器兼容的关系。Although the caching module is part of the client from an HTTP protocol point of view, the implementation aims to be compatible with the requirements placed on a transparent caching proxy.尽管缓存模块部分的客户从HTTP协议的角度来看,实施旨在适合需求放在一个透明的缓存代理。

Finally, caching HttpClient includes support the Cache-Control extensions specified by RFC 5861 (stale-if-error and stale-while-revalidate).最后,缓存HttpClient包括支持指定的cache - control扩展RFC 5861(过期如果误差和陈旧而重新验证)。

When caching HttpClient executes a request, it goes through the following flow:当缓存HttpClient执行一个请求时,它会通过以下流程:

  1. Check the request for basic compliance with the HTTP 1.1 protocol and attempt to correct the request.检查要求基本符合HTTP 1.1协议并尝试正确的请求。

  2. Flush any cache entries which would be invalidated by this request.平任何缓存条目将无效,因为这个请求。

  3. Determine if the current request would be servable from cache.确定当前请求从缓存将违法者给予。If not, directly pass through the request to the origin server and return the response, after caching it if appropriate.如果没有,直接通过请求源服务器并返回响应,在缓存它如果适当的。

  4. If it was a a cache-servable request, it will attempt to read it from the cache.如果这是一个一个缓存违法者给予请求时,它将尝试从缓存中读取它。If it is not in the cache, call the origin server and cache the response, if appropriate.如果它不是在缓存中,调用源服务器和缓存响应,如果适当的。

  5. If the cached response is suitable to be served as a response, construct a BasicHttpResponse containing a ByteArrayEntity and return it.如果缓存的响应是适合作为响应,构建一个包含一个ByteArrayEntity BasicHttpResponse并返回它。Otherwise, attempt to revalidate the cache entry against the origin server.否则,尝试重新验证缓存条目与源服务器。

  6. In the case of a cached response which cannot be revalidated, call the origin server and cache the response, if appropriate.对于一个缓存的响应不能重新检验它,叫源服务器和缓存响应,如果适当的。

When caching HttpClient receives a response, it goes through the following flow:当缓存HttpClient收到一个响应,它会通过以下流程:

  1. Examining the response for protocol compliance检查响应协议遵从性

  2. Determine whether the response is cacheable确定是否可缓存的响应

  3. If it is cacheable, attempt to read up to the maximum size allowed in the configuration and store it in the cache.如果它是缓存,试图读到最大尺寸允许的配置并将其存储在缓存中。

  4. If the response is too large for the cache, reconstruct the partially consumed response and return it directly without caching it.如果响应是太大的缓存,重建部分响应并返回它直接消耗没有缓存它。

It is important to note that caching HttpClient is not, itself, a different implementation of HttpClient, but that it works by inserting itself as an additonal processing component to the request execution pipeline.重要的是要注意,缓存不是HttpClient,本身,一个不同的实现,但这作品HttpClient通过插入本身作为一个额外的处理组件请求执行管道。

6.2.6.2。RFC-2616 Compliancerfc - 2616合规

We believe HttpClient Cache is我们相信HttpClient缓存unconditionally compliant无条件顺从的withRFC-2616rfc - 2616. That is, wherever the specification indicates MUST, MUST NOT, SHOULD, or SHOULD NOT for HTTP caches, the caching layer attempts to behave in a way that satisfies those requirements.即,无论该规范表明必须,必须,应该或不应该对于HTTP缓存,缓存层试图表现的方式满足这些需求。This means the caching module won't produce incorrect behavior when you drop it in.这意味着缓存模块不会产生不正确的行为,当你把它在。

6.3.6.3。Example Usage示例使用

This is a simple example of how to set up a basic caching HttpClient.这是一个简单的例子,如何建立一个基本的缓存HttpClient。As configured, it will store a maximum of 1000 cached objects, each of which may have a maximum body size of 8192 bytes.根据配置,它将存储的最大值是1000缓存对象,每一种都可能有一个最大的身体大小为8192字节。The numbers selected here are for example only and not intended to be prescriptive or considered as recommendations.数字选择这里有例如只有和不准备规定性或视为建议。

CacheConfig cacheConfig = CacheConfig.custom()        .setMaxCacheEntries(1000)        .setMaxObjectSize(8192)        .build();RequestConfig requestConfig = RequestConfig.custom()        .setConnectTimeout(30000)        .setSocketTimeout(30000)        .build();CloseableHttpClient cachingClient = caching HttpClients.custom()        .setCacheConfig(cacheConfig)        .setDefaultRequestConfig(requestConfig)        .build();HttpCacheContext context = HttpCacheContext.create();HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");CloseableHttpResponse response = cachingClient.execute(httpget, context);try {    CacheResponseStatus responseStatus = context.getCacheResponseStatus();    switch (responseStatus) {        case CACHE_HIT:            System.out.println("A response was generated from the cache with " +                    "no requests sent upstream");            break;        case CACHE_MODULE_RESPONSE:            System.out.println("The response was generated directly by the " +                    "caching module");            break;        case CACHE_MISS:            System.out.println("The response came from an upstream server");            break;        case VALIDATED:            System.out.println("The response was generated from the cache " +                    "after validating the entry with the origin server");            break;    }} finally {    response.close();}    

6.4.6.4。Configuration配置

The caching HttpClient inherits all configuration options and parameters of the default non-caching implementation (this includes setting options like timeouts and connection pool sizes).缓存HttpClient继承所有的配置选项和参数的默认非缓存实现(这包括设置选项如超时和连接池大小)。For caching-specific configuration, you can provide a对缓存的具体配置,您可以提供一个 CacheConfiginstance to customize behavior across the following areas:实例来定制行为在以下领域:

Cache size.缓存大小。If the backend storage supports these limits, you can specify the maximum number of cache entries as well as the maximum cacheable response body size.如果后端存储支持这些限制,你可以指定缓存条目的最大数量以及最大缓存响应体大小。

Public/private caching.公共/私有缓存。By default, the caching module considers itself to be a shared (public) cache, and will not, for example, cache responses to requests with Authorization headers or responses marked with "Cache-Control: private".默认情况下,缓存模块认为自己是一个共享的(公共)缓存,不会,例如,缓存响应或响应请求的授权标题注明“cache - control:私人”。If, however, the cache is only going to be used by one logical "user" (behaving similarly to a browser cache), then you will want to turn off the shared cache setting.然而,如果缓存中只会使用一个逻辑上的“用户”(同样的行为到浏览器缓存),然后你会想关掉共享缓存设置。

Heuristic caching.启发式缓存。Per RFC2616, a cache MAY cache certain cache entries even if no explicit cache control headers are set by the origin.每RFC2616,缓存可能缓存特定的缓存条目即使没有显式的缓存控制标题设置的起源。This behavior is off by default, but you may want to turn this on if you are working with an origin that doesn't set proper headers but where you still want to cache the responses.这种行为默认是关闭的,但是你可能想要把这个如果你正与一个起源,没有设置适当的标题,但你仍然想要缓存的响应。You will want to enable heuristic caching, then specify either a default freshness lifetime and/or a fraction of the time since the resource was last modified.你会想让启发式缓存,然后指定一个默认的新鲜要么一生和/或一个短的时间自资源最后修改。See Sections 13.2.2 and 13.2.4 of the HTTP/1.1 RFC for more details on heuristic caching.见章节13 2 2和13 2 4 HTTP / 1.1 RFC详情启发式缓存。

Background validation.背景验证。The cache module supports the stale-while-revalidate directive of RFC5861, which allows certain cache entry revalidations to happen in the background. 缓存模块支持的RFC5861陈旧而重新验证指令,允许特定的缓存条目revalidations发生的背景。You may want to tweak the settings for the minimum and maximum number of background worker threads, as well as the maximum time they can be idle before being reclaimed. 你可能想要调整设置最小和最大数量的背景工作线程,以及最大的时间他们可以被回收之前闲置。You can also control the size of the queue used for revalidations when there aren't enough workers to keep up with demand.您还可以控制队列的大小用于revalidations当没有足够的工人,才能跟上需求。

6.5.6.5。Storage Backends存储后端

The default implementation of caching HttpClient stores cache entries and cached response bodies in memory in the JVM of your application.默认实现缓存的缓存条目和HttpClient存储在内存中缓存的响应的身体在JVM您的应用程序。While this offers high performance, it may not be appropriate for your application due to the limitation on size or because the cache entries are ephemeral and don't survive an application restart. 虽然这提供高性能,它可能不适合您的应用程序由于限制大小或因为缓存条目都是短暂的,不要在应用重启。The current release includes support for storing cache entries using EhCache and memcached implementations, which allow for spilling cache entries to disk or storing them in an external process.当前版本支持存储缓存条目使用EhCache和memcached的实现,它允许溢出到磁盘缓存条目或将它们存储在外部过程。

If none of those options are suitable for your application, it is possible to provide your own storage backend by implementing the HttpCacheStorage interface and then supplying that to caching HttpClient at construction time.如果没有这些选项适合您的应用程序,可以提供自己的存储后端通过实现接口,然后HttpCacheStorage供应,在构造时,缓存HttpClient。In this case, the cache entries will be stored using your scheme but you will get to reuse all of the logic surrounding HTTP/1.1 compliance and cache handling. 在这种情况下,缓存条目将存储使用您的方案,但你会得到重用所有的逻辑周围的HTTP / 1.1合规和缓存处理。Generally speaking, it should be possible to create an HttpCacheStorage implementation out of anything that supports a key/value store (similar to the Java Map interface) with the ability to apply atomic updates.一般来说,它应该有可能创建一个HttpCacheStorage实现从任何支持一键/值存储(类似于Java Map接口)与原子更新应用能力。

Finally, with some extra efforts it's entirely possible to set up a multi-tier caching hierarchy; for example, wrapping an in-memory caching HttpClient around one that stores cache entries on disk or remotely in memcached, following a pattern similar to virtual memory, L1/L2 processor caches, etc.最后,一些额外的努力完全有可能建立一个多层缓存层次;例如,包装一个内存中的缓存HttpClient围绕着一个高速缓存条目存储在磁盘或远程在memcached,遵循一个模式类似于虚拟内存,L1 / L2处理器缓存等。

原创粉丝点击