grsf-publisher-ws/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/cache/CacheImpl.java

54 lines
1.3 KiB
Java

package org.gcube.data_catalogue.grsf_publish_ws.utils.cache;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.LoggerFactory;
/**
* Cache implementation.
* @author Costantino Perciante at ISTI-CNR
* (costantino.perciante@isti.cnr.it)
*/
public class CacheImpl implements CacheInterface<String, Long> {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CacheImpl.class);
/**
* The hashmap
*/
private Map<String, CacheValueBean<Long>> userSpaceMap;
/**
* Cache entry expires after EXPIRED_AFTER ms
*/
private long ttl;
public CacheImpl(long timeout){
ttl = timeout;
userSpaceMap = new ConcurrentHashMap<>();
}
@Override
public Long get(String key) {
if(userSpaceMap.containsKey(key)){
CacheValueBean<Long> bean = userSpaceMap.get(key);
if(CacheUtilities.expired(bean.getTTL(), ttl)){
userSpaceMap.remove(key);
logger.debug("Amount of space in the infrastructure used expired for key " + key + ", returning null");
}
else
return bean.getValue();
}
return null;
}
@Override
public boolean insert(String key, Long value) {
CacheValueBean<Long> newBean = new CacheValueBean<Long>(value, System.currentTimeMillis());
userSpaceMap.put(key, newBean);
return true;
}
}