From 8d217b873e44aa96d0be831b7df03cb4616e93c6 Mon Sep 17 00:00:00 2001 From: Will Ezell Date: Mon, 18 Nov 2024 11:53:16 -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/caffine/CaffineCache.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) 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 fe149aaec3b2..d3734a57664e 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 @@ -31,6 +31,7 @@ * i.e., for the "graphqlquerycache" group *

* cache.graphqlquerycache.chain=com.dotmarketing.business.cache.provider.caffine.CaffineCache + * cache.graphqlquerycache.size=10000 * cache.graphqlquerycache.seconds=15 */ public class CaffineCache extends CacheProvider { @@ -59,7 +60,9 @@ public boolean isDistributed() { @Override public void init() { - isInitialized.compareAndSet(false, doInit()); + if(!isInitialized.getAndSet(true)){ + doInit(); + }; } private boolean doInit() { @@ -116,8 +119,7 @@ public Object get(String group, String key) { if (group == null || key == null) { return null; } - DynamicTTLCache cache = getCache(group); - return cache.getIfPresent(key.toLowerCase()); + return getCache(group).getIfPresent(key.toLowerCase()); } @@ -128,15 +130,14 @@ public void remove(String group, String key) { return; } - DynamicTTLCache cache = getCache(group); // Invalidates from Cache a key from a given group - cache.invalidate(key.toLowerCase()); + getCache(group).invalidate(key.toLowerCase()); } @Override public void remove(String group) { Logger.debug(this.getClass(), "===== Calling remove for [" + getName() - + "] - " + cacheKey(group, "")); + + "] - " + group); if (group == null) { return; } @@ -150,8 +151,7 @@ public void removeAll() { @Override public Set getKeys(String group) { - DynamicTTLCache cache = getCache(group); - return cache.asMap().keySet(); + return getCache(group).asMap().keySet(); } @Override @@ -194,7 +194,7 @@ public CacheProviderStats getStats() { long millis = foundCache.defaultTTLInMillis; - String duration = millis == Long.MAX_VALUE ? "" : " / " + nf.format(millis / 1000) + "s"; + String duration = millis == Long.MAX_VALUE ? "" : " | ttl:" + nf.format(millis / 1000) + "s"; com.github.benmanes.caffeine.cache.stats.CacheStats cstats = foundCache.stats(); stats.addStat(CacheStats.REGION, group); @@ -226,9 +226,7 @@ public void shutdown() { isInitialized.set(false); } - private String cacheKey(String group, String key) { - return (group + ":" + key).toLowerCase(); - } + private DynamicTTLCache getCache(String cacheNameIn) { if (cacheNameIn == null) {