IDEMPIERE-5267 Make Cache Max Size configurable via system property (#1325)

* IDEMPIERE-5267 Make Cache Max Size configurable via system property

* Allow also value zero for cache max size
This commit is contained in:
Carlos Ruiz 2022-05-12 10:58:38 +02:00 committed by GitHub
parent fda23ef1ae
commit cfb4ebe785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 4 deletions

View File

@ -43,7 +43,7 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
/**
*
*/
private static final long serialVersionUID = -2268565219001179841L;
private static final long serialVersionUID = 4960404895430292476L;
protected Map<K, V> cache = null;
@ -76,6 +76,31 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
} catch (Throwable t) {}
return 60;
}
/**
* Get the max size for the cache based on a system property
* for example -DCache.MaxSize.AD_Column=15000 will set the max size for AD_Column
* @param name
* @return
*/
private static int getCacheMaxSize(String name)
{
try
{
String property = System.getProperty("Cache.MaxSize." + name);
if (property != null && property.trim().length() > 0)
{
int cacheMaxSize = 0;
try
{
cacheMaxSize = Integer.parseInt(property.trim());
} catch (Throwable t) {}
if (cacheMaxSize > 0)
return cacheMaxSize;
}
} catch (Throwable t) {}
return -1;
}
public CCache (String name, int initialCapacity)
{
@ -100,7 +125,7 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
/**
* Adempiere Cache - expires after 2 hours
* @param name (table) name of the cache
* @param initialCapacity initial capacity
* @param initialCapacity initial capacity // ignored
*/
public CCache (String tableName, String name, int initialCapacity)
{
@ -120,7 +145,7 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
/**
* Adempiere Cache
* @param name (table) name of the cache
* @param initialCapacity initial capacity
* @param initialCapacity initial capacity // ignored
* @param expireMinutes expire after minutes (0=no expire)
* @param distributed
* @param maxSize ignore if distributed=true
@ -130,7 +155,11 @@ public class CCache<K,V> implements CacheInterface, Map<K, V>, Serializable
m_name = name;
m_tableName = tableName;
setExpireMinutes(expireMinutes);
m_maxSize = maxSize;
int propMaxSize = getCacheMaxSize(name);
if (propMaxSize >= 0)
m_maxSize = propMaxSize;
else
m_maxSize = maxSize;
cache = CacheMgt.get().register(this, distributed);
m_distributed = distributed;
if (distributed) {