Skip to content

Commit

Permalink
Optional refresh policy for Caffeine in-memory cache, v2
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryExtraordinaryUsername committed Nov 14, 2023
1 parent 2aa5291 commit 5e4bb21
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public USCustomLogicModuleCreator(USCustomLogicGppReaderFactory gppReaderFactory
JsonLogic jsonLogic,
Integer cacheTtl,
Integer cacheSize,
boolean refresh,
int refreshPeriod,
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, refresh)
? SettingsCache.createCache(cacheTtl, cacheSize, refreshPeriod)
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public CachingApplicationSettings(ApplicationSettings delegate,
Metrics metrics,
int ttl,
int size,
boolean refresh) {
int refreshPeriod) {

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, refresh);
this.accountToErrorCache = SettingsCache.createCache(ttl, size, refresh);
this.adServerPublisherToErrorCache = SettingsCache.createCache(ttl, size, refresh);
this.categoryConfigCache = SettingsCache.createCache(ttl, size, refresh);
this.accountCache = SettingsCache.createCache(ttl, size, refreshPeriod);
this.accountToErrorCache = SettingsCache.createCache(ttl, size, refreshPeriod);
this.adServerPublisherToErrorCache = SettingsCache.createCache(ttl, size, refreshPeriod);
this.categoryConfigCache = SettingsCache.createCache(ttl, size, refreshPeriod);
this.cache = Objects.requireNonNull(cache);
this.ampCache = Objects.requireNonNull(ampCache);
this.videoCache = Objects.requireNonNull(videoCache);
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/prebid/server/settings/SettingsCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.lang3.ObjectUtils;
import org.prebid.server.settings.model.StoredItem;

import javax.validation.ValidationException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -20,17 +21,20 @@ 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, boolean refresh) {
public SettingsCache(int ttl, int size, int refreshPeriod) {
if (ttl <= 0 || size <= 0) {
throw new IllegalArgumentException("ttl and size must be positive");
}
requestCache = createCache(ttl, size, refresh);
impCache = createCache(ttl, size, refresh);
requestCache = createCache(ttl, size, refreshPeriod);
impCache = createCache(ttl, size, refreshPeriod);
}

public static <T> Map<String, T> createCache(int ttl, int size, boolean refresh) {
public static <T> Map<String, T> createCache(int ttl, int size, int refreshPeriod) {
final Caffeine<Object, Object> caffeine = Caffeine.newBuilder().maximumSize(size);
if (refresh) {
if (refreshPeriod > 0 && refreshPeriod > ttl) {
throw new ValidationException("SettingsCache parameter \"refreshPeriod\" should be less than \"ttl\"");
}
if (refreshPeriod > 0) {
caffeine.refreshAfterWrite(ttl, TimeUnit.SECONDS).expireAfterWrite(ttl * 5L, TimeUnit.SECONDS);
} else {
caffeine.expireAfterWrite(ttl, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ USCustomLogicModuleCreator usCustomLogicModuleCreator(
JsonLogic jsonLogic,
@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.refresh:false}") boolean refresh,
@Value("${settings.in-memory-cache.refreshPeriod:0}") int refreshPeriod,
Metrics metrics) {

return new USCustomLogicModuleCreator(
gppReaderFactory, jsonLogic, ttlSeconds, cacheSize, refresh, metrics);
gppReaderFactory, jsonLogic, ttlSeconds, cacheSize, refreshPeriod, metrics);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ CachingApplicationSettings cachingApplicationSettings(
metrics,
cacheProperties.getTtlSeconds(),
cacheProperties.getCacheSize(),
cacheProperties.isRefresh());
cacheProperties.getRefreshPeriod());
}
}

Expand All @@ -304,21 +304,24 @@ static class CacheConfiguration {
@Qualifier("settingsCache")
SettingsCache settingsCache(ApplicationSettingsCacheProperties cacheProperties) {
return new SettingsCache(
cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), cacheProperties.isRefresh());
cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), cacheProperties.getRefreshPeriod()
);
}

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

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

Expand All @@ -336,6 +339,7 @@ private static class ApplicationSettingsCacheProperties {
@NotNull
@Min(1)
private Integer cacheSize;
private boolean refresh;
@Min(0)
private int refreshPeriod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void setUp() {
given(jsonLogic.parse(any())).willReturn(JsonLogicBoolean.TRUE);

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

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

cachingApplicationSettings = new CachingApplicationSettings(
applicationSettings,
new SettingsCache(360, 100, false),
new SettingsCache(360, 100, false),
new SettingsCache(360, 100, false),
new SettingsCache(360, 100, 0),
new SettingsCache(360, 100, 0),
new SettingsCache(360, 100, 0),
metrics,
360,
100,
false);
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, false);
settingsCache = new SettingsCache(10, 10, 0);
}

@Test
Expand Down

0 comments on commit 5e4bb21

Please sign in to comment.