From 47486ca8a8819f0d333faa75b5ebda8cd25180c3 Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Wed, 12 Jun 2024 17:14:00 +0200 Subject: [PATCH] cache in folder esterna, gestisco errore in retriew --- .../user/cloudcomputing/CC_Portlet.java | 2 +- .../user/cloudcomputing/is/CacheEntry.java | 16 ++++++++++ .../user/cloudcomputing/is/CacheIsClient.java | 30 ++++++++----------- .../user/cloudcomputing/is/CacheIsClient.txt | 9 ------ 4 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheEntry.java diff --git a/src/main/java/org/gcube/portlets/user/cloudcomputing/CC_Portlet.java b/src/main/java/org/gcube/portlets/user/cloudcomputing/CC_Portlet.java index 2b694c0..15dde95 100644 --- a/src/main/java/org/gcube/portlets/user/cloudcomputing/CC_Portlet.java +++ b/src/main/java/org/gcube/portlets/user/cloudcomputing/CC_Portlet.java @@ -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; } } diff --git a/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheEntry.java b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheEntry.java new file mode 100644 index 0000000..7967ec6 --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheEntry.java @@ -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 endpoints; + long timestamp; + + CacheEntry(List endpoints, long timestamp) { + this.endpoints = endpoints; + this.timestamp = timestamp; + } +} diff --git a/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.java b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.java index 0112e60..17abfbc 100644 --- a/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.java +++ b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.java @@ -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 endpoints; - long timestamp; - - CacheEntry(List endpoints, long timestamp) { - this.endpoints = endpoints; - this.timestamp = timestamp; - } - } - // Define the cache name private static final String CACHE_NAME = "my-cache"; private PortalCache 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 getEndpointsFromISWithCache(String resource_name, String category) throws ServerException { + public synchronized List 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 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); } - } diff --git a/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.txt b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.txt index e1e6a60..b25f07c 100644 --- a/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.txt +++ b/src/main/java/org/gcube/portlets/user/cloudcomputing/is/CacheIsClient.txt @@ -31,15 +31,6 @@ public class CacheIsClient extends IsClient { return _singleInstance; } - private static class CacheEntry { - List endpoints; - long timestamp; - - CacheEntry(List endpoints, long timestamp) { - this.endpoints = endpoints; - this.timestamp = timestamp; - } - } // Mappa per mantenere la cache private ConcurrentHashMap cache = new ConcurrentHashMap<>();