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.\n\nref:#30670
  • Loading branch information
wezell committed Nov 18, 2024
1 parent e7a1b2e commit c52ee8b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ 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));
});
Expand Down
13 changes: 11 additions & 2 deletions dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import java.util.concurrent.TimeUnit;
import javax.validation.constraints.NotNull;

public class DynamicTTLCache<K, V> {

/**
* This class constructs a cache with dynamic TTLs for each key.
* Relies on Caffeine for the underlying cache implementation.
* @param <K>
* @param <Object>
*/
public class DynamicTTLCache<K, Object> {

public final long defaultTTLInMillis;
private final Cache<K, CacheValue> cache;
Expand Down Expand Up @@ -62,9 +69,10 @@ public void put(K key, Object value) {
public Object getIfPresent(K key) {
CacheValue cacheValue = cache.getIfPresent(key);

return cacheValue != null ? cacheValue.value : null;
return cacheValue != null ? (Object) cacheValue.value : null;
}


public void invalidate(K key) {
cache.invalidate(key);
}
Expand All @@ -85,4 +93,5 @@ public long estimatedSize() {
public Map<K, CacheValue> asMap() {
return cache.asMap();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class CaffineCache extends CacheProvider {




private static final String CACHE_PREFIX = "cache.";
private static final String SIZE_POSTFIX = ".size";
private static final String SECONDS_POSTFIX = ".seconds";
private static final String DEFAULT_CACHE = CacheProviderAPI.DEFAULT_CACHE;
private static final long serialVersionUID = 1L;
private static final ConcurrentHashMap<String, DynamicTTLCache<String, Object>> groups = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -68,24 +72,6 @@ public void init() {
private boolean doInit() {
groups.clear();
Logger.info(this.getClass(), "===== Initializing [" + getName() + "].");

Iterator<String> it = Config.getKeys();
while (it.hasNext()) {
String key = it.next();
if (key == null) {
continue;
}
if (key.startsWith("cache.")) {
String cacheName = key.split("\\.")[1];
if (key.endsWith(".size")) {
int inMemory = Config.getIntProperty(key, 0);

Logger.info(this.getClass(), "***\t Cache Config Memory : "
+ cacheName + ": " + inMemory);
}
}
}

return true;
}

Expand Down Expand Up @@ -161,16 +147,16 @@ public Set<String> getGroups() {

private long getTTLMillis(String group) {

long seconds = Config.getLongProperty("cache." + group + ".seconds",
Config.getLongProperty("cache." + DEFAULT_CACHE + ".seconds", -1));
long seconds = Config.getLongProperty(CACHE_PREFIX + group + SECONDS_POSTFIX,
Config.getLongProperty(CACHE_PREFIX + DEFAULT_CACHE + SECONDS_POSTFIX, -1));

return seconds < 0 ? Long.MAX_VALUE : seconds * 1000;
}

private int getMaxSize(String group) {

return Config.getIntProperty("cache." + group + ".size",
Config.getIntProperty("cache." + DEFAULT_CACHE + ".size", 1000));
return Config.getIntProperty(CACHE_PREFIX + group + SIZE_POSTFIX,
Config.getIntProperty(CACHE_PREFIX + DEFAULT_CACHE + SIZE_POSTFIX, 1000));

}

Expand Down Expand Up @@ -243,7 +229,7 @@ private DynamicTTLCache<String, Object> getCache(String cacheNameIn) {
+ ",Concurrency:"
+ Config.getIntProperty("cache.concurrencylevel", 32), 60000);

return new DynamicTTLCache<String, Object>(maxSize, ttlSeconds);
return new DynamicTTLCache<>(maxSize, ttlSeconds);

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
import com.dotmarketing.util.Logger;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

@Deprecated
/**
* Provides a timed cache implementation using Caffeine.
* @deprecated
* This class is deprecated and will be removed in a future version - use CaffineCache instead as it
* provides a more robust and efficient implementation.
* <p> Use {@link com.dotmarketing.business.cache.provider.caffine.CaffineCache} instead.
*/
@Deprecated(since = "24.12", forRemoval = false)
public class TimedCacheProvider extends CacheProvider {


Expand Down

0 comments on commit c52ee8b

Please sign in to comment.