Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 30670 per object ttl cache #30692

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions dotCMS/src/main/java/com/dotcms/cache/CacheValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dotcms.cache;

import java.io.Serializable;

public class CacheValue implements Serializable {

public final Object value;
public final long ttlInMillis;

public CacheValue(Object value, long ttlInMillis) {
this.value = value;
this.ttlInMillis = ttlInMillis <= 0 ? Long.MAX_VALUE : ttlInMillis;
}

public CacheValue(Object value) {
this.value = value;
this.ttlInMillis = Long.MAX_VALUE;
}
}
96 changes: 96 additions & 0 deletions dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.dotcms.cache;


import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.validation.constraints.NotNull;


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

public final long defaultTTLInMillis;
private final Cache<K, CacheValue> cache;

public DynamicTTLCache(long maxCapacity) {
this(maxCapacity, Long.MAX_VALUE);
wezell marked this conversation as resolved.
Show resolved Hide resolved
}


public DynamicTTLCache(long maxCapacity, long defaultTTLInMillis) {
this.defaultTTLInMillis = defaultTTLInMillis;
this.cache = Caffeine.newBuilder()
.initialCapacity((int) maxCapacity)
.expireAfter(new Expiry<K, CacheValue>() {
@Override
public long expireAfterCreate(@NotNull K key, @NotNull CacheValue value, long currentTime) {
return TimeUnit.MILLISECONDS.toNanos(value.ttlInMillis);
}

@Override
public long expireAfterUpdate(K key, CacheValue value, long currentTime,
long currentDuration) {
return currentDuration;
}

@Override
public long expireAfterRead(K key, CacheValue value, long currentTime, long currentDuration) {
return currentDuration;
}
})
.maximumSize(maxCapacity)
.build();
}

public void put(K key, V value, long ttlInMillis) {

if(value instanceof CacheValue) {
cache.put(key, (CacheValue) value);
}else{
cache.put(key, new CacheValue(value, ttlInMillis ));
}

}

public void put(K key, V value) {
this.put(key, value, defaultTTLInMillis);
}

public V getIfPresent(K key) {
CacheValue cacheValue = cache.getIfPresent(key);

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


public void invalidate(K key) {
cache.invalidate(key);
}

public void invalidateAll() {

cache.invalidateAll();
}

public CacheStats stats() {
return cache.stats();
}

public long estimatedSize() {
return cache.estimatedSize();
}

public Map<K, CacheValue> asMap() {
return cache.asMap();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotmarketing.business.cache.provider;

import com.dotmarketing.util.Logger;
import java.io.Serializable;
import java.util.Set;

Expand Down Expand Up @@ -67,6 +68,11 @@ public abstract class CacheProvider implements Serializable {
*/
public abstract boolean isInitialized () throws Exception;

public void put(String group, String key, Object content, long ttlMillis){
Logger.warn(this.getClass(), "This cache implementation does not support per object TTL, ignoring it.");
put(group, key, content);
}

/**
* Adds the given content to the given region and for the given key
*
Expand Down Expand Up @@ -132,4 +138,4 @@ public abstract class CacheProvider implements Serializable {
*/
public abstract void shutdown ();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public class CacheStats {

public CacheStats(){}

public void addStat(String statName, String value) {
public CacheStats addStat(String statName, String value) {
stats.put(statName, value);
return this;
}

public void addStat(String statName, long value) {
public CacheStats addStat(String statName, long value) {
stats.put(statName, value+"");
return this;
}

public Set<String> getStatColumns() {
Expand All @@ -45,4 +47,4 @@ public String getStatValue(String columnName) {



}
}
Loading
Loading