[OFBiz] Dev - caching questions
Si Chen
schen at graciousstyle.com
Thu Sep 23 16:35:39 EDT 2004
Hi David,
Thanks. I am creating here a HashMap of contentTypeId -> contentCaches,
each of which is a UtilCache object. Then when the contentTypeId is
requested, I am going into the HashMap to get the UtilCache for that
contentTypeId and using the get, put methods on the UtilCache. Do you
think there is another way to make many caches available for all the
possible content types?
Si
David E. Jones wrote:
>
> Si,
>
> Use UtilCache for caches like this. It is there so we can have much
> more control over cache management and such.
>
> As for size, we added some stuff for this recently to UtilCache,
> though it isn't 100% reliable (and we don't know of anything that is
> for Java at this point...).
>
> -David
>
>
> On Sep 22, 2004, at 11:56 AM, Si Chen wrote:
>
>> Hi David,
>>
>> Thanks for your tips on caching. What do you (and Al) think of this
>> code for the product content wrapper? I set up a Map of caches, one
>> for each product content type, and am using it to cache the results
>> of the ProductContentWrapper, so that product content such as
>> names, descriptions, can be available faster (especially if the
>> content were complicated and had freemarker rendering.) The caches
>> use soft refrence. I did not do the synchronized checking--do you
>> think that would be necessary? One potential problem is if the
>> content were really big, such as a big image. When I looked through
>> the content types, and the only one which might be a problem is the
>> digital download. Do you think the content wrapper would/should be
>> used to return large content back?
>>
>> Also, as for monitoring the sizes of the cache, I noticed that there
>> is the size() method for finding out how many entries there are in
>> the cache. But is there anything to find out the amount of memory
>> the cache has actually taken up, or should I just do that through
>> my operating system?
>>
>> Thanks,
>>
>> Si
>> Index: ProductContentWrapper.java
>> ===================================================================
>> RCS file:
>> /usr/local/cvs/ofbiz/components/product/src/org/ofbiz/product/product/
>> ProductContentWrapper.java,v
>> retrieving revision 1.1.1.1
>> diff -u -w -r1.1.1.1 ProductContentWrapper.java
>> --- ProductContentWrapper.java 6 Apr 2004 07:00:02 -0000 1.1.1.1
>> +++ ProductContentWrapper.java 22 Sep 2004 17:48:18 -0000
>> @@ -30,11 +30,14 @@
>> import java.util.List;
>> import java.util.Locale;
>> import java.util.Map;
>> +import java.util.ArrayList;
>> +import java.util.Iterator;
>>
>> import javax.servlet.http.HttpServletRequest;
>>
>> import org.ofbiz.base.util.Debug;
>> import org.ofbiz.base.util.GeneralException;
>> +import org.ofbiz.base.util.UtilCache;
>> import org.ofbiz.base.util.UtilHttp;
>> import org.ofbiz.base.util.UtilMisc;
>> import org.ofbiz.base.util.UtilValidate;
>> @@ -60,20 +63,47 @@
>> protected Locale locale;
>> protected String mimeTypeId;
>>
>> + protected static Map caches;
>> +
>> public static ProductContentWrapper
>> makeProductContentWrapper(GenericValue product, HttpServletRequest
>> request) {
>> return new ProductContentWrapper(product, request);
>> }
>>
>> + private Map makeProductContentCaches(GenericDelegator delegator) {
>> + Map contentCaches = new HashMap();
>> + List contentTypes = new ArrayList();
>> +
>> + try {
>> + contentTypes = delegator.findAll("ProductContentType");
>> +
>> + for (Iterator cTi = contentTypes.iterator();
>> cTi.hasNext(); ) {
>> + String nextContentType = ((GenericValue)
>> cTi.next()).getString("productContentTypeId");
>> + contentCaches.put(nextContentType, new
>> UtilCache("product.content." + nextContentType, true));
>> + Debug.logInfo("Creating product content cache for "
>> + nextContentType, module);
>> + }
>> + } catch (GeneralException ex) {
>> + Debug.logError(ex, "Cannot build product content cache
>> table", module);
>> + }
>> +
>> + return contentCaches;
>> + }
>> +
>> public ProductContentWrapper(GenericValue product, Locale
>> locale, String mimeTypeId) {
>> this.product = product;
>> this.locale = locale;
>> this.mimeTypeId = mimeTypeId;
>> + if (caches == null) {
>> + caches = makeProductContentCaches(product.getDelegator());
>> + }
>> }
>>
>> public ProductContentWrapper(GenericValue product,
>> HttpServletRequest request) {
>> this.product = product;
>> this.locale = UtilHttp.getLocale(request);
>> this.mimeTypeId = "text/html";
>> + if (caches == null) {
>> + caches = makeProductContentCaches(product.getDelegator());
>> + }
>> }
>>
>> public String get(String productContentTypeId) {
>> @@ -91,10 +121,24 @@
>> public static String getProductContentAsText(GenericValue
>> product, String productContentTypeId, Locale locale, String
>> mimeTypeId, GenericDelegator delegator) {
>> String candidateFieldName =
>> ModelUtil.dbNameToVarName(productContentTypeId);
>> try {
>> + UtilCache contentCache = null;
>> + if (caches.get(productContentTypeId) != null) {
>> + contentCache = (UtilCache)
>> caches.get(productContentTypeId);
>> + }
>> +
>> + if (contentCache != null &&
>> contentCache.get(product.get("productId")) != null) {
>> + Debug.logInfo("found content cache for " +
>> product.get("productId") + "; will be returning " +
>> contentCache.get(product.get("productId")), module);
>> + return (String)
>> contentCache.get(product.get("productId"));
>> + }
>> +
>> Writer outWriter = new StringWriter();
>> getProductContentAsText(null, product,
>> productContentTypeId, locale, mimeTypeId, delegator, outWriter);
>> String outString = outWriter.toString();
>> if (outString.length() > 0) {
>> + if (contentCache != null) {
>> + Debug.logInfo("putting " + outString + " into
>> content cache under " + product.get("productId"), module);
>> + contentCache.put(product.get("productId"),
>> outString);
>> + }
>> return outString;
>> } else {
>> return null;
>>
>> _______________________________________________
>> Dev mailing list
>> Dev at lists.ofbiz.org
>> http://lists.ofbiz.org/mailman/listinfo/dev
>
>------------------------------------------------------------------------
>
>
>_______________________________________________
>Dev mailing list
>Dev at lists.ofbiz.org
>http://lists.ofbiz.org/mailman/listinfo/dev
>
More information about the Dev
mailing list