Author: jacopoc
Date: Tue May 29 13:52:32 2012
New Revision: 1343727

URL: http://svn.apache.org/viewvc?rev=1343727&view=rev
Log:
Improved code that manages the cache:
* protected the UtilCache object (static field) by making it private and final
* removed unnecessary synchronization


Modified:
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1343727&r1=1343726&r2=1343727&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
 (original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/template/FreeMarkerWorker.java
 Tue May 29 13:52:32 2012
@@ -75,7 +75,7 @@ public class FreeMarkerWorker {
     public static final String module = FreeMarkerWorker.class.getName();
 
     // use soft references for this so that things from Content records don't 
kill all of our memory, or maybe not for performance reasons... hmmm, leave to 
config file...
-    public static UtilCache<String, Template> cachedTemplates = 
UtilCache.createUtilCache("template.ftl.general", 0, 0, false);
+    private static final UtilCache<String, Template> cachedTemplates = 
UtilCache.createUtilCache("template.ftl.general", 0, 0, false);
     private static final BeansWrapper defaultOfbizWrapper = 
configureBeansWrapper(new BeansWrapper());
     private static final Configuration defaultOfbizConfig = 
makeConfiguration(defaultOfbizWrapper);
 
@@ -200,49 +200,34 @@ public class FreeMarkerWorker {
     public static Environment renderTemplateFromString(String templateString, 
String templateLocation, Map<String, Object> context, Appendable outWriter) 
throws TemplateException, IOException {
         Template template = cachedTemplates.get(templateLocation);
         if (template == null) {
-            synchronized (cachedTemplates) {
-                template = cachedTemplates.get(templateLocation);
-                if (template == null) {
-                    Reader templateReader = new StringReader(templateString);
-                    template = new Template(templateLocation, templateReader, 
defaultOfbizConfig);
-                    templateReader.close();
-                    cachedTemplates.put(templateLocation, template);
-                }
-            }
+            Reader templateReader = new StringReader(templateString);
+            template = new Template(templateLocation, templateReader, 
defaultOfbizConfig);
+            templateReader.close();
+            template = cachedTemplates.putIfAbsentAndGet(templateLocation, 
template);
         }
         return renderTemplate(template, context, outWriter);
     }
 
     public static Environment renderTemplateFromString(String templateString, 
String templateLocation, Map<String, Object> context, Appendable outWriter, 
boolean useCache) throws TemplateException, IOException {
         Template template = null;
-        if (useCache){
+        if (useCache) {
             template = cachedTemplates.get(templateLocation);
-        }
-        if (template == null) {
-            if (useCache){
-                synchronized (cachedTemplates) {
-                    template = cachedTemplates.get(templateLocation);
-                    if (template == null) {
-                        Reader templateReader = new 
StringReader(templateString);
-                        template = new Template(templateLocation, 
templateReader, defaultOfbizConfig);
-                        templateReader.close();
-                        cachedTemplates.put(templateLocation, template);
-                    }
-                }
-            } else {
+            if (template == null) {
                 Reader templateReader = new StringReader(templateString);
                 template = new Template(templateLocation, templateReader, 
defaultOfbizConfig);
                 templateReader.close();
+                template = cachedTemplates.putIfAbsentAndGet(templateLocation, 
template);
             }
+        } else {
+            Reader templateReader = new StringReader(templateString);
+            template = new Template(templateLocation, templateReader, 
defaultOfbizConfig);
+            templateReader.close();
         }
-
         return renderTemplate(template, context, outWriter);
     }
 
     public static void clearTemplateFromCache(String templateLocation) {
-        synchronized (cachedTemplates) {
-            cachedTemplates.remove(templateLocation);
-        }
+        cachedTemplates.remove(templateLocation);
     }
 
     /**
@@ -341,16 +326,11 @@ public class FreeMarkerWorker {
     public static Template getTemplate(String templateLocation, 
UtilCache<String, Template> cache, Configuration config) throws 
TemplateException, IOException {
         Template template = cache.get(templateLocation);
         if (template == null) {
-            synchronized (cache) {
-                template = cache.get(templateLocation);
-                if (template == null) {
-                    // only make the reader if we need it, and then close it 
right after!
-                    Reader templateReader = makeReader(templateLocation);
-                    template = new Template(templateLocation, templateReader, 
config);
-                    templateReader.close();
-                    cache.put(templateLocation, template);
-                }
-            }
+            // only make the reader if we need it, and then close it right 
after!
+            Reader templateReader = makeReader(templateLocation);
+            template = new Template(templateLocation, templateReader, config);
+            templateReader.close();
+            template = cache.putIfAbsentAndGet(templateLocation, template);
         }
         return template;
     }


Reply via email to