Skip to content

Commit

Permalink
feat(cache) per object ttl for cache items. This adds TTL capibilites…
Browse files Browse the repository at this point in the history
… to our CaffineCache and really deprecates the timed cache provider.

ref:#30670
  • Loading branch information
wezell committed Nov 18, 2024
1 parent 5442163 commit 8d217b8
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* i.e., for the "graphqlquerycache" group
* <p>
* cache.graphqlquerycache.chain=com.dotmarketing.business.cache.provider.caffine.CaffineCache
* cache.graphqlquerycache.size=10000
* cache.graphqlquerycache.seconds=15
*/
public class CaffineCache extends CacheProvider {
Expand Down Expand Up @@ -59,7 +60,9 @@ public boolean isDistributed() {

@Override
public void init() {
isInitialized.compareAndSet(false, doInit());
if(!isInitialized.getAndSet(true)){
doInit();
};
}

private boolean doInit() {
Expand Down Expand Up @@ -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());


}
Expand All @@ -128,15 +130,14 @@ public void remove(String group, String key) {
return;
}

DynamicTTLCache<String, Object> 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;
}
Expand All @@ -150,8 +151,7 @@ public void removeAll() {

@Override
public Set<String> getKeys(String group) {
DynamicTTLCache<String, Object> cache = getCache(group);
return cache.asMap().keySet();
return getCache(group).asMap().keySet();
}

@Override
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -226,9 +226,7 @@ public void shutdown() {
isInitialized.set(false);
}

private String cacheKey(String group, String key) {
return (group + ":" + key).toLowerCase();
}


private DynamicTTLCache<String, Object> getCache(String cacheNameIn) {
if (cacheNameIn == null) {
Expand Down

0 comments on commit 8d217b8

Please sign in to comment.