This adds caches to CategoryContentWrapper and ProductConfigItemContentWrapper classes

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1708326 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2015-10-13 10:58:55 +00:00
parent ab2d2d772c
commit bd08e516ea
2 changed files with 18 additions and 0 deletions
@@ -36,6 +36,7 @@ import org.ofbiz.base.util.UtilCodec;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.cache.UtilCache;
import org.ofbiz.content.content.ContentWorker;
import org.ofbiz.content.content.ContentWrapper;
import org.ofbiz.entity.Delegator;
@@ -52,6 +53,8 @@ import org.ofbiz.service.LocalDispatcher;
public class CategoryContentWrapper implements ContentWrapper {
public static final String module = CategoryContentWrapper.class.getName();
public static final String SEPARATOR = "::"; // cache key separator
private static final UtilCache<String, String> categoryContentCache = UtilCache.createUtilCache("category.content", true); // use soft reference to free up memory if needed
protected LocalDispatcher dispatcher;
protected GenericValue productCategory;
@@ -92,11 +95,17 @@ public class CategoryContentWrapper implements ContentWrapper {
public static String getProductCategoryContentAsText(GenericValue productCategory, String prodCatContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, String encoderType) {
String candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId);
UtilCodec.SimpleEncoder encoder = UtilCodec.getEncoder(encoderType);
String cacheKey = prodCatContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + productCategory.get("productCategoryId");
try {
String cachedValue = categoryContentCache.get(cacheKey);
if (cachedValue != null) {
return cachedValue;
}
Writer outWriter = new StringWriter();
getProductCategoryContentAsText(null, productCategory, prodCatContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter);
String outString = outWriter.toString();
if (outString.length() > 0) {
outString = categoryContentCache.putIfAbsentAndGet(cacheKey, encoder.encode(outString));
return encoder.encode(outString);
} else {
return null;
@@ -34,6 +34,7 @@ import org.ofbiz.base.util.StringUtil.StringWrapper;
import org.ofbiz.base.util.UtilCodec;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.cache.UtilCache;
import org.ofbiz.content.content.ContentWorker;
import org.ofbiz.content.content.ContentWrapper;
import org.ofbiz.entity.Delegator;
@@ -52,6 +53,8 @@ import org.ofbiz.service.ServiceContainer;
public class ProductConfigItemContentWrapper implements ContentWrapper {
public static final String module = ProductConfigItemContentWrapper.class.getName();
public static final String SEPARATOR = "::"; // cache key separator
private static final UtilCache<String, String> configItemContentCache = UtilCache.createUtilCache("configItem.content", true); // use soft reference to free up memory if needed
protected transient LocalDispatcher dispatcher;
protected String dispatcherName;
@@ -116,11 +119,17 @@ public class ProductConfigItemContentWrapper implements ContentWrapper {
public static String getProductConfigItemContentAsText(GenericValue productConfigItem, String confItemContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, String encoderType) {
UtilCodec.SimpleEncoder encoder = UtilCodec.getEncoder(encoderType);
String candidateFieldName = ModelUtil.dbNameToVarName(confItemContentTypeId);
String cacheKey = confItemContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + productConfigItem.get("productCategoryId");
try {
String cachedValue = configItemContentCache.get(cacheKey);
if (cachedValue != null) {
return cachedValue;
}
Writer outWriter = new StringWriter();
getProductConfigItemContentAsText(null, productConfigItem, confItemContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter);
String outString = outWriter.toString();
if (outString.length() > 0) {
outString = configItemContentCache.putIfAbsentAndGet(cacheKey, encoder.encode(outString));
return encoder.encode(outString);
} else {
String candidateOut = productConfigItem.getModelEntity().isField(candidateFieldName) ? productConfigItem.getString(candidateFieldName): "";