minor fixes for the caches

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@140058 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-16 18:00:54 +00:00
parent 5dfa58909a
commit ceca8a5497
3 changed files with 20 additions and 10 deletions

View File

@ -58,8 +58,8 @@ public abstract class HelperMethods {
private static final String PATH_SEPARATOR = "/"; private static final String PATH_SEPARATOR = "/";
// caches // caches
private static CacheInterface userEmailCache = new CacheImpl(1000 * 60 * 30); private static CacheInterface<String, String> userEmailCache = new CacheImpl<String, String>(1000 * 60 * 30);
private static CacheInterface userFullnameCache = new CacheImpl(1000 * 60 * 30); private static CacheInterface<String, String> userFullnameCache = new CacheImpl<String, String>(1000 * 60 * 30);
/** /**
* Convert a group name to its id on ckan * Convert a group name to its id on ckan
@ -132,7 +132,6 @@ public abstract class HelperMethods {
* @return * @return
* @throws Exception * @throws Exception
*/ */
@SuppressWarnings("unchecked")
public static String getUserEmail(String context, String token){ public static String getUserEmail(String context, String token){
// check in cache // check in cache
@ -156,7 +155,6 @@ public abstract class HelperMethods {
* @return * @return
* @throws Exception * @throws Exception
*/ */
@SuppressWarnings("unchecked")
public static String getUserFullname(String context, String token){ public static String getUserFullname(String context, String token){
// check in cache // check in cache

View File

@ -10,14 +10,14 @@ import org.slf4j.LoggerFactory;
* @author Costantino Perciante at ISTI-CNR * @author Costantino Perciante at ISTI-CNR
* (costantino.perciante@isti.cnr.it) * (costantino.perciante@isti.cnr.it)
*/ */
public class CacheImpl implements CacheInterface<String, Long> { public class CacheImpl <K, V> implements CacheInterface<K, V> {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheImpl.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheImpl.class);
/** /**
* The hashmap * The hashmap
*/ */
private Map<String, CacheValueBean<Long>> userSpaceMap; private Map<K, CacheValueBean<V>> userSpaceMap;
/** /**
* Cache entry expires after EXPIRED_AFTER ms * Cache entry expires after EXPIRED_AFTER ms
@ -30,10 +30,10 @@ public class CacheImpl implements CacheInterface<String, Long> {
} }
@Override @Override
public Long get(String key) { public V get(K key) {
if(userSpaceMap.containsKey(key)){ if(userSpaceMap.containsKey(key)){
CacheValueBean<Long> bean = userSpaceMap.get(key); CacheValueBean<V> bean = userSpaceMap.get(key);
if(CacheUtilities.expired(bean.getTTL(), ttl)){ if(CacheUtilities.expired(bean.getTTL(), ttl)){
userSpaceMap.remove(key); userSpaceMap.remove(key);
logger.debug("Amount of space in the infrastructure used expired for key " + key + ", returning null"); logger.debug("Amount of space in the infrastructure used expired for key " + key + ", returning null");
@ -45,8 +45,8 @@ public class CacheImpl implements CacheInterface<String, Long> {
} }
@Override @Override
public boolean insert(String key, Long value) { public boolean insert(K key, V obj) {
CacheValueBean<Long> newBean = new CacheValueBean<Long>(value, System.currentTimeMillis()); CacheValueBean<V> newBean = new CacheValueBean<V>(obj, System.currentTimeMillis());
userSpaceMap.put(key, newBean); userSpaceMap.put(key, newBean);
return true; return true;
} }

View File

@ -42,6 +42,7 @@ import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread; import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory; import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -516,4 +517,15 @@ public class JTests {
} }
//@Test
public void testCaches(){
String context = "/gcube/devNext/NextNext";
String token = "";
for (int i = 0; i < 1000; i++) {
logger.debug(HelperMethods.getUserEmail(context, token));
}
}
} }