Skip to content

Commit

Permalink
Bug-fix: HTTP cache to use response Cache-Control s-maxage attribut…
Browse files Browse the repository at this point in the history
…e only when configured as shared
  • Loading branch information
ok2c committed Dec 24, 2023
1 parent 083f9dd commit 6a497be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6a497be

Please sign in to comment.