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 19, 2024
1 parent c52ee8b commit f40ac88
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ private List<CacheProvider> getProvidersForRegion ( String group ) {
}

List<CacheProvider> initProviders(List<CacheProvider> 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;
}

Expand Down
4 changes: 2 additions & 2 deletions dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* This class constructs a cache with dynamic TTLs for each key.
* Relies on Caffeine for the underlying cache implementation.
* @param <K>
* @param <Object>
* @param <V>
*/
public class DynamicTTLCache<K, Object> {
public class DynamicTTLCache<K, V> {

public final long defaultTTLInMillis;
private final Cache<K, CacheValue> cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
/**
* In-Memory Cache implementation using https://github.com/ben-manes/caffeine
* <p>
* 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.
* <p>
* A group-wide invalidation time can also be set by config properties.
* <p>
Expand All @@ -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 {

Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions dotCMS/src/test/java/com/dotcms/cache/DynamicTTLCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}




}

0 comments on commit f40ac88

Please sign in to comment.