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 20, 2024
1 parent be4def4 commit 64443d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,33 @@ private List<String> getProviderNamesPerRegion(String group){
* memory the initialized providers in order to impact as much as possible performance, keep in mind this class
* is heavily used!.
*
* It turns out that using Map.computeIfAbsent in a ConcurrentHashMap throws a recursion exception, so we need to
* do it old school
*
* @param group
* @return
*/
private List<CacheProvider> getProvidersForRegion ( String group ) {

//The case is very simple here, no license no chance to modify any region chain

return configuredChainsPerRegion.computeIfAbsent(group, k -> {
List<CacheProvider> providers = new ArrayList<>();
for(String providerClassName : getProviderNamesPerRegion(group)){
getInstanceFor(providerClassName).ifPresent(providers::add);
List<CacheProvider> caches = configuredChainsPerRegion.get(group);
if(caches != null){
return caches;
}

synchronized (CacheProviderAPIImpl.class) {
caches = configuredChainsPerRegion.get(group);
if(caches != null){
return caches;
}
return List.copyOf(initProviders(providers));
});
List<CacheProvider> providers = new ArrayList<>();
getProviderNamesPerRegion(group).forEach(c->getInstanceFor(c).ifPresent(providers::add));
configuredChainsPerRegion.put(group,List.copyOf(initProviders(providers)));
return configuredChainsPerRegion.get(group);
}



}

Expand Down Expand Up @@ -259,6 +272,10 @@ public void put ( String group, String key, Object content ) {
try {
provider.put(group, key, content);
} catch ( Exception e ) {

e.printStackTrace();


//On Error we don't want to stop the execution of the other providers
Logger.error(this.getClass(), "Error adding record to CacheProvider [" + provider.getName() + "]: group [" + group + "] - key [" + key + "].", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean isInitialized() throws Exception {

@Override
public void put(String group, String key, Object content, long ttlMillis) {
if (group == null || key == null || content == null) {
if ( key == null || content == null) {
return;
}
DynamicTTLCache<String, Object> cache = getCache(group);
Expand All @@ -86,32 +86,22 @@ public void put(String group, String key, Object content, long ttlMillis) {

@Override
public void put(String group, String key, Object content) {
if (group == null || key == null || content == null) {
return;
}

put(group, key, content, Long.MAX_VALUE);

}

@Override
public Object get(String group, String key) {
if (group == null || key == null) {
return null;
}
return getCache(group).getIfPresent(key.toLowerCase());
return getCache(group).getIfPresent(key);


}

@Override
public void remove(String group, String key) {
if (group == null || key == null) {
return;
}

// Invalidates from Cache a key from a given group
getCache(group).invalidate(key.toLowerCase());
getCache(group).invalidate(key);
}

@Override
Expand Down Expand Up @@ -216,13 +206,28 @@ public void shutdown() {
}



private DynamicTTLCache<String, Object> getCache(String cacheNameIn) {
if (cacheNameIn == null) {
/**
* Tried to make this nice and functional, but unfortunately ConcurrentHashMap.computeIfAbsent throws a recursion
* error if when computing the group value. See: <a href="https://bugs.openjdk.java.net/browse/JDK-8062841">...</a> or
* <a href="https://stackoverflow.com/questions/54824656/since-java-9-hashmap-computeifabsent-throws-concurrentmodificationexception-on">...</a>
*
* @param cacheName
* @return
*/
private DynamicTTLCache<String, Object> getCache(String cacheName) {
if (cacheName == null) {
throw new DotStateException("Null cache region passed in");
}
final String cacheName = cacheNameIn.toLowerCase();
return groups.computeIfAbsent(cacheName, k -> {
DynamicTTLCache<String, Object> cache = groups.get(cacheName);

if (cache != null) {
return cache;
}
synchronized (groups) {
cache = groups.get(cacheName);
if (cache != null) {
return cache;
}

final int maxSize = getMaxSize(cacheName);
final long ttlSeconds = getTTLMillis(cacheName);
Expand All @@ -232,9 +237,11 @@ private DynamicTTLCache<String, Object> getCache(String cacheNameIn) {
+ ",Concurrency:"
+ Config.getIntProperty("cache.concurrencylevel", 32), 60000);

return new DynamicTTLCache<>(maxSize, ttlSeconds);
groups.put(cacheName,new DynamicTTLCache<>(maxSize, ttlSeconds));
return groups.get(cacheName);

}

});


}
Expand Down

0 comments on commit 64443d1

Please sign in to comment.