-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache) per object ttl for cache items. This adds TTL capibilites…
… to our CaffineCache and really deprecates the timed cache provider. ref:#30670
- Loading branch information
Showing
10 changed files
with
428 additions
and
714 deletions.
There are no files selected for viewing
344 changes: 87 additions & 257 deletions
344
dotCMS/src/enterprise/java/com/dotcms/enterprise/cache/provider/CacheProviderAPIImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
dotCMS/src/main/java/com/dotcms/cache/DynamicTTLCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.