IDEMPIERE-3653 1008016 Remove max size limit for msg and element cache. Some cache adjustment. Remove backup of hazelcast map. / integrate partial patch from hengsin

This commit is contained in:
Carlos Ruiz 2017-05-25 16:49:51 +08:00
parent 6105b42ecb
commit 3bc0e45599
8 changed files with 48 additions and 24 deletions

View File

@ -71,7 +71,7 @@ public class MImage extends X_AD_Image
} // get } // get
/** Cache */ /** Cache */
private static CCache<Integer,MImage> s_cache = new CCache<Integer,MImage>(Table_Name, 20); private static CCache<Integer,MImage> s_cache = new CCache<Integer,MImage>(Table_Name, 20, 10);
/** /**
* Constructor * Constructor

View File

@ -172,7 +172,7 @@ public class MPriceList extends X_M_PriceList
} // getPricePrecision } // getPricePrecision
/** Cache of Price Lists */ /** Cache of Price Lists */
private static CCache<Integer,MPriceList> s_cache = new CCache<Integer,MPriceList>(Table_Name, 5); private static CCache<Integer,MPriceList> s_cache = new CCache<Integer,MPriceList>(Table_Name, 5, 5);
/************************************************************************** /**************************************************************************

View File

@ -124,9 +124,9 @@ public class ImageElement extends PrintElement
return new ImageElement(image.getImage()); return new ImageElement(image.getImage());
} // get } // get
/** 60 minute Cache */ /** 10 minute Cache */
private static CCache<Object,ImageElement> s_cache private static CCache<Object,ImageElement> s_cache
= new CCache<Object,ImageElement>(null, "ImageElement", 10, 60, false); = new CCache<Object,ImageElement>(null, "ImageElement", 10, 10, false);
/************************************************************************** /**************************************************************************
* Create from existing Image * Create from existing Image

View File

@ -52,6 +52,8 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
@SuppressWarnings("unused") @SuppressWarnings("unused")
private boolean m_distributed; private boolean m_distributed;
private int m_maxSize = 0;
public CCache (String name, int initialCapacity) public CCache (String name, int initialCapacity)
{ {
this(name, name, initialCapacity); this(name, name, initialCapacity);
@ -67,6 +69,11 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
this(name, name, initialCapacity, expireMinutes, distributed); 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 * Adempiere Cache - expires after 2 hours
* @param name (table) name of the cache * @param name (table) name of the cache
@ -79,7 +86,12 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
public CCache (String tableName, String name, int initialCapacity, boolean distributed) 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);
} }
/** /**
@ -87,12 +99,15 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
* @param name (table) name of the cache * @param name (table) name of the cache
* @param initialCapacity initial capacity * @param initialCapacity initial capacity
* @param expireMinutes expire after minutes (0=no expire) * @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_name = name;
m_tableName = tableName; m_tableName = tableName;
setExpireMinutes(expireMinutes); setExpireMinutes(expireMinutes);
m_maxSize = maxSize;
cache = CacheMgt.get().register(this, distributed); cache = CacheMgt.get().register(this, distributed);
m_distributed = distributed; m_distributed = distributed;
if (distributed) { if (distributed) {
@ -408,4 +423,8 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
@Override @Override
public void newRecord(int record_ID) { public void newRecord(int record_ID) {
} }
public int getMaxSize() {
return m_maxSize;
}
} // CCache } // CCache

View File

@ -21,7 +21,6 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.logging.Level; import java.util.logging.Level;
@ -68,7 +67,7 @@ public class CacheMgt
/** Logger */ /** Logger */
private static CLogger log = CLogger.getCLogger(CacheMgt.class); private static CLogger log = CLogger.getCLogger(CacheMgt.class);
private static int MAX_SIZE = 1000; public static int MAX_SIZE = 1000;
static static
{ {
try try
@ -114,18 +113,7 @@ public class CacheMgt
if (map == null) if (map == null)
{ {
map = Collections.synchronizedMap(new LinkedHashMap<K, V>() { map = Collections.synchronizedMap(new MaxSizeHashMap<K, V>(instance.getMaxSize()));
/**
* generated serial id
*/
private static final long serialVersionUID = -9111152673370957054L;
@Override
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return size() > MAX_SIZE;
}
});
} }
return map; return map;
} // register } // register
@ -396,4 +384,21 @@ public class CacheMgt
public void newRecord(String tableName, int recordId) { public void newRecord(String tableName, int recordId) {
clusterNewRecord(tableName, recordId); clusterNewRecord(tableName, recordId);
} }
private static class MaxSizeHashMap<K, V> extends LinkedHashMap<K, V> {
/**
* 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<K, V> eldest) {
return maxSize <= 0 ? false : size() > maxSize;
}
}
} // CCache } // CCache

View File

@ -108,7 +108,7 @@ public final class Msg
if (retValue != null && retValue.size() > 0) if (retValue != null && retValue.size() > 0)
return retValue; return retValue;
retValue = new CCache<String, String>("element", 100); retValue = new CCache<String, String>("element", 100, 0, false, 0);
m_elementCache.put(AD_Language, retValue); m_elementCache.put(AD_Language, retValue);
return retValue; return retValue;
} }
@ -124,7 +124,7 @@ public final class Msg
private CCache<String,String> initMsg (String AD_Language) private CCache<String,String> initMsg (String AD_Language)
{ {
// Trace.printStack(); // Trace.printStack();
CCache<String,String> msg = new CCache<String,String>(I_AD_Message.Table_Name, MAP_SIZE, 0); CCache<String,String> msg = new CCache<String,String>(I_AD_Message.Table_Name, MAP_SIZE, 0, false, 0);
// //
if (!DB.isConnected()) if (!DB.isConnected())
{ {

View File

@ -117,7 +117,7 @@ public class DB_PostgreSQL implements AdempiereDatabase
public static final String NATIVE_MARKER = "NATIVE_"+Database.DB_POSTGRESQL+"_KEYWORK"; public static final String NATIVE_MARKER = "NATIVE_"+Database.DB_POSTGRESQL+"_KEYWORK";
private CCache<String, String> convertCache = new CCache<String, String>(null, "DB_PostgreSQL_Convert_Cache", 1000, 0, true); private CCache<String, String> convertCache = new CCache<String, String>(null, "DB_PostgreSQL_Convert_Cache", 1000, 60, true);
private Random rand = new Random(); private Random rand = new Random();

View File

@ -117,7 +117,7 @@
then all entries of the map will be copied to another JVM for then all entries of the map will be copied to another JVM for
fail-safety. 0 means no backup. fail-safety. 0 means no backup.
--> -->
<backup-count>1</backup-count> <backup-count>0</backup-count>
<!-- <!--
Number of async backups. 0 means no backup. Number of async backups. 0 means no backup.
--> -->