Bug-fix: HTTP cache to use response Cache-Control s-maxage attribute only when configured as shared

This commit is contained in:
Oleg Kalnichevski 2023-12-24 11:59:56 +01:00
parent 083f9dd74f
commit 6a497be400
2 changed files with 24 additions and 5 deletions

View File

@ -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

View File

@ -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()