Class CacheConfig
- java.lang.Object
-
- org.apache.http.impl.client.cache.CacheConfig
-
- All Implemented Interfaces:
java.lang.Cloneable
public class CacheConfig extends java.lang.Object implements java.lang.CloneableJava Beans-style configuration for a
CachingHttpClient. Any class in the caching module that has configuration options should take aCacheConfigargument in one of its constructors. ACacheConfiginstance has sane and conservative defaults, so the easiest way to specify options is to get an instance and then set just the options you want to modify from their defaults.N.B. This class is only for caching-specific configuration; to configure the behavior of the rest of the client, configure the
HttpClientused as the "backend" for theCachingHttpClient.Cache configuration can be grouped into the following categories:
Cache size. If the backend storage supports these limits, you can specify the
maximum number of cache entriesas well as themaximum 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
Authorizationheaders or responses marked withCache-Control: private. If, however, the cache is only going to be used by one logical "user" (behaving similarly to a browser cache), then you will want toturn off the shared cache setting.303 caching. RFC2616 explicitly disallows caching 303 responses; however, the HTTPbis working group says they can be cached if explicitly indicated in the response headers and permitted by the request method. (They also indicate that disallowing 303 caching is actually an unintended spec error in RFC2616). This behavior is off by default, to err on the side of a conservative adherence to the existing standard, but you may want to
enable it.Weak ETags on PUT/DELETE If-Match requests. RFC2616 explicitly prohibits the use of weak validators in non-GET requests, however, the HTTPbis working group says while the limitation for weak validators on ranged requests makes sense, weak ETag validation is useful on full non-GET requests; e.g., PUT with If-Match. This behavior is off by default, to err on the side of a conservative adherence to the existing standard, but you may want to
enable it.Heuristic caching. Per RFC2616, a cache may cache certain cache entries even if no explicit cache control headers are set by the origin. 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 adefault freshness lifetimeand/or afraction 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.Background validation. The cache module supports the
stale-while-revalidatedirective of RFC5861, which allows certain cache entry revalidations to happen in the background. You may want to tweak the settings for theminimumandmaximumnumber of background worker threads, as well as themaximum time they can be idle before being reclaimed. You can also control thesize of the queueused for revalidations when there aren't enough workers to keep up with demand.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCacheConfig.Builder
-
Field Summary
Fields Modifier and Type Field Description static CacheConfigDEFAULTstatic booleanDEFAULT_303_CACHING_ENABLEDDefault setting for 303 cachingstatic intDEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECSDefault maximum idle lifetime for a background revalidation thread before it gets reclaimed.static intDEFAULT_ASYNCHRONOUS_WORKERS_COREDefault minimum number of worker threads to allow for background revalidations resulting from the stale-while-revalidate directive.static intDEFAULT_ASYNCHRONOUS_WORKERS_MAXDefault number of worker threads to allow for background revalidations resulting from the stale-while-revalidate directive.static booleanDEFAULT_HEURISTIC_CACHING_ENABLEDDefault setting for heuristic cachingstatic floatDEFAULT_HEURISTIC_COEFFICIENTDefault coefficient used to heuristically determine freshness lifetime from the Last-Modified time of a cache entry.static longDEFAULT_HEURISTIC_LIFETIMEDefault lifetime in seconds to be assumed when we cannot calculate freshness heuristically.static intDEFAULT_MAX_CACHE_ENTRIESDefault setting for the maximum number of cache entries that will be retained.static intDEFAULT_MAX_OBJECT_SIZE_BYTESDefault setting for the maximum object size that will be cached, in bytes.static intDEFAULT_MAX_UPDATE_RETRIESDefault setting for the number of retries on a failed cache updatestatic intDEFAULT_REVALIDATION_QUEUE_SIZEDefault maximum queue length for background revalidation requests.static booleanDEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWEDDefault setting to allow weak tags on PUT/DELETE methods
-
Constructor Summary
Constructors Constructor Description CacheConfig()Deprecated.(4.3) useCacheConfig.Builder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static CacheConfig.Buildercopy(CacheConfig config)static CacheConfig.Buildercustom()intgetAsynchronousWorkerIdleLifetimeSecs()Returns the current maximum idle lifetime in seconds for a background revalidation worker thread.intgetAsynchronousWorkersCore()Returns the minimum number of threads to keep alive for background revalidations due to thestale-while-revalidatedirective.intgetAsynchronousWorkersMax()Returns the maximum number of threads to allow for background revalidations due to thestale-while-revalidatedirective.floatgetHeuristicCoefficient()Returns lifetime coefficient used in heuristic freshness caching.longgetHeuristicDefaultLifetime()Get the default lifetime to be used if heuristic freshness calculation is not possible.intgetMaxCacheEntries()Returns the maximum number of cache entries the cache will retain.longgetMaxObjectSize()Returns the current maximum response body size that will be cached.intgetMaxObjectSizeBytes()Deprecated.(4.2) usegetMaxObjectSize()intgetMaxUpdateRetries()Returns the number of times to retry a cache update on failureintgetRevalidationQueueSize()Returns the current maximum queue size for background revalidations.booleanis303CachingEnabled()Returns whether 303 caching is enabled.booleanisHeuristicCachingEnabled()Returns whether heuristic caching is enabled.booleanisNeverCacheHTTP10ResponsesWithQuery()Returns whether the cache will never cache HTTP 1.0 responses with a query string or not.booleanisSharedCache()Returns whether the cache will behave as a shared cache or not.booleanisWeakETagOnPutDeleteAllowed()Returns whether weak etags is allowed with PUT/DELETE methods.voidsetAsynchronousWorkerIdleLifetimeSecs(int secs)Deprecated.(4.3) useCacheConfig.Builder.voidsetAsynchronousWorkersCore(int min)Deprecated.(4.3) useCacheConfig.Builder.voidsetAsynchronousWorkersMax(int max)Deprecated.(4.3) useCacheConfig.Builder.voidsetHeuristicCachingEnabled(boolean heuristicCachingEnabled)Deprecated.(4.3) useCacheConfig.Builder.voidsetHeuristicCoefficient(float heuristicCoefficient)Deprecated.(4.3) useCacheConfig.Builder.voidsetHeuristicDefaultLifetime(long heuristicDefaultLifetimeSecs)Deprecated.(4.3) useCacheConfig.Builder.voidsetMaxCacheEntries(int maxCacheEntries)Deprecated.(4.3) useCacheConfig.Builder.voidsetMaxObjectSize(long maxObjectSize)Deprecated.(4.3) useCacheConfig.Builder.voidsetMaxObjectSizeBytes(int maxObjectSizeBytes)Deprecated.(4.2) useCacheConfig.Builder.voidsetMaxUpdateRetries(int maxUpdateRetries)Deprecated.(4.3) useCacheConfig.Builder.voidsetRevalidationQueueSize(int size)Deprecated.(4.3) useCacheConfig.Builder.voidsetSharedCache(boolean isSharedCache)Deprecated.(4.3) useCacheConfig.Builder.java.lang.StringtoString()
-
-
-
Field Detail
-
DEFAULT_MAX_OBJECT_SIZE_BYTES
public static final int DEFAULT_MAX_OBJECT_SIZE_BYTES
Default setting for the maximum object size that will be cached, in bytes.- See Also:
- Constant Field Values
-
DEFAULT_MAX_CACHE_ENTRIES
public static final int DEFAULT_MAX_CACHE_ENTRIES
Default setting for the maximum number of cache entries that will be retained.- See Also:
- Constant Field Values
-
DEFAULT_MAX_UPDATE_RETRIES
public static final int DEFAULT_MAX_UPDATE_RETRIES
Default setting for the number of retries on a failed cache update- See Also:
- Constant Field Values
-
DEFAULT_303_CACHING_ENABLED
public static final boolean DEFAULT_303_CACHING_ENABLED
Default setting for 303 caching- See Also:
- Constant Field Values
-
DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED
public static final boolean DEFAULT_WEAK_ETAG_ON_PUTDELETE_ALLOWED
Default setting to allow weak tags on PUT/DELETE methods- See Also:
- Constant Field Values
-
DEFAULT_HEURISTIC_CACHING_ENABLED
public static final boolean DEFAULT_HEURISTIC_CACHING_ENABLED
Default setting for heuristic caching- See Also:
- Constant Field Values
-
DEFAULT_HEURISTIC_COEFFICIENT
public static final float DEFAULT_HEURISTIC_COEFFICIENT
Default coefficient used to heuristically determine freshness lifetime from the Last-Modified time of a cache entry.- See Also:
- Constant Field Values
-
DEFAULT_HEURISTIC_LIFETIME
public static final long DEFAULT_HEURISTIC_LIFETIME
Default lifetime in seconds to be assumed when we cannot calculate freshness heuristically.- See Also:
- Constant Field Values
-
DEFAULT_ASYNCHRONOUS_WORKERS_MAX
public static final int DEFAULT_ASYNCHRONOUS_WORKERS_MAX
Default number of worker threads to allow for background revalidations resulting from the stale-while-revalidate directive.- See Also:
- Constant Field Values
-
DEFAULT_ASYNCHRONOUS_WORKERS_CORE
public static final int DEFAULT_ASYNCHRONOUS_WORKERS_CORE
Default minimum number of worker threads to allow for background revalidations resulting from the stale-while-revalidate directive.- See Also:
- Constant Field Values
-
DEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS
public static final int DEFAULT_ASYNCHRONOUS_WORKER_IDLE_LIFETIME_SECS
Default maximum idle lifetime for a background revalidation thread before it gets reclaimed.- See Also:
- Constant Field Values
-
DEFAULT_REVALIDATION_QUEUE_SIZE
public static final int DEFAULT_REVALIDATION_QUEUE_SIZE
Default maximum queue length for background revalidation requests.- See Also:
- Constant Field Values
-
DEFAULT
public static final CacheConfig DEFAULT
-
-
Constructor Detail
-
CacheConfig
@Deprecated public CacheConfig()
Deprecated.(4.3) useCacheConfig.Builder.
-
-
Method Detail
-
getMaxObjectSizeBytes
@Deprecated public int getMaxObjectSizeBytes()
Deprecated.(4.2) usegetMaxObjectSize()Returns the current maximum response body size that will be cached.- Returns:
- size in bytes
-
setMaxObjectSizeBytes
@Deprecated public void setMaxObjectSizeBytes(int maxObjectSizeBytes)
Deprecated.(4.2) useCacheConfig.Builder.Specifies the maximum response body size that will be eligible for caching.- Parameters:
maxObjectSizeBytes- size in bytes
-
getMaxObjectSize
public long getMaxObjectSize()
Returns the current maximum response body size that will be cached.- Returns:
- size in bytes
- Since:
- 4.2
-
setMaxObjectSize
@Deprecated public void setMaxObjectSize(long maxObjectSize)
Deprecated.(4.3) useCacheConfig.Builder.Specifies the maximum response body size that will be eligible for caching.- Parameters:
maxObjectSize- size in bytes- Since:
- 4.2
-
isNeverCacheHTTP10ResponsesWithQuery
public boolean isNeverCacheHTTP10ResponsesWithQuery()
Returns whether the cache will never cache HTTP 1.0 responses with a query string or not.- Returns:
trueto not cache query string responses,falseto cache if explicit cache headers are found
-
getMaxCacheEntries
public int getMaxCacheEntries()
Returns the maximum number of cache entries the cache will retain.
-
setMaxCacheEntries
@Deprecated public void setMaxCacheEntries(int maxCacheEntries)
Deprecated.(4.3) useCacheConfig.Builder.Sets the maximum number of cache entries the cache will retain.
-
getMaxUpdateRetries
public int getMaxUpdateRetries()
Returns the number of times to retry a cache update on failure
-
setMaxUpdateRetries
@Deprecated public void setMaxUpdateRetries(int maxUpdateRetries)
Deprecated.(4.3) useCacheConfig.Builder.Sets the number of times to retry a cache update on failure
-
is303CachingEnabled
public boolean is303CachingEnabled()
Returns whether 303 caching is enabled.- Returns:
trueif it is enabled.
-
isWeakETagOnPutDeleteAllowed
public boolean isWeakETagOnPutDeleteAllowed()
Returns whether weak etags is allowed with PUT/DELETE methods.- Returns:
trueif it is allowed.
-
isHeuristicCachingEnabled
public boolean isHeuristicCachingEnabled()
Returns whether heuristic caching is enabled.- Returns:
trueif it is enabled.
-
setHeuristicCachingEnabled
@Deprecated public void setHeuristicCachingEnabled(boolean heuristicCachingEnabled)
Deprecated.(4.3) useCacheConfig.Builder.Enables or disables heuristic caching.- Parameters:
heuristicCachingEnabled- should betrueto permit heuristic caching,falseto disable it.
-
getHeuristicCoefficient
public float getHeuristicCoefficient()
Returns lifetime coefficient used in heuristic freshness caching.
-
setHeuristicCoefficient
@Deprecated public void setHeuristicCoefficient(float heuristicCoefficient)
Deprecated.(4.3) useCacheConfig.Builder.Sets coefficient to be used in heuristic freshness caching. This is interpreted as the fraction of the time between theLast-ModifiedandDateheaders of a cached response during which the cached response will be considered heuristically fresh.- Parameters:
heuristicCoefficient- should be between0.0and1.0.
-
getHeuristicDefaultLifetime
public long getHeuristicDefaultLifetime()
Get the default lifetime to be used if heuristic freshness calculation is not possible.
-
setHeuristicDefaultLifetime
@Deprecated public void setHeuristicDefaultLifetime(long heuristicDefaultLifetimeSecs)
Deprecated.(4.3) useCacheConfig.Builder.Sets default lifetime in seconds to be used if heuristic freshness calculation is not possible. Explicit cache control directives on either the request or origin response will override this, as will the heuristicLast-Modifiedfreshness calculation if it is available.- Parameters:
heuristicDefaultLifetimeSecs- is the number of seconds to consider a cache-eligible response fresh in the absence of other information. Set this to0to disable this style of heuristic caching.
-
isSharedCache
public boolean isSharedCache()
Returns whether the cache will behave as a shared cache or not.- Returns:
truefor a shared cache,falsefor a non- shared (private) cache
-
setSharedCache
@Deprecated public void setSharedCache(boolean isSharedCache)
Deprecated.(4.3) useCacheConfig.Builder.Sets whether the cache should behave as a shared cache or not.- Parameters:
isSharedCache- true to behave as a shared cache, false to behave as a non-shared (private) cache. To have the cache behave like a browser cache, you want to set this tofalse.
-
getAsynchronousWorkersMax
public int getAsynchronousWorkersMax()
Returns the maximum number of threads to allow for background revalidations due to thestale-while-revalidatedirective. A value of 0 means background revalidations are disabled.
-
setAsynchronousWorkersMax
@Deprecated public void setAsynchronousWorkersMax(int max)
Deprecated.(4.3) useCacheConfig.Builder.Sets the maximum number of threads to allow for background revalidations due to thestale-while-revalidatedirective.- Parameters:
max- number of threads; a value of 0 disables background revalidations.
-
getAsynchronousWorkersCore
public int getAsynchronousWorkersCore()
Returns the minimum number of threads to keep alive for background revalidations due to thestale-while-revalidatedirective.
-
setAsynchronousWorkersCore
@Deprecated public void setAsynchronousWorkersCore(int min)
Deprecated.(4.3) useCacheConfig.Builder.Sets the minimum number of threads to keep alive for background revalidations due to thestale-while-revalidatedirective.- Parameters:
min- should be greater than zero and less than or equal togetAsynchronousWorkersMax()
-
getAsynchronousWorkerIdleLifetimeSecs
public int getAsynchronousWorkerIdleLifetimeSecs()
Returns the current maximum idle lifetime in seconds for a background revalidation worker thread. If a worker thread is idle for this long, and there are more than the core number of worker threads alive, the worker will be reclaimed.
-
setAsynchronousWorkerIdleLifetimeSecs
@Deprecated public void setAsynchronousWorkerIdleLifetimeSecs(int secs)
Deprecated.(4.3) useCacheConfig.Builder.Sets the current maximum idle lifetime in seconds for a background revalidation worker thread. If a worker thread is idle for this long, and there are more than the core number of worker threads alive, the worker will be reclaimed.- Parameters:
secs- idle lifetime in seconds
-
getRevalidationQueueSize
public int getRevalidationQueueSize()
Returns the current maximum queue size for background revalidations.
-
setRevalidationQueueSize
@Deprecated public void setRevalidationQueueSize(int size)
Deprecated.(4.3) useCacheConfig.Builder.Sets the current maximum queue size for background revalidations.
-
custom
public static CacheConfig.Builder custom()
-
copy
public static CacheConfig.Builder copy(CacheConfig config)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-