From fca0963fd957d8aa458fed72d6660588160e9009 Mon Sep 17 00:00:00 2001 From: hengsin Date: Fri, 29 May 2020 19:53:32 +0800 Subject: [PATCH] IDEMPIERE-4311 Implement cache for MAccount(C_ValidCombination) (#84) Implement cache for MAccount.get(ctx, C_ValidCombination_ID) --- .../src/org/compiere/model/MAccount.java | 19 +++++++++++++++++-- .../org/compiere/model/MAccountLookup.java | 7 ++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MAccount.java b/org.adempiere.base/src/org/compiere/model/MAccount.java index c60e311d7c..80e094e48d 100644 --- a/org.adempiere.base/src/org/compiere/model/MAccount.java +++ b/org.adempiere.base/src/org/compiere/model/MAccount.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Properties; import java.util.logging.Level; +import org.compiere.util.CCache; import org.compiere.util.CLogger; import org.compiere.util.Env; @@ -40,6 +41,8 @@ public class MAccount extends X_C_ValidCombination * */ private static final long serialVersionUID = 7980515458720808532L; + + private static final CCache s_cache = new CCache(Table_Name, 100); /* * Deprecated - use the same method with trxName instead @@ -365,8 +368,20 @@ public class MAccount extends X_C_ValidCombination */ public static MAccount get (Properties ctx, int C_ValidCombination_ID) { - // Maybe later cache - return new MAccount(ctx, C_ValidCombination_ID, null); + MAccount account = s_cache.get(C_ValidCombination_ID); + if (account != null && account.getCtx() == ctx) + return account; + + account = new MAccount(ctx, C_ValidCombination_ID, null); + if (account.getC_ValidCombination_ID() == C_ValidCombination_ID) + { + s_cache.put(C_ValidCombination_ID, account); + return account; + } + else + { + return null; + } } // getAccount /** diff --git a/org.adempiere.base/src/org/compiere/model/MAccountLookup.java b/org.adempiere.base/src/org/compiere/model/MAccountLookup.java index 19c3e05ce3..b125577ccd 100644 --- a/org.adempiere.base/src/org/compiere/model/MAccountLookup.java +++ b/org.adempiere.base/src/org/compiere/model/MAccountLookup.java @@ -145,12 +145,9 @@ public final class MAccountLookup extends Lookup implements Serializable if (ID == C_ValidCombination_ID) // already loaded return true; - final String whereClause = "C_ValidCombination_ID=?"; - MAccount account = new Query(Env.getCtx(),I_C_ValidCombination.Table_Name,whereClause,null) - .setParameters(ID) - .firstOnly(); + MAccount account = MAccount.get(Env.getCtx(), ID); - if(account == null) + if(account == null || account.getC_ValidCombination_ID() != ID) return false; C_ValidCombination_ID = account.getC_ValidCombination_ID();