public BasicHttpCache(ResourceFactory resourceFactory, HttpCacheStorage storage, CacheConfig config) { this.resourceFactory = resourceFactory; this.uriExtractor = new CacheKeyGenerator(); this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory); this.maxObjectSizeBytes = config.getMaxObjectSize(); this.responseGenerator = new CachedHttpResponseGenerator(); this.storage = storage; this.cacheInvalidator = new CacheInvalidator(this.uriExtractor, this.storage); }
public static CacheConfig createDefaultCacheConfig() { return CacheConfig.custom().setMaxCacheEntries(500).setMaxObjectSize(1024 * 256) .setSharedCache(false).setHeuristicCachingEnabled(true) .setHeuristicDefaultLifetime(86400).build(); }
/** * Constructs a {@code CachingHttpClient} with default caching settings that * stores cache entries in memory and uses the given {@link HttpClient} * for backend requests. * @param client used to make origin requests */ public CachingHttpClient(HttpClient client) { this(client, new BasicHttpCache(), new CacheConfig()); }
/** * Constructs a default HttpJsonHomeClient build on top of a CachingHttpClient with in-memory storage. */ public HttpJsonHomeClient() { final CacheConfig cacheConfig = CacheConfig.custom() .setMaxCacheEntries(100) .setMaxObjectSize(50000) .build(); this.cacheStorage = new BasicHttpCacheStorage(cacheConfig); this.httpClient = CachingHttpClientBuilder.create() .setHttpCacheStorage(cacheStorage) .setCacheConfig(cacheConfig) .build(); }
public static org.apache.http.client.HttpClient withCaching(org.apache.http.client.HttpClient client, int maxCacheEntries, int maxObjectSizeBytes) { CacheConfig cacheConfig = new CacheConfig(); cacheConfig.setMaxCacheEntries(maxCacheEntries); cacheConfig.setMaxObjectSize(maxObjectSizeBytes); return new CachingHttpClient(client, cacheConfig); } }
/** * Constructs a HttpJsonHomeClient using a HttpClient and a CacheConfig. * * Internally, these two are used to build a CachingHttpClient using a BasicHttpCacheStorage. * * @param httpClient non-caching HttpClient used to get resources. * @param cacheConfig configuration of the HttpCacheStorage */ public HttpJsonHomeClient(final HttpClient httpClient, final CacheConfig cacheConfig) { this.cacheStorage = new BasicHttpCacheStorage(cacheConfig); this.httpClient = new CachingHttpClient(httpClient, cacheStorage, cacheConfig); }
/** * Constructs a storage backend using the provided Ehcache * with default configuration options. * @param cache where to store cached origin responses */ public EhcacheHttpCacheStorage(Ehcache cache) { this(cache, new CacheConfig(), new DefaultHttpCacheEntrySerializer()); }
private boolean isFreshEnough(HttpCacheEntry entry, HttpRequest request, Date now) { if (validityStrategy.isResponseFresh(entry, now)) return true; if (useHeuristicCaching && validityStrategy.isResponseHeuristicallyFresh(entry, now, heuristicCoefficient, heuristicDefaultLifetime)) return true; if (originInsistsOnFreshness(entry)) return false; long maxstale = getMaxStale(request); if (maxstale == -1) return false; return (maxstale > validityStrategy.getStalenessSecs(entry, now)); }
private boolean staleResponseNotAllowed(final HttpRequestWrapper request, final HttpCacheEntry entry, final Date now) { return validityPolicy.mustRevalidate(entry) || (isSharedCache() && validityPolicy.proxyRevalidate(entry)) || explicitFreshnessRequest(request, entry, now); }
private boolean staleResponseNotAllowed( final HttpRequestWrapper request, final HttpCacheEntry entry, final Date now) { return validityPolicy.mustRevalidate(entry) || (cacheConfig.isSharedCache() && validityPolicy.proxyRevalidate(entry)) || explicitFreshnessRequest(request, entry, now); }
private HttpResponse getFatallyNoncompliantResponse(HttpRequest request, HttpContext context) { HttpResponse fatalErrorResponse = null; List<RequestProtocolError> fatalError = requestCompliance.requestIsFatallyNonCompliant(request); for (RequestProtocolError error : fatalError) { setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE); fatalErrorResponse = requestCompliance.getErrorForRequest(error); } return fatalErrorResponse; }
private AsynchronousValidator createAsynchronousRevalidator(final CacheConfig config) { if (config.getAsynchronousWorkersMax() > 0) { final SchedulingStrategy configuredSchedulingStrategy = createSchedulingStrategy(config); final AsynchronousValidator revalidator = new AsynchronousValidator( configuredSchedulingStrategy); addCloseable(revalidator); return revalidator; } return null; }
private HttpResponse getFatallyNoncompliantResponse( final HttpRequestWrapper request, final HttpContext context) { HttpResponse fatalErrorResponse = null; final List<RequestProtocolError> fatalError = requestCompliance.requestIsFatallyNonCompliant(request); for (final RequestProtocolError error : fatalError) { setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE); fatalErrorResponse = requestCompliance.getErrorForRequest(error); } return fatalErrorResponse; }
private AsynchronousValidator makeAsynchronousValidator( CacheConfig config) { if (config.getAsynchronousWorkersMax() > 0) { return new AsynchronousValidator(this, config); } return null; }
private HttpResponse retryRequestUnconditionally(HttpHost target, HttpRequest request, HttpContext context, HttpCacheEntry matchedEntry) throws IOException { HttpRequest unconditional = conditionalRequestBuilder .buildUnconditionalRequest(request, matchedEntry); return callBackend(target, unconditional, context); }
private CloseableHttpResponse retryRequestUnconditionally( final HttpRoute route, final HttpRequestWrapper request, final HttpClientContext context, final HttpExecutionAware execAware, final HttpCacheEntry matchedEntry) throws IOException, HttpException { final HttpRequestWrapper unconditional = conditionalRequestBuilder .buildUnconditionalRequest(request, matchedEntry); return callBackend(route, unconditional, context, execAware); }
private AsynchronousValidator makeAsynchronousValidator( final CacheConfig config) { if (config.getAsynchronousWorkersMax() > 0) { return new AsynchronousValidator(this, config); } return null; }
public static CacheConfig createDefaultCacheConfig() { return CacheConfig.custom().setMaxCacheEntries(500).setMaxObjectSize(1024 * 256) .setSharedCache(false).setHeuristicCachingEnabled(true) .setHeuristicDefaultLifetime(86400).build(); }
private boolean staleResponseNotAllowed(HttpRequest request, HttpCacheEntry entry, Date now) { return validityPolicy.mustRevalidate(entry) || (isSharedCache() && validityPolicy.proxyRevalidate(entry)) || explicitFreshnessRequest(request, entry, now); }
private HttpResponse getFatallyNoncompliantResponse(final HttpRequestWrapper request, final HttpContext context) { HttpResponse fatalErrorResponse = null; final List<RequestProtocolError> fatalError = requestCompliance.requestIsFatallyNonCompliant(request); for (final RequestProtocolError error : fatalError) { setResponseStatus(context, CacheResponseStatus.CACHE_MODULE_RESPONSE); fatalErrorResponse = requestCompliance.getErrorForRequest(error); } return fatalErrorResponse; }