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
/** 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

View File

@ -172,7 +172,7 @@ public class MPriceList extends X_M_PriceList
} // getPricePrecision
/** 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());
} // get
/** 60 minute Cache */
/** 10 minute 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

View File

@ -52,6 +52,8 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, 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<K,V> implements CacheInterface, Map<K, V>, 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<K,V> implements CacheInterface, Map<K, V>, 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<K,V> implements CacheInterface, Map<K, V>, Serializable
@Override
public void newRecord(int record_ID) {
}
public int getMaxSize() {
return m_maxSize;
}
} // CCache

View File

@ -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<K, V>() {
/**
* generated serial id
*/
private static final long serialVersionUID = -9111152673370957054L;
@Override
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return size() > MAX_SIZE;
}
});
map = Collections.synchronizedMap(new MaxSizeHashMap<K, V>(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<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

View File

@ -108,7 +108,7 @@ public final class Msg
if (retValue != null && retValue.size() > 0)
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);
return retValue;
}
@ -124,7 +124,7 @@ public final class Msg
private CCache<String,String> initMsg (String AD_Language)
{
// 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())
{

View File

@ -117,7 +117,7 @@ public class DB_PostgreSQL implements AdempiereDatabase
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();

View File

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