Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved expiration policy for Caffeine in-memory cache #2752

Closed
wants to merge 4 commits into from
Closed
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,7 +19,6 @@
import org.prebid.server.json.JsonLogic;
import org.prebid.server.metric.MetricName;
import org.prebid.server.metric.Metrics;
import org.prebid.server.settings.SettingsCache;
import org.prebid.server.settings.model.activity.privacy.AccountUSCustomLogicModuleConfig;

import java.util.Collection;
Expand All @@ -46,17 +45,13 @@ public class USCustomLogicModuleCreator implements PrivacyModuleCreator {

public USCustomLogicModuleCreator(USCustomLogicGppReaderFactory gppReaderFactory,
JsonLogic jsonLogic,
Integer cacheTtl,
Integer cacheSize,
Map<String, JsonLogicNode> jsonLogicNodesCache,
Metrics metrics) {

this.gppReaderFactory = Objects.requireNonNull(gppReaderFactory);
this.jsonLogic = Objects.requireNonNull(jsonLogic);
this.metrics = Objects.requireNonNull(metrics);

jsonLogicNodesCache = cacheTtl != null && cacheSize != null
? SettingsCache.createCache(cacheTtl, cacheSize)
: null;
this.jsonLogicNodesCache = jsonLogicNodesCache;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ public CachingApplicationSettings(ApplicationSettings delegate,
SettingsCache videoCache,
Metrics metrics,
int ttl,
int size) {
int size,
int jitter) {

if (ttl <= 0 || size <= 0) {
throw new IllegalArgumentException("ttl and size must be positive");
}
this.delegate = Objects.requireNonNull(delegate);
this.accountCache = SettingsCache.createCache(ttl, size);
this.accountToErrorCache = SettingsCache.createCache(ttl, size);
this.adServerPublisherToErrorCache = SettingsCache.createCache(ttl, size);
this.categoryConfigCache = SettingsCache.createCache(ttl, size);
this.accountCache = SettingsCache.createCache(ttl, size, jitter);
this.accountToErrorCache = SettingsCache.createCache(ttl, size, jitter);
this.adServerPublisherToErrorCache = SettingsCache.createCache(ttl, size, jitter);
this.categoryConfigCache = SettingsCache.createCache(ttl, size, jitter);
this.cache = Objects.requireNonNull(cache);
this.ampCache = Objects.requireNonNull(ampCache);
this.videoCache = Objects.requireNonNull(videoCache);
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/prebid/server/settings/SettingsCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prebid.server.settings;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -10,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -20,20 +22,25 @@ public class SettingsCache implements CacheNotificationListener {
private final Map<String, Set<StoredItem>> requestCache;
private final Map<String, Set<StoredItem>> impCache;

public SettingsCache(int ttl, int size) {
public SettingsCache(int ttl, int size, int jitter) {
if (ttl <= 0 || size <= 0) {
throw new IllegalArgumentException("ttl and size must be positive");
}
requestCache = createCache(ttl, size);
impCache = createCache(ttl, size);
requestCache = createCache(ttl, size, jitter);
impCache = createCache(ttl, size, jitter);
}

public static <T> Map<String, T> createCache(int ttl, int size) {
return Caffeine.newBuilder()
public static <T> Map<String, T> createCache(int ttl, int size, int jitter) {
final Cache<String, T> cache = Caffeine.newBuilder()
.expireAfterWrite(ttl, TimeUnit.SECONDS)
.maximumSize(size)
.<String, T>build()
.asMap();
.build();
cache.policy()
.expireAfterWrite()
.ifPresent(expiration -> expiration.setExpiresAfter(
jitter != 0 ? ttl + ThreadLocalRandom.current().nextLong(jitter * -1, jitter) : ttl,
TimeUnit.SECONDS));
VeryExtraordinaryUsername marked this conversation as resolved.
Show resolved Hide resolved
return cache.asMap();
}

Map<String, Set<StoredItem>> getRequestCache() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prebid.server.spring.config;

import io.github.jamsesso.jsonlogic.ast.JsonLogicNode;
import org.prebid.server.activity.infrastructure.creator.ActivityInfrastructureCreator;
import org.prebid.server.activity.infrastructure.creator.ActivityRuleFactory;
import org.prebid.server.activity.infrastructure.creator.privacy.PrivacyModuleCreator;
Expand All @@ -13,11 +14,13 @@
import org.prebid.server.activity.infrastructure.creator.rule.RuleCreator;
import org.prebid.server.json.JsonLogic;
import org.prebid.server.metric.Metrics;
import org.prebid.server.settings.SettingsCache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

@Configuration
public class ActivityInfrastructureConfiguration {
Expand Down Expand Up @@ -47,15 +50,26 @@ USCustomLogicGppReaderFactory usCustomLogicGppReaderFactory() {
return new USCustomLogicGppReaderFactory();
}

@Bean
Map<String, JsonLogicNode> jsonLogicNodesCache(
@Value("${settings.in-memory-cache.ttl-seconds:#{null}}") Integer ttlSeconds,
@Value("${settings.in-memory-cache.cache-size:#{null}}") Integer cacheSize,
@Value("${settings.in-memory-cache.cache-size:0}") int jitter) {

return ttlSeconds != null && cacheSize != null
? SettingsCache.createCache(ttlSeconds, cacheSize, jitter)
: null;
}

@Bean
USCustomLogicModuleCreator usCustomLogicModuleCreator(
USCustomLogicGppReaderFactory gppReaderFactory,
JsonLogic jsonLogic,
@Value("${settings.in-memory-cache.ttl-seconds:#{null}}") Integer ttlSeconds,
@Value("${settings.in-memory-cache.cache-size:#{null}}") Integer cacheSize,
Map<String, JsonLogicNode> jsonLogicNodesCache,
Metrics metrics) {

return new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, ttlSeconds, cacheSize, metrics);
return new USCustomLogicModuleCreator(
gppReaderFactory, jsonLogic, jsonLogicNodesCache, metrics);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ CachingApplicationSettings cachingApplicationSettings(
videoCache,
metrics,
cacheProperties.getTtlSeconds(),
cacheProperties.getCacheSize());
cacheProperties.getCacheSize(),
cacheProperties.getJitter());
}
}

Expand All @@ -302,19 +303,22 @@ static class CacheConfiguration {
@Bean
@Qualifier("settingsCache")
SettingsCache settingsCache(ApplicationSettingsCacheProperties cacheProperties) {
return new SettingsCache(cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize());
return new SettingsCache(
cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), cacheProperties.getJitter());
}

@Bean
@Qualifier("ampSettingsCache")
SettingsCache ampSettingsCache(ApplicationSettingsCacheProperties cacheProperties) {
return new SettingsCache(cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize());
return new SettingsCache(
cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), cacheProperties.getJitter());
}

@Bean
@Qualifier("videoSettingCache")
SettingsCache videoSettingCache(ApplicationSettingsCacheProperties cacheProperties) {
return new SettingsCache(cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize());
return new SettingsCache(
cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), cacheProperties.getJitter());
}
}

Expand All @@ -332,5 +336,6 @@ private static class ApplicationSettingsCacheProperties {
@NotNull
@Min(1)
private Integer cacheSize;
private int jitter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void setUp() {
.willReturn(new USNationalGppReader(null));
given(jsonLogic.parse(any())).willReturn(JsonLogicBoolean.TRUE);

target = new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, null, null, metrics);
target = new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, null, metrics);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ public void setUp() {

cachingApplicationSettings = new CachingApplicationSettings(
applicationSettings,
new SettingsCache(360, 100),
new SettingsCache(360, 100),
new SettingsCache(360, 100),
new SettingsCache(360, 100, 0),
new SettingsCache(360, 100, 0),
new SettingsCache(360, 100, 0),
metrics,
360,
100);
100,
0);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SettingsCacheTest {

@Before
public void setUp() {
settingsCache = new SettingsCache(10, 10);
settingsCache = new SettingsCache(10, 10, 0);
}

@Test
Expand Down
Loading