cache in folder esterna, gestisco errore in retriew

This commit is contained in:
Alfredo Oliviero 2024-06-12 17:14:00 +02:00
parent c0f34e3df8
commit 47486ca8a8
4 changed files with 29 additions and 28 deletions

View File

@ -77,7 +77,7 @@ public class CC_Portlet extends MVCPortlet {
configuration = config;
} catch (Exception e) {
e.printStackTrace();
_log.error(e);
_log.error("cannot obtain configuration", e);
return null;
}
}

View File

@ -0,0 +1,16 @@
package org.gcube.portlets.user.cloudcomputing.is;
import java.io.Serializable;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
public class CacheEntry implements Serializable {
List<ServiceEndpoint> endpoints;
long timestamp;
CacheEntry(List<ServiceEndpoint> endpoints, long timestamp) {
this.endpoints = endpoints;
this.timestamp = timestamp;
}
}

View File

@ -1,6 +1,5 @@
package org.gcube.portlets.user.cloudcomputing.is;
import java.io.Serializable;
import java.rmi.ServerException;
import java.util.List;
@ -24,35 +23,25 @@ public class CacheIsClient extends IsClient {
// Class to represent a cache entry
static CacheIsClient getSingleton() {
static CacheIsClient getSingleton() {
if (_singleInstance == null) {
_singleInstance = new CacheIsClient();
}
return _singleInstance;
}
private static class CacheEntry implements Serializable {
List<ServiceEndpoint> endpoints;
long timestamp;
CacheEntry(List<ServiceEndpoint> endpoints, long timestamp) {
this.endpoints = endpoints;
this.timestamp = timestamp;
}
}
// Define the cache name
private static final String CACHE_NAME = "my-cache";
private PortalCache<Serializable, Serializable> portalCache = MultiVMPoolUtil.getCache(CACHE_NAME);
// Method to get the cache key
private String getCacheKey(String resourceName, String category) {
return resourceName + ":" + category;
return "IS_"+ resourceName + ":" + category;
}
// Main method with caching and locking
public synchronized List<ServiceEndpoint> getEndpointsFromISWithCache(String resource_name, String category) throws ServerException {
public synchronized List<ServiceEndpoint> getEndpointsFromISWithCache(String resource_name, String category)
throws ServerException {
String key = getCacheKey(resource_name, category);
long currentTime = System.currentTimeMillis();
@ -60,9 +49,14 @@ public class CacheIsClient extends IsClient {
synchronized (key.intern()) {
logger.info("Acquired lock for key: " + key);
CacheEntry cacheEntry = null;
// Check if the entry is present in the cache and is valid
CacheEntry cacheEntry = (CacheEntry)portalCache.get(key);
try {
cacheEntry = (CacheEntry) portalCache.get(key);
} catch (ClassCastException e) {
portalCache.remove(key);
}
if (cacheEntry != null) {
if (currentTime - cacheEntry.timestamp <= TimeUnit.MINUTES.toMillis(10)) {
// If the cache value is still valid, return it
@ -92,6 +86,7 @@ public class CacheIsClient extends IsClient {
return endpoints;
}
}
// Metodo principale con caching e locking
public List<ServiceEndpoint> getEndpointsFromIS(String resource_name, String category) throws ServerException {
logger.info("@@@ cached getEndpointsFromIS: " + resource_name + ":" + category);
@ -99,5 +94,4 @@ public class CacheIsClient extends IsClient {
return getEndpointsFromISWithCache(resource_name, category);
}
}

View File

@ -31,15 +31,6 @@ public class CacheIsClient extends IsClient {
return _singleInstance;
}
private static class CacheEntry {
List<ServiceEndpoint> endpoints;
long timestamp;
CacheEntry(List<ServiceEndpoint> endpoints, long timestamp) {
this.endpoints = endpoints;
this.timestamp = timestamp;
}
}
// Mappa per mantenere la cache
private ConcurrentHashMap<String, CacheEntry> cache = new ConcurrentHashMap<>();