Browse Source

getTemplate(IStringSource) 添加对 getKey() 的 null 判断,也即添加是否缓存的判断。
添加 getTemplateByString(String content, boolean cache) 方法

James 8 years ago
parent
commit
bee0172e9d
1 changed files with 17 additions and 5 deletions
  1. 17 5
      src/main/java/com/jfinal/template/Engine.java

+ 17 - 5
src/main/java/com/jfinal/template/Engine.java

@@ -19,7 +19,6 @@ package com.jfinal.template;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
-import com.jfinal.kit.HashKit;
 import com.jfinal.kit.StrKit;
 import com.jfinal.template.expr.ast.MethodKit;
 import com.jfinal.template.stat.Parser;
@@ -154,18 +153,27 @@ public class Engine {
 		return template;
 	}
 	
+	public Template getTemplateByString(String content) {
+		return getTemplateByString(content, false);
+	}
+	
 	/**
 	 * Get template by string content
 	 */
-	public Template getTemplateByString(String content) {
-		String key = HashKit.md5(content);
+	public Template getTemplateByString(String content, boolean cache) {
+		MemoryStringSource memoryStringSource = new MemoryStringSource(content, cache);
+		if (!cache) {
+			return buildTemplateByStringSource(memoryStringSource);
+		}
+		
+		String key = memoryStringSource.getKey();
 		Template template = templateCache.get(key);
 		if (template == null) {
-			template = buildTemplateByStringSource(new MemoryStringSource(content));
+			template = buildTemplateByStringSource(memoryStringSource);
 			templateCache.put(key, template);
 		} else if (devMode) {
 			if (template.isModified()) {
-				template = buildTemplateByStringSource(new MemoryStringSource(content));
+				template = buildTemplateByStringSource(memoryStringSource);
 				templateCache.put(key, template);
 			}
 		}
@@ -177,6 +185,10 @@ public class Engine {
 	 */
 	public Template getTemplate(IStringSource stringSource) {
 		String key = stringSource.getKey();
+		if (key == null) {
+			return buildTemplateByStringSource(stringSource);
+		}
+		
 		Template template = templateCache.get(key);
 		if (template == null) {
 			template = buildTemplateByStringSource(stringSource);