diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java index 63d941763..f3c99f1a1 100644 --- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java +++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java @@ -42,6 +42,7 @@ class CacheValidityPolicy { private static final Logger LOG = LoggerFactory.getLogger(CacheValidityPolicy.class); + private final boolean shared; private final boolean useHeuristicCaching; private final float heuristicCoefficient; private final TimeValue heuristicDefaultLifetime; @@ -55,6 +56,7 @@ class CacheValidityPolicy { */ CacheValidityPolicy(final CacheConfig config) { super(); + this.shared = config != null ? config.isSharedCache() : CacheConfig.DEFAULT.isSharedCache(); this.useHeuristicCaching = config != null ? config.isHeuristicCachingEnabled() : CacheConfig.DEFAULT.isHeuristicCachingEnabled(); this.heuristicCoefficient = config != null ? config.getHeuristicCoefficient() : CacheConfig.DEFAULT.getHeuristicCoefficient(); this.heuristicDefaultLifetime = config != null ? config.getHeuristicDefaultLifetime() : CacheConfig.DEFAULT.getHeuristicDefaultLifetime(); @@ -87,12 +89,14 @@ public TimeValue getCurrentAge(final HttpCacheEntry entry, final Instant now) { */ public TimeValue getFreshnessLifetime(final ResponseCacheControl responseCacheControl, final HttpCacheEntry entry) { // If the cache is shared and the s-maxage response directive is present, use its value - final long sharedMaxAge = responseCacheControl.getSharedMaxAge(); - if (sharedMaxAge > -1) { - if (LOG.isDebugEnabled()) { - LOG.debug("Using s-maxage directive for freshness lifetime calculation: {} seconds", sharedMaxAge); + if (shared) { + final long sharedMaxAge = responseCacheControl.getSharedMaxAge(); + if (sharedMaxAge > -1) { + if (LOG.isDebugEnabled()) { + LOG.debug("Using s-maxage directive for freshness lifetime calculation: {} seconds", sharedMaxAge); + } + return TimeValue.ofSeconds(sharedMaxAge); } - return TimeValue.ofSeconds(sharedMaxAge); } // If the max-age response directive is present, use its value diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java index 02cf52fcd..06d4aa7de 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java @@ -152,11 +152,26 @@ protected TimeValue getResidentTime(final HttpCacheEntry ent, final Instant d) { public void testFreshnessLifetimeIsSMaxAgeIfPresent() { final ResponseCacheControl cacheControl = ResponseCacheControl.builder() .setSharedMaxAge(10) + .setMaxAge(5) .build(); final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); assertEquals(TimeValue.ofSeconds(10), impl.getFreshnessLifetime(cacheControl, entry)); } + @Test + public void testSMaxAgeIsIgnoredWhenNotShared() { + final CacheConfig cacheConfig = CacheConfig.custom() + .setSharedCache(false) + .build(); + impl = new CacheValidityPolicy(cacheConfig); + final ResponseCacheControl cacheControl = ResponseCacheControl.builder() + .setSharedMaxAge(10) + .setMaxAge(5) + .build(); + final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); + assertEquals(TimeValue.ofSeconds(5), impl.getFreshnessLifetime(cacheControl, entry)); + } + @Test public void testFreshnessLifetimeIsMaxAgeIfPresent() { final ResponseCacheControl cacheControl = ResponseCacheControl.builder()