Fixed user cache

This commit is contained in:
Luca Frosini 2022-05-27 17:25:36 +02:00
parent 2ed6c66285
commit bceb4acd74
3 changed files with 13 additions and 5 deletions

View File

@ -72,6 +72,7 @@
<dependency> <dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId> <artifactId>authorization-utils</artifactId>
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>

View File

@ -56,7 +56,7 @@ public class CatalogueConfigurationFactory {
// The supported organizations could be changed we need to empty the user cache for the context // The supported organizations could be changed we need to empty the user cache for the context
// to avoid to miss to add an user in an organization which has been added. // to avoid to miss to add an user in an organization which has been added.
CKANUserCache.emptyUsersCache(); CKANUserCache.emptyUserCache();
return catalogueConfiguration; return catalogueConfiguration;
} }

View File

@ -1,5 +1,7 @@
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;
@ -22,6 +24,8 @@ 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();
@ -29,6 +33,8 @@ 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() {
@ -37,9 +43,10 @@ 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 = cacheManager.getCache(context); Cache<String,CKANUser> userCache = userCachePerContext.get(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();
@ -61,16 +68,16 @@ 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 = cacheManager.getCache(context); Cache<String,CKANUser> userCache = userCachePerContext.get(context);
if(userCache != null) { if(userCache != null) {
userCache.remove(gcubeUsername); userCache.remove(gcubeUsername);
} }
} }
public synchronized static void emptyUsersCache() { public synchronized static void emptyUserCache() {
SecretManager secretManager = SecretManagerProvider.instance.get(); SecretManager secretManager = SecretManagerProvider.instance.get();
String context = secretManager.getContext(); String context = secretManager.getContext();
cacheManager.destroyCache(context); userCachePerContext.remove(context);
} }
@Override @Override