Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.github.benmanes.caffeine.cache.AsyncCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Policy;
import com.github.benmanes.caffeine.cache.Weigher;
import com.github.benmanes.caffeine.cache.stats.CacheStats;

Expand Down Expand Up @@ -57,7 +58,10 @@ public CaffeineCache(
builder.maximumSize(maxCacheSize);
}
builder.expireAfterWrite(Duration.ofSeconds(lifespanSeconds));
builder.expireAfterAccess(Duration.ofSeconds(maxIdleSeconds != -1 ? maxIdleSeconds : 180));
// -1 means disabled (see InklessConfig.CONSUME_CACHE_EXPIRATION_MAX_IDLE_SEC_CONFIG doc)
if (maxIdleSeconds != -1) {
builder.expireAfterAccess(Duration.ofSeconds(maxIdleSeconds));
}
builder.recordStats();
cache = builder.buildAsync();
metrics = new CaffeineCacheMetrics(cache.synchronous());
Expand Down Expand Up @@ -132,6 +136,11 @@ void cleanUp() {
cache.synchronous().cleanUp();
}

// visible for testing
Policy<CacheKey, FileExtent> policy() {
return cache.synchronous().policy();
}

public CacheStats stats() {
return cache.synchronous().stats();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.junit.jupiter.api.Test;

import java.time.Duration;

import io.aiven.inkless.generated.CacheKey;
import io.aiven.inkless.generated.FileExtent;

Expand Down Expand Up @@ -55,6 +57,22 @@ void bytesBasedEvictionWhenBytesConfigured() throws Exception {
}
}

@Test
void negativeMaxIdleDisablesExpireAfterAccess() throws Exception {
// -1 means "disabled" per InklessConfig docs: no expireAfterAccess policy should be set.
try (CaffeineCache cache = new CaffeineCache(2, 0, 3600, -1)) {
assertThat(cache.policy().expireAfterAccess()).isEmpty();
}
}

@Test
void positiveMaxIdleSetsExpireAfterAccess() throws Exception {
try (CaffeineCache cache = new CaffeineCache(2, 0, 3600, 180)) {
assertThat(cache.policy().expireAfterAccess())
.hasValueSatisfying(p -> assertThat(p.getExpiresAfter()).isEqualTo(Duration.ofSeconds(180)));
}
}

@Test
void bytesBasedEvictionIgnoresCountLimit() throws Exception {
// maxCacheSize = 1 but bytes takes precedence; 200 bytes fits 4x50-byte entries
Expand Down
Loading