|
Author: jacopoc
Date: Tue May 29 12:24:19 2012 New Revision: 1343696 URL: http://svn.apache.org/viewvc?rev=1343696&view=rev Log: Improved code that manages the cache: * protected the UtilCache object (static field) by making it private and final * removed unnecessary synchronization * removed unnecessary null checks * moved code that initializes static cache fields out of constructors * replaced get+null check+get with get+null check * replaced containsKey + null check + get with get + null check * replaced some put with putIfAbsentAndGet Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigWorker.java ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java (original) +++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderContentWrapper.java Tue May 29 12:24:19 2012 @@ -50,7 +50,7 @@ public class OrderContentWrapper { public static final String module = OrderContentWrapper.class.getName(); public static final String SEPARATOR = "::"; // cache key separator - public static UtilCache<String, String> orderContentCache; + private static final UtilCache<String, String> orderContentCache = UtilCache.createUtilCache("order.content", true); // use soft reference to free up memory if needed public static OrderContentWrapper makeOrderContentWrapper(GenericValue order, HttpServletRequest request) { return new OrderContentWrapper(order, request); @@ -66,9 +66,6 @@ public class OrderContentWrapper { this.order = order; this.locale = locale; this.mimeTypeId = mimeTypeId; - if (orderContentCache == null) { - orderContentCache = UtilCache.createUtilCache("order.content", true); // use soft reference to free up memory if needed - } } public OrderContentWrapper(GenericValue order, HttpServletRequest request) { @@ -76,9 +73,6 @@ public class OrderContentWrapper { this.order = order; this.locale = UtilHttp.getLocale(request); this.mimeTypeId = "text/html"; - if (orderContentCache == null) { - orderContentCache = UtilCache.createUtilCache("order.content", true); // use soft reference to free up memory if needed - } } public String get(String orderContentTypeId) { @@ -102,17 +96,16 @@ public class OrderContentWrapper { String cacheKey = orderContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + order.get("orderId") + SEPARATOR + orderItemSeqId; try { - if (orderContentCache != null && orderContentCache.get(cacheKey) != null) { - return orderContentCache.get(cacheKey); + String cachedValue = orderContentCache.get(cacheKey); + if (cachedValue != null) { + return cachedValue; } Writer outWriter = new StringWriter(); getOrderContentAsText(null, null, order, orderContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter); String outString = outWriter.toString(); if (outString.length() > 0) { - if (orderContentCache != null) { - orderContentCache.put(cacheKey, outString); - } + outString = orderContentCache.putIfAbsentAndGet(cacheKey, outString); } return outString; Modified: ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java (original) +++ ofbiz/trunk/applications/party/src/org/ofbiz/party/content/PartyContentWrapper.java Tue May 29 12:24:19 2012 @@ -47,7 +47,7 @@ public class PartyContentWrapper impleme public static final String module = PartyContentWrapper.class.getName(); public static final String CACHE_KEY_SEPARATOR = "::"; - public static UtilCache<String, String> partyContentCache = UtilCache.createUtilCache("party.content.rendered", true); + private static final UtilCache<String, String> partyContentCache = UtilCache.createUtilCache("party.content.rendered", true); protected LocalDispatcher dispatcher; protected GenericValue party; @@ -135,8 +135,11 @@ public class PartyContentWrapper impleme } try { - if (useCache && partyContentCache.get(cacheKey) != null) { - return partyContentCache.get(cacheKey); + if (useCache) { + String cachedValue = partyContentCache.get(cacheKey); + if (cachedValue != null) { + return cachedValue; + } } Writer outWriter = new StringWriter(); @@ -144,10 +147,7 @@ public class PartyContentWrapper impleme String outString = outWriter.toString(); if (outString.length() > 0) { - if (partyContentCache != null) { - partyContentCache.put(cacheKey, outString); - } - return outString; + return partyContentCache.putIfAbsentAndGet(cacheKey, outString); } else { String candidateOut = party.getModelEntity().isField(candidateFieldName) ? party.getString(candidateFieldName): ""; return candidateOut == null ? "" : candidateOut; Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigWorker.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigWorker.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/config/ProductConfigWorker.java Tue May 29 12:24:19 2012 @@ -52,7 +52,7 @@ public class ProductConfigWorker { public static final String resource = "ProductUiLabels"; public static final String SEPARATOR = "::"; // cache key separator - public static UtilCache<String, ProductConfigWrapper> productConfigCache = UtilCache.createUtilCache("product.config", true); // use soft reference to free up memory if needed + private static final UtilCache<String, ProductConfigWrapper> productConfigCache = UtilCache.createUtilCache("product.config", true); // use soft reference to free up memory if needed public static ProductConfigWrapper getProductConfigWrapper(String productId, String currencyUomId, HttpServletRequest request) { ProductConfigWrapper configWrapper = null; @@ -65,15 +65,16 @@ public class ProductConfigWorker { * productId::catalogId::webSiteId::currencyUomId, or whatever the SEPARATOR is defined above to be. */ String cacheKey = productId + SEPARATOR + productStoreId + SEPARATOR + catalogId + SEPARATOR + webSiteId + SEPARATOR + currencyUomId; - if (!productConfigCache.containsKey(cacheKey)) { + configWrapper = productConfigCache.get(cacheKey); + if (configWrapper == null) { configWrapper = new ProductConfigWrapper((Delegator)request.getAttribute("delegator"), (LocalDispatcher)request.getAttribute("dispatcher"), productId, productStoreId, catalogId, webSiteId, currencyUomId, UtilHttp.getLocale(request), autoUserLogin); - productConfigCache.put(cacheKey, new ProductConfigWrapper(configWrapper)); + configWrapper = productConfigCache.putIfAbsentAndGet(cacheKey, new ProductConfigWrapper(configWrapper)); } else { - configWrapper = new ProductConfigWrapper(productConfigCache.get(cacheKey)); + configWrapper = new ProductConfigWrapper(configWrapper); } } catch (ProductConfigWrapperException we) { configWrapper = null; Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductContentWrapper.java Tue May 29 12:24:19 2012 @@ -54,7 +54,7 @@ public class ProductContentWrapper imple public static final String module = ProductContentWrapper.class.getName(); public static final String SEPARATOR = "::"; // cache key separator - public static UtilCache<String, String> productContentCache = UtilCache.createUtilCache("product.content.rendered", true); + private static final UtilCache<String, String> productContentCache = UtilCache.createUtilCache("product.content.rendered", true); public static ProductContentWrapper makeProductContentWrapper(GenericValue product, HttpServletRequest request) { return new ProductContentWrapper(product, request); @@ -107,18 +107,16 @@ public class ProductContentWrapper imple */ String cacheKey = productContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + product.get("productId"); try { - if (productContentCache.get(cacheKey) != null) { - return productContentCache.get(cacheKey); + String cachedValue = productContentCache.get(cacheKey); + if (cachedValue != null) { + return cachedValue; } Writer outWriter = new StringWriter(); getProductContentAsText(null, product, productContentTypeId, locale, mimeTypeId, partyId, roleTypeId, delegator, dispatcher, outWriter); String outString = outWriter.toString(); if (outString.length() > 0) { - if (productContentCache != null) { - productContentCache.put(cacheKey, outString); - } - return outString; + return productContentCache.putIfAbsentAndGet(cacheKey, outString); } else { String candidateOut = product.getModelEntity().isField(candidateFieldName) ? product.getString(candidateFieldName): ""; return candidateOut == null? "" : candidateOut; Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductPromoContentWrapper.java Tue May 29 12:24:19 2012 @@ -58,7 +58,7 @@ public class ProductPromoContentWrapper public static final String module = ProductPromoContentWrapper.class.getName(); public static final String SEPARATOR = "::"; // cache key separator - public static UtilCache<String, String> productPromoContentCache = UtilCache.createUtilCache("product.promo.content.rendered", true); + private static final UtilCache<String, String> productPromoContentCache = UtilCache.createUtilCache("product.promo.content.rendered", true); public static ProductPromoContentWrapper makeProductPromoContentWrapper(GenericValue productPromo, HttpServletRequest request) { return new ProductPromoContentWrapper(productPromo, request); @@ -111,18 +111,16 @@ public class ProductPromoContentWrapper */ String cacheKey = productPromoContentTypeId + SEPARATOR + locale + SEPARATOR + mimeTypeId + SEPARATOR + productPromo.get("productPromoId"); try { - if (productPromoContentCache.get(cacheKey) != null) { - return productPromoContentCache.get(cacheKey); + String cachedValue = productPromoContentCache.get(cacheKey); + if (cachedValue != null) { + return cachedValue; } Writer outWriter = new StringWriter(); getProductPromoContentAsText(null, productPromo, productPromoContentTypeId, locale, mimeTypeId, partyId, roleTypeId, delegator, dispatcher, outWriter); String outString = outWriter.toString(); if (outString.length() > 0) { - if (productPromoContentCache != null) { - productPromoContentCache.put(cacheKey, outString); - } - return outString; + return productPromoContentCache.putIfAbsentAndGet(cacheKey, outString); } else { String candidateOut = productPromo.getModelEntity().isField(candidateFieldName) ? productPromo.getString(candidateFieldName): ""; return candidateOut == null? "" : candidateOut; Modified: ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java?rev=1343696&r1=1343695&r2=1343696&view=diff ============================================================================== --- ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java (original) +++ ofbiz/trunk/applications/workeffort/src/org/ofbiz/workeffort/content/WorkEffortContentWrapper.java Tue May 29 12:24:19 2012 @@ -52,7 +52,7 @@ public class WorkEffortContentWrapper im public static final String module = WorkEffortContentWrapper.class.getName(); public static final String CACHE_KEY_SEPARATOR = "::"; - public static UtilCache<String, String> workEffortContentCache = UtilCache.createUtilCache("workeffort.content.rendered", true); + private static final UtilCache<String, String> workEffortContentCache = UtilCache.createUtilCache("workeffort.content.rendered", true); protected LocalDispatcher dispatcher; protected GenericValue workEffort; @@ -236,18 +236,18 @@ public class WorkEffortContentWrapper im } try { - if (useCache && workEffortContentCache.get(cacheKey) != null) { - return workEffortContentCache.get(cacheKey); + if (useCache) { + String cachedValue = workEffortContentCache.get(cacheKey); + if (cachedValue != null) { + return cachedValue; + } } Writer outWriter = new StringWriter(); getWorkEffortContentAsText(contentId, null, workEffort, workEffortContentTypeId, locale, mimeTypeId, delegator, dispatcher, outWriter); String outString = outWriter.toString(); if (outString.length() > 0) { - if (workEffortContentCache != null) { - workEffortContentCache.put(cacheKey, outString); - } - return outString; + return workEffortContentCache.putIfAbsentAndGet(cacheKey, outString); } else { String candidateOut = workEffort.getModelEntity().isField(candidateFieldName) ? workEffort.getString(candidateFieldName): ""; return candidateOut == null? "" : candidateOut; |
| Free forum by Nabble | Edit this page |
