Apache HttpClient Cache 4.5 Http Caching Example
HttpClient Cache provides an HTTP/1.1-compliant caching layer to be used with HttpClient–the Java equivalent of a browser cache. The following example uses the CacheConfig of the HttpClient Cache library.
Maven dependencies
We use maven to manage our dependencies and are using Apache HttpClient version 4.5. Add the following dependency to your project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.memorynotfound.apache</groupId> <artifactId>caching</artifactId> <version>1.0.0-SNAPSHOT</version> <url>https://memorynotfound.com</url> <name>httpclient - ${project.artifactId}</name> <dependencies> <!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <!-- Apache HttpClient Cache --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient-cache</artifactId> <version>4.5.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Http Caching Example
This is a simple example of how to set up a basic caching HttpClient. As configured it will store a maximum of 3000 cached objects, each of which may have a maximum body size of 10240 bytes. We configure the CacheConfig and use this configuration to create the HttpClient. We loop execute a simple HTTP GET request 3 times and expect that the last two request will be cached.
package com.memorynotfound.httpclient; import org.apache.http.client.cache.CacheResponseStatus; import org.apache.http.client.cache.HttpCacheContext; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.cache.CacheConfig; import org.apache.http.impl.client.cache.CachingHttpClients; import java.io.IOException; /** * This example demonstrates how to use caching {@link CacheConfig}. */ public class HttpClientCachingExample { public static void main(String... args) throws IOException { CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(3000) .setMaxObjectSize(10240) // 10MB .build(); CloseableHttpClient cachingClient = CachingHttpClients.custom() .setCacheConfig(cacheConfig) .build(); for (int i = 0; i < 3; i++){ HttpCacheContext context = HttpCacheContext.create(); HttpGet httpget = new HttpGet("http://httpbin.org/cache"); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = cachingClient.execute(httpget, context); try { System.out.println("----------------------------------------"); 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(); } } } }
Output
The previous application generates the following output.
Executing request GET http://httpbin.org/cache HTTP/1.1 ---------------------------------------- The response came from an upstream server Executing request GET http://httpbin.org/cache HTTP/1.1 ---------------------------------------- The response was generated from the cache after validating the entry with the origin server Executing request GET http://httpbin.org/cache HTTP/1.1 ---------------------------------------- The response was generated from the cache after validating the entry with the origin server
Download
From:一号门
COMMENTS