Author: jleroux
Date: Tue Oct 13 10:58:55 2015 New Revision: 1708326 URL: http://svn.apache.org/viewvc?rev=1708326&view=rev Log: This adds caches to CategoryContentWrapper and ProductConfigItemContentWrapper classes Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java?rev=1708326&r1=1708325&r2=1708326&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryContentWrapper.java Tue Oct 13 10:58:55 2015 @@ -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 impl 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; Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java?rev=1708326&r1=1708325&r2=1708326&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigItemContentWrapper.java Tue Oct 13 10:58:55 2015 @@ -34,6 +34,7 @@ import org.ofbiz.base.util.StringUtil.St 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.ServiceContaine 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 ProductConfigItemContentWra 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): ""; |
Free forum by Nabble | Edit this page |