From f40ac88ef020186bc2d6242622f28f0e9b60f3c9 Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Tue, 19 Nov 2024 13:24:28 -0500 Subject: [PATCH] feat(cache) per object ttl for cache items. This adds TTL capibilites to our CaffineCache and really deprecates the timed cache provider. ref:#30670 --- .../cache/provider/CacheProviderAPIImpl.java | 6 +++--- .../java/com/dotcms/cache/DynamicTTLCache.java | 4 ++-- .../cache/provider/caffine/CaffineCache.java | 17 ++++++++--------- .../com/dotcms/cache/DynamicTTLCacheTest.java | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/dotCMS/src/enterprise/java/com/dotcms/enterprise/cache/provider/CacheProviderAPIImpl.java b/dotCMS/src/enterprise/java/com/dotcms/enterprise/cache/provider/CacheProviderAPIImpl.java index be77618042fa..5fe668b9e42a 100644 --- a/dotCMS/src/enterprise/java/com/dotcms/enterprise/cache/provider/CacheProviderAPIImpl.java +++ b/dotCMS/src/enterprise/java/com/dotcms/enterprise/cache/provider/CacheProviderAPIImpl.java @@ -178,10 +178,10 @@ private List getProvidersForRegion ( String group ) { } List initProviders(List cacheProviders) { - cacheProviders.forEach(provider -> { + cacheProviders.forEach(provider -> Try.run(provider::init).onFailure( - e -> Logger.error(this, "Error initializing CacheProvider [" + provider.getName() + "].", e)); - }); + e -> Logger.error(this, "Error initializing CacheProvider [" + provider.getName() + "].", e)) + ); return cacheProviders; } diff --git a/dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java b/dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java index 08de90beac30..1db1702066ae 100644 --- a/dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java +++ b/dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java @@ -14,9 +14,9 @@ * This class constructs a cache with dynamic TTLs for each key. * Relies on Caffeine for the underlying cache implementation. * @param - * @param + * @param */ -public class DynamicTTLCache { +public class DynamicTTLCache { public final long defaultTTLInMillis; private final Cache cache; diff --git a/dotCMS/src/main/java/com/dotmarketing/business/cache/provider/caffine/CaffineCache.java b/dotCMS/src/main/java/com/dotmarketing/business/cache/provider/caffine/CaffineCache.java index 525f312a75b8..3d47e5901fd6 100644 --- a/dotCMS/src/main/java/com/dotmarketing/business/cache/provider/caffine/CaffineCache.java +++ b/dotCMS/src/main/java/com/dotmarketing/business/cache/provider/caffine/CaffineCache.java @@ -23,8 +23,8 @@ /** * In-Memory Cache implementation using https://github.com/ben-manes/caffeine *

- * Supports key-specific time invalidations by providing an {@link Expirable} object in the - * {@link #put(String, String, Object)} method with the desired TTL. + * Supports key-specific time invalidations by using the method + * {@link #put(String, String, Object, long)} method with the desired TTL. *

* A group-wide invalidation time can also be set by config properties. *

@@ -33,6 +33,10 @@ * cache.graphqlquerycache.chain=com.dotmarketing.business.cache.provider.caffine.CaffineCache * cache.graphqlquerycache.size=10000 * cache.graphqlquerycache.seconds=15 + * + * An individual object's TTL will override the group's TTL. + * + * */ public class CaffineCache extends CacheProvider { @@ -65,16 +69,11 @@ public boolean isDistributed() { @Override public void init() { if(!isInitialized.getAndSet(true)){ - doInit(); + groups.clear(); + Logger.info(this.getClass(), "===== Initializing [" + getName() + "]."); }; } - private boolean doInit() { - groups.clear(); - Logger.info(this.getClass(), "===== Initializing [" + getName() + "]."); - return true; - } - @Override public boolean isInitialized() throws Exception { return isInitialized.get(); diff --git a/dotCMS/src/test/java/com/dotcms/cache/DynamicTTLCacheTest.java b/dotCMS/src/test/java/com/dotcms/cache/DynamicTTLCacheTest.java index e957394938a2..184001ee25c4 100644 --- a/dotCMS/src/test/java/com/dotcms/cache/DynamicTTLCacheTest.java +++ b/dotCMS/src/test/java/com/dotcms/cache/DynamicTTLCacheTest.java @@ -73,4 +73,18 @@ public void testCacheExpiry()throws InterruptedException { } + + @Test + public void testCacheTTLGreaterThanObjectTTL()throws InterruptedException { + // cache has a 5000ms timeout + cache.put("10Seconds", "10Seconds", 10000); + TimeUnit.MILLISECONDS.sleep(7000); + assertNotNull(cache.getIfPresent("10Seconds")); + TimeUnit.MILLISECONDS.sleep(4000); + assertNull(cache.getIfPresent("10Seconds")); + } + + + + }