From 31746638f0a7d99093f9348e14436c3b7aea28db Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 18 Dec 2012 17:02:17 +0100 Subject: Added optional WeakCache to hold fewer ClusterStorage objects in memory. Enabled with Storage.useWeakCache property. Useful for large imports and other operations that involve accessing a large number of items over a short period. --- .../persistency/ClusterStorageManager.java | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/c2kernel/persistency/ClusterStorageManager.java') diff --git a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java index 70fb46d..c49b41f 100644 --- a/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java +++ b/src/main/java/com/c2kernel/persistency/ClusterStorageManager.java @@ -16,7 +16,9 @@ import com.c2kernel.persistency.outcome.Outcome; import com.c2kernel.persistency.outcome.Viewpoint; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Logger; +import com.c2kernel.utils.NonStrongRefCache; import com.c2kernel.utils.SoftCache; +import com.c2kernel.utils.WeakCache; /** * instantiates ClusterStorages listed in properties file All read/write requests to storage pass through this object, which @@ -31,7 +33,7 @@ public class ClusterStorageManager { HashMap> clusterWriters = new HashMap>(); HashMap> clusterReaders = new HashMap>(); // we don't need a soft cache for the top level cache - the proxies and entities clear that when reaped - HashMap> memoryCache = new HashMap>(); + HashMap> memoryCache = new HashMap>(); /** * Initialises all ClusterStorage handlers listed by class name in the property "ClusterStorages" @@ -159,7 +161,7 @@ public class ClusterStorageManager { public C2KLocalObject get(Integer sysKeyIntObj, String path) throws ClusterStorageException, ObjectNotFoundException { C2KLocalObject result = null; // check cache first - SoftCache sysKeyMemCache = null; + NonStrongRefCache sysKeyMemCache = null; if (memoryCache.containsKey(sysKeyIntObj)) { sysKeyMemCache = memoryCache.get(sysKeyIntObj); synchronized(sysKeyMemCache) { @@ -208,7 +210,9 @@ public class ClusterStorageManager { if (result != null) { // got it! // store it in the cache if (sysKeyMemCache == null) { // create cache if needed - sysKeyMemCache = new SoftCache(0); + boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true"); + Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj); + sysKeyMemCache = useWeak?new WeakCache():new SoftCache(0); synchronized (memoryCache) { memoryCache.put(sysKeyIntObj, sysKeyMemCache); } @@ -242,11 +246,13 @@ public class ClusterStorageManager { } } // put in mem cache if that worked - SoftCache sysKeyMemCache; + NonStrongRefCache sysKeyMemCache; if (memoryCache.containsKey(sysKeyIntObj)) sysKeyMemCache = memoryCache.get(sysKeyIntObj); else { - sysKeyMemCache = new SoftCache(); + boolean useWeak = Gateway.getProperty("Storage.useWeakCache","false").equals("true"); + Logger.msg(7,"ClusterStorageManager.put() - Creating "+(useWeak?"Weak":"Strong")+" cache for entity "+sysKeyIntObj); + sysKeyMemCache = useWeak?new WeakCache():new SoftCache(0); synchronized (memoryCache) { memoryCache.put(sysKeyIntObj, sysKeyMemCache); } @@ -277,7 +283,7 @@ public class ClusterStorageManager { } if (memoryCache.containsKey(sysKeyIntObj)) { - SoftCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); + NonStrongRefCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); synchronized (sysKeyMemCache) { sysKeyMemCache.remove(path); } @@ -292,7 +298,7 @@ public class ClusterStorageManager { Logger.msg(7, "CSM.clearCache() - removing "+sysKeyIntObj+"/"+path); if (memoryCache.containsKey(sysKeyIntObj)) { - SoftCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); + NonStrongRefCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); synchronized(sysKeyMemCache) { for (Iterator iter = sysKeyMemCache.keySet().iterator(); iter.hasNext();) { String thisPath = iter.next(); @@ -312,7 +318,7 @@ public class ClusterStorageManager { if (memoryCache.containsKey(sysKeyIntObj)) { synchronized (memoryCache) { if (Logger.doLog(6)) { - SoftCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); + NonStrongRefCache sysKeyMemCache = memoryCache.get(sysKeyIntObj); int size = sysKeyMemCache.size(); Logger.msg(6, "CSM.clearCache() - "+size+" objects to remove."); } @@ -335,7 +341,7 @@ public class ClusterStorageManager { synchronized(memoryCache) { for (Integer sysKey : memoryCache.keySet()) { Logger.msg(logLevel, "Cached Objects of Entity "+sysKey); - SoftCache sysKeyMemCache = memoryCache.get(sysKey); + NonStrongRefCache sysKeyMemCache = memoryCache.get(sysKey); try { synchronized(sysKeyMemCache) { for (Object name : sysKeyMemCache.keySet()) { -- cgit v1.2.3