Fixed user cache

This commit is contained in:
Luca Frosini 2022-07-13 11:59:45 +02:00
parent cff70a3545
commit 24ad5b9a1f
1 changed files with 3 additions and 10 deletions

View File

@ -1,7 +1,5 @@
package org.gcube.gcat.persistence.ckan; package org.gcube.gcat.persistence.ckan;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.cache.Cache; import javax.cache.Cache;
@ -24,8 +22,6 @@ public abstract class CKANUserCache {
private static final MutableConfiguration<String,CKANUser> userCacheConfiguration; private static final MutableConfiguration<String,CKANUser> userCacheConfiguration;
private static final Map<String,Cache<String,CKANUser>> userCachePerContext;
static { static {
CachingProvider provider = Caching.getCachingProvider(); CachingProvider provider = Caching.getCachingProvider();
cacheManager = provider.getCacheManager(); cacheManager = provider.getCacheManager();
@ -33,8 +29,6 @@ public abstract class CKANUserCache {
userCacheConfiguration = new MutableConfiguration<String,CKANUser>().setTypes(String.class, CKANUser.class) userCacheConfiguration = new MutableConfiguration<String,CKANUser>().setTypes(String.class, CKANUser.class)
.setStoreByValue(false) .setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15))); .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
userCachePerContext = new HashMap<String,Cache<String,CKANUser>>();
} }
private CKANUserCache() { private CKANUserCache() {
@ -43,10 +37,9 @@ public abstract class CKANUserCache {
public synchronized static CKANUser getCurrrentCKANUser() { public synchronized static CKANUser getCurrrentCKANUser() {
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
String context = secretManager.getContext(); String context = secretManager.getContext();
Cache<String,CKANUser> userCache = userCachePerContext.get(context); Cache<String,CKANUser> userCache = cacheManager.getCache(context);
if(userCache == null) { if(userCache == null) {
userCache = cacheManager.createCache(context, userCacheConfiguration); userCache = cacheManager.createCache(context, userCacheConfiguration);
userCachePerContext.put(context, userCache);
} }
String gcubeUsername = secretManager.getUser().getUsername(); String gcubeUsername = secretManager.getUser().getUsername();
@ -68,7 +61,7 @@ public abstract class CKANUserCache {
public synchronized static void removeUserFromCache(String gcubeUsername) { public synchronized static void removeUserFromCache(String gcubeUsername) {
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
String context = secretManager.getContext(); String context = secretManager.getContext();
Cache<String,CKANUser> userCache = userCachePerContext.get(context); Cache<String,CKANUser> userCache = cacheManager.getCache(context);
if(userCache != null) { if(userCache != null) {
userCache.remove(gcubeUsername); userCache.remove(gcubeUsername);
} }
@ -77,7 +70,7 @@ public abstract class CKANUserCache {
public synchronized static void emptyUserCache() { public synchronized static void emptyUserCache() {
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
String context = secretManager.getContext(); String context = secretManager.getContext();
userCachePerContext.remove(context); cacheManager.destroyCache(context);
} }
@Override @Override