diff --git a/org.adempiere.base/src/org/compiere/model/MImage.java b/org.adempiere.base/src/org/compiere/model/MImage.java index d00523ec44..2f52c87d36 100644 --- a/org.adempiere.base/src/org/compiere/model/MImage.java +++ b/org.adempiere.base/src/org/compiere/model/MImage.java @@ -71,7 +71,7 @@ public class MImage extends X_AD_Image } // get /** Cache */ - private static CCache s_cache = new CCache(Table_Name, 20); + private static CCache s_cache = new CCache(Table_Name, 20, 10); /** * Constructor diff --git a/org.adempiere.base/src/org/compiere/model/MPriceList.java b/org.adempiere.base/src/org/compiere/model/MPriceList.java index 4d312dbc39..f569d2b6f9 100644 --- a/org.adempiere.base/src/org/compiere/model/MPriceList.java +++ b/org.adempiere.base/src/org/compiere/model/MPriceList.java @@ -172,7 +172,7 @@ public class MPriceList extends X_M_PriceList } // getPricePrecision /** Cache of Price Lists */ - private static CCache s_cache = new CCache(Table_Name, 5); + private static CCache s_cache = new CCache(Table_Name, 5, 5); /************************************************************************** diff --git a/org.adempiere.base/src/org/compiere/print/layout/ImageElement.java b/org.adempiere.base/src/org/compiere/print/layout/ImageElement.java index 6186cd4e02..170b01cb0c 100644 --- a/org.adempiere.base/src/org/compiere/print/layout/ImageElement.java +++ b/org.adempiere.base/src/org/compiere/print/layout/ImageElement.java @@ -124,9 +124,9 @@ public class ImageElement extends PrintElement return new ImageElement(image.getImage()); } // get - /** 60 minute Cache */ + /** 10 minute Cache */ private static CCache s_cache - = new CCache(null, "ImageElement", 10, 60, false); + = new CCache(null, "ImageElement", 10, 10, false); /************************************************************************** * Create from existing Image diff --git a/org.adempiere.base/src/org/compiere/util/CCache.java b/org.adempiere.base/src/org/compiere/util/CCache.java index 5fa06758d7..dc176830fb 100644 --- a/org.adempiere.base/src/org/compiere/util/CCache.java +++ b/org.adempiere.base/src/org/compiere/util/CCache.java @@ -52,6 +52,8 @@ public class CCache implements CacheInterface, Map, Serializable @SuppressWarnings("unused") private boolean m_distributed; + private int m_maxSize = 0; + public CCache (String name, int initialCapacity) { this(name, name, initialCapacity); @@ -67,6 +69,11 @@ public class CCache implements CacheInterface, Map, Serializable this(name, name, initialCapacity, expireMinutes, distributed); } + public CCache (String name, int initialCapacity, int expireMinutes, boolean distributed, int maxSize) + { + this(name, name, initialCapacity, expireMinutes, distributed, maxSize); + } + /** * Adempiere Cache - expires after 2 hours * @param name (table) name of the cache @@ -79,20 +86,28 @@ public class CCache implements CacheInterface, Map, Serializable public CCache (String tableName, String name, int initialCapacity, boolean distributed) { - this (tableName, name, initialCapacity, 120, distributed); + this (tableName, name, initialCapacity, 60, distributed); } + public CCache (String tableName, String name, int initialCapacity, int expireMinutes, boolean distributed) + { + this(tableName, name, initialCapacity, expireMinutes, distributed, CacheMgt.MAX_SIZE); + } + /** * Adempiere Cache * @param name (table) name of the cache * @param initialCapacity initial capacity * @param expireMinutes expire after minutes (0=no expire) + * @param distributed + * @param maxSize ignore if distributed=true */ - public CCache (String tableName, String name, int initialCapacity, int expireMinutes, boolean distributed) + public CCache (String tableName, String name, int initialCapacity, int expireMinutes, boolean distributed, int maxSize) { m_name = name; m_tableName = tableName; setExpireMinutes(expireMinutes); + m_maxSize = maxSize; cache = CacheMgt.get().register(this, distributed); m_distributed = distributed; if (distributed) { @@ -408,4 +423,8 @@ public class CCache implements CacheInterface, Map, Serializable @Override public void newRecord(int record_ID) { } + + public int getMaxSize() { + return m_maxSize; + } } // CCache diff --git a/org.adempiere.base/src/org/compiere/util/CacheMgt.java b/org.adempiere.base/src/org/compiere/util/CacheMgt.java index 9947048176..0d7031b692 100644 --- a/org.adempiere.base/src/org/compiere/util/CacheMgt.java +++ b/org.adempiere.base/src/org/compiere/util/CacheMgt.java @@ -21,7 +21,6 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.logging.Level; @@ -68,7 +67,7 @@ public class CacheMgt /** Logger */ private static CLogger log = CLogger.getCLogger(CacheMgt.class); - private static int MAX_SIZE = 1000; + public static int MAX_SIZE = 1000; static { try @@ -114,18 +113,7 @@ public class CacheMgt if (map == null) { - map = Collections.synchronizedMap(new LinkedHashMap() { - /** - * generated serial id - */ - private static final long serialVersionUID = -9111152673370957054L; - - @Override - protected boolean removeEldestEntry(Entry eldest) { - return size() > MAX_SIZE; - } - - }); + map = Collections.synchronizedMap(new MaxSizeHashMap(instance.getMaxSize())); } return map; } // register @@ -396,4 +384,21 @@ public class CacheMgt public void newRecord(String tableName, int recordId) { clusterNewRecord(tableName, recordId); } + + private static class MaxSizeHashMap extends LinkedHashMap { + /** + * generated serial id + */ + private static final long serialVersionUID = 5532596165440544235L; + private final int maxSize; + + public MaxSizeHashMap(int maxSize) { + this.maxSize = maxSize; + } + + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return maxSize <= 0 ? false : size() > maxSize; + } + } } // CCache diff --git a/org.adempiere.base/src/org/compiere/util/Msg.java b/org.adempiere.base/src/org/compiere/util/Msg.java index 6872563371..41f5e198a9 100644 --- a/org.adempiere.base/src/org/compiere/util/Msg.java +++ b/org.adempiere.base/src/org/compiere/util/Msg.java @@ -108,7 +108,7 @@ public final class Msg if (retValue != null && retValue.size() > 0) return retValue; - retValue = new CCache("element", 100); + retValue = new CCache("element", 100, 0, false, 0); m_elementCache.put(AD_Language, retValue); return retValue; } @@ -124,7 +124,7 @@ public final class Msg private CCache initMsg (String AD_Language) { // Trace.printStack(); - CCache msg = new CCache(I_AD_Message.Table_Name, MAP_SIZE, 0); + CCache msg = new CCache(I_AD_Message.Table_Name, MAP_SIZE, 0, false, 0); // if (!DB.isConnected()) { diff --git a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java index 93caefe48a..bb0ebeea9a 100755 --- a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java +++ b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java @@ -117,7 +117,7 @@ public class DB_PostgreSQL implements AdempiereDatabase public static final String NATIVE_MARKER = "NATIVE_"+Database.DB_POSTGRESQL+"_KEYWORK"; - private CCache convertCache = new CCache(null, "DB_PostgreSQL_Convert_Cache", 1000, 0, true); + private CCache convertCache = new CCache(null, "DB_PostgreSQL_Convert_Cache", 1000, 60, true); private Random rand = new Random(); diff --git a/org.idempiere.hazelcast.service.config/hazelcast.xml b/org.idempiere.hazelcast.service.config/hazelcast.xml index 989aa85ef0..38d2f63938 100644 --- a/org.idempiere.hazelcast.service.config/hazelcast.xml +++ b/org.idempiere.hazelcast.service.config/hazelcast.xml @@ -117,7 +117,7 @@ then all entries of the map will be copied to another JVM for fail-safety. 0 means no backup. --> - 1 + 0