From 5e4bb21e58e1a4befdaad6d8b1303c8b339c0383 Mon Sep 17 00:00:00 2001 From: Serhii Kolomiiets Date: Tue, 14 Nov 2023 15:39:48 +0200 Subject: [PATCH] Optional refresh policy for Caffeine in-memory cache, v2 --- .../uscustomlogic/USCustomLogicModuleCreator.java | 4 ++-- .../settings/CachingApplicationSettings.java | 10 +++++----- .../org/prebid/server/settings/SettingsCache.java | 14 +++++++++----- .../ActivityInfrastructureConfiguration.java | 4 ++-- .../spring/config/SettingsConfiguration.java | 14 +++++++++----- .../USCustomLogicModuleCreatorTest.java | 2 +- .../settings/CachingApplicationSettingsTest.java | 8 ++++---- .../prebid/server/settings/SettingsCacheTest.java | 2 +- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreator.java b/src/main/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreator.java index da2fb4ec2ca..5576403d02f 100644 --- a/src/main/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreator.java +++ b/src/main/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreator.java @@ -48,7 +48,7 @@ public USCustomLogicModuleCreator(USCustomLogicGppReaderFactory gppReaderFactory JsonLogic jsonLogic, Integer cacheTtl, Integer cacheSize, - boolean refresh, + int refreshPeriod, Metrics metrics) { this.gppReaderFactory = Objects.requireNonNull(gppReaderFactory); @@ -56,7 +56,7 @@ public USCustomLogicModuleCreator(USCustomLogicGppReaderFactory gppReaderFactory this.metrics = Objects.requireNonNull(metrics); jsonLogicNodesCache = cacheTtl != null && cacheSize != null - ? SettingsCache.createCache(cacheTtl, cacheSize, refresh) + ? SettingsCache.createCache(cacheTtl, cacheSize, refreshPeriod) : null; } diff --git a/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java b/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java index 3730fb0ee0a..2107691dcfd 100644 --- a/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java +++ b/src/main/java/org/prebid/server/settings/CachingApplicationSettings.java @@ -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); diff --git a/src/main/java/org/prebid/server/settings/SettingsCache.java b/src/main/java/org/prebid/server/settings/SettingsCache.java index 9a336858516..57641e96cee 100644 --- a/src/main/java/org/prebid/server/settings/SettingsCache.java +++ b/src/main/java/org/prebid/server/settings/SettingsCache.java @@ -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; @@ -20,17 +21,20 @@ public class SettingsCache implements CacheNotificationListener { private final Map> requestCache; private final Map> 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 Map createCache(int ttl, int size, boolean refresh) { + public static Map createCache(int ttl, int size, int refreshPeriod) { final Caffeine 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); diff --git a/src/main/java/org/prebid/server/spring/config/ActivityInfrastructureConfiguration.java b/src/main/java/org/prebid/server/spring/config/ActivityInfrastructureConfiguration.java index 58041461f66..81a8f8daa78 100644 --- a/src/main/java/org/prebid/server/spring/config/ActivityInfrastructureConfiguration.java +++ b/src/main/java/org/prebid/server/spring/config/ActivityInfrastructureConfiguration.java @@ -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); } } } diff --git a/src/main/java/org/prebid/server/spring/config/SettingsConfiguration.java b/src/main/java/org/prebid/server/spring/config/SettingsConfiguration.java index 3142152547c..0a04823ba62 100644 --- a/src/main/java/org/prebid/server/spring/config/SettingsConfiguration.java +++ b/src/main/java/org/prebid/server/spring/config/SettingsConfiguration.java @@ -281,7 +281,7 @@ CachingApplicationSettings cachingApplicationSettings( metrics, cacheProperties.getTtlSeconds(), cacheProperties.getCacheSize(), - cacheProperties.isRefresh()); + cacheProperties.getRefreshPeriod()); } } @@ -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() + ); } } @@ -336,6 +339,7 @@ private static class ApplicationSettingsCacheProperties { @NotNull @Min(1) private Integer cacheSize; - private boolean refresh; + @Min(0) + private int refreshPeriod; } } diff --git a/src/test/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreatorTest.java b/src/test/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreatorTest.java index eb297a33671..8f95a9ba798 100644 --- a/src/test/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreatorTest.java +++ b/src/test/java/org/prebid/server/activity/infrastructure/creator/privacy/uscustomlogic/USCustomLogicModuleCreatorTest.java @@ -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 diff --git a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java index fce4f7a5c53..ec4546bb2a9 100644 --- a/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java +++ b/src/test/java/org/prebid/server/settings/CachingApplicationSettingsTest.java @@ -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 diff --git a/src/test/java/org/prebid/server/settings/SettingsCacheTest.java b/src/test/java/org/prebid/server/settings/SettingsCacheTest.java index 080f3883f19..f185d40edd7 100644 --- a/src/test/java/org/prebid/server/settings/SettingsCacheTest.java +++ b/src/test/java/org/prebid/server/settings/SettingsCacheTest.java @@ -15,7 +15,7 @@ public class SettingsCacheTest { @Before public void setUp() { - settingsCache = new SettingsCache(10, 10, false); + settingsCache = new SettingsCache(10, 10, 0); } @Test