下面列出了怎么用org.apache.http.impl.client.cache.CachingHttpClients的API类实例代码及写法,或者点击链接到github查看源代码。
private CloseableHttpClient createHttpClient(HttpCacheStorage httpCacheStorage) {
HttpClientBuilder builder;
if (httpCacheStorage != null) {
builder = CachingHttpClients.custom().setCacheConfig(cacheConfig).setHttpCacheStorage(httpCacheStorage);
} else {
builder = HttpClients.custom();
}
builder.useSystemProperties();
if (sslConfig != null) {
builder.setSSLSocketFactory(sslConfig.toSSLConnectionSocketFactory());
}
return builder.build();
}
MCRRESTResolver() {
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxObjectSize(MAX_OBJECT_SIZE)
.setMaxCacheEntries(MAX_CACHE_ENTRIES)
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(REQUEST_TIMEOUT)
.setSocketTimeout(REQUEST_TIMEOUT)
.build();
this.restClient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setDefaultRequestConfig(requestConfig)
.setUserAgent(MCRHttpUtils.getHttpUserAgent())
.useSystemProperties()
.build();
MCRShutdownHandler.getInstance().addCloseable(this::close);
this.logger = LogManager.getLogger();
}
@PostConstruct
protected void postConstruct() {
try {
// TODO make these settings configurable!!
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(2000)
.setMaxObjectSize(16384)
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30000)
.setSocketTimeout(30000)
.build();
this.httpClient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setDefaultRequestConfig(requestConfig)
.build();
} catch (Exception e) {
logger.error("Error creating HTTP client.", e);
throw new RuntimeException(e);
}
}
private static HttpClientBuilder configureCaching(final Caching caching,
@Nullable final Object cacheStorage) {
final Heuristic heuristic = caching.getHeuristic();
final CacheConfig.Builder config = CacheConfig.custom()
.setSharedCache(caching.getShared())
.setMaxObjectSize(caching.getMaxObjectSize())
.setMaxCacheEntries(caching.getMaxCacheEntries());
if (heuristic.getEnabled()) {
config.setHeuristicCachingEnabled(true);
config.setHeuristicCoefficient(heuristic.getCoefficient());
config.setHeuristicDefaultLifetime(heuristic.getDefaultLifeTime().to(TimeUnit.SECONDS));
}
@Hack("return cast tricks classloader in case of missing httpclient-cache")
CachingHttpClientBuilder builder = CachingHttpClients.custom()
.setCacheConfig(config.build())
.setHttpCacheStorage((HttpCacheStorage) cacheStorage)
.setCacheDir(Optional.ofNullable(caching.getDirectory())
.map(Path::toFile)
.orElse(null));
return HttpClientBuilder.class.cast(builder);
}
public HttpEndpoint(URI endpoint, Config cfg, HttpClientContextFactory clientContextFactory) {
if (endpoint == null) {
throw new IllegalArgumentException("Endpoint is required");
}
if (cfg == null) {
cfg = new ConfigurationBuilder().build();
}
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(cfg.getMaxCacheEntries())
.setMaxObjectSize(cfg.getMaxCacheObjectSize())
.build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(1000 * cfg.getConnectTimeOutSeconds())
.setSocketTimeout(1000 * cfg.getSocketTimeOutSeconds())
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(cfg.getMaxConections());
this.endpoint = endpoint;
this.httpClient = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(new StandardHttpRequestRetryHandler())
.setConnectionManager(cm)
.build();
this.clientContextFactory = clientContextFactory;
initPingThread(cfg.getPingSeconds());
}
@SuppressWarnings("restriction")
private static CloseableHttpClient getHttpClient(URI url){
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(120*1024).setHeuristicCachingEnabled(true)
.setHeuristicDefaultLifetime(TimeUnit.HOURS.toSeconds(12))
.build();
CachingHttpClientBuilder builder = CachingHttpClients.custom()
.setCacheConfig(cacheConfig)
.setHttpCacheStorage(new BundleHttpCacheStorage(HybridCore.getContext().getBundle()));
builder = setupProxy(builder, url);
return builder.build();
}