ソースを参照

Merge branch 'master' into jfinal-java8

James 8 年 前
コミット
a43b922858

+ 28 - 3
src/main/java/com/jfinal/template/Engine.java

@@ -155,17 +155,38 @@ public class Engine {
 	}
 	}
 	
 	
 	/**
 	/**
-	 * Get template by string content
+	 * Get template by string content and do not cache the template
+	 * 
+	 * 重要:MemoryStringSource 中的 key = HashKit.md5(content),也即 key
+	 *     与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化
+	 *     因此,原先 key 所对应的 Template 缓存对象已无法被获取,当 getTemplateByString(String)
+	 *     的 String 参数的数量不确定时会引发内存泄漏
+	 *     
+	 *     当 getTemplateByString(String, boolean) 中的 String 参数的
+	 *     数量可控并且确定时,才可对其使用缓存 
 	 */
 	 */
 	public Template getTemplateByString(String content) {
 	public Template getTemplateByString(String content) {
+		return getTemplateByString(content, false);
+	}
+	
+	/**
+	 * Get template by string content
+	 * @param content 模板内容
+	 * @param cache true 则缓存 Template,否则不缓存
+	 */
+	public Template getTemplateByString(String content, boolean cache) {
+		if (!cache) {
+			return buildTemplateByStringSource(new MemoryStringSource(content, cache));
+		}
+		
 		String key = HashKit.md5(content);
 		String key = HashKit.md5(content);
 		Template template = templateCache.get(key);
 		Template template = templateCache.get(key);
 		if (template == null) {
 		if (template == null) {
-			template = buildTemplateByStringSource(new MemoryStringSource(content));
+			template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
 			templateCache.put(key, template);
 			templateCache.put(key, template);
 		} else if (devMode) {
 		} else if (devMode) {
 			if (template.isModified()) {
 			if (template.isModified()) {
-				template = buildTemplateByStringSource(new MemoryStringSource(content));
+				template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
 				templateCache.put(key, template);
 				templateCache.put(key, template);
 			}
 			}
 		}
 		}
@@ -177,6 +198,10 @@ public class Engine {
 	 */
 	 */
 	public Template getTemplate(IStringSource stringSource) {
 	public Template getTemplate(IStringSource stringSource) {
 		String key = stringSource.getKey();
 		String key = stringSource.getKey();
+		if (key == null) {	// key 为 null 则不缓存,详见 IStringSource.getKey() 注释
+			return buildTemplateByStringSource(stringSource);
+		}
+		
 		Template template = templateCache.get(key);
 		Template template = templateCache.get(key);
 		if (template == null) {
 		if (template == null) {
 			template = buildTemplateByStringSource(stringSource);
 			template = buildTemplateByStringSource(stringSource);

+ 3 - 1
src/main/java/com/jfinal/template/EngineConfig.java

@@ -98,7 +98,9 @@ public class EngineConfig {
 	 * Add shared function by string content
 	 * Add shared function by string content
 	 */
 	 */
 	public void addSharedFunctionByString(String content) {
 	public void addSharedFunctionByString(String content) {
-		MemoryStringSource memoryStringSource = new MemoryStringSource(content);
+		// content 中的内容被解析后会存放在 Env 之中,而 MemoryStringSource 所对应的
+		// Template 对象 isModified() 始终返回 false,所以没有必要对其缓存
+		MemoryStringSource memoryStringSource = new MemoryStringSource(content, false);
 		doAddSharedFunction(memoryStringSource, null);
 		doAddSharedFunction(memoryStringSource, null);
 	}
 	}
 	
 	

+ 4 - 1
src/main/java/com/jfinal/template/IStringSource.java

@@ -27,7 +27,10 @@ public interface IStringSource {
 	boolean isModified();
 	boolean isModified();
 	
 	
 	/**
 	/**
-	 * key used to cache
+	 * key used to cache, return false if do not cache the template
+	 * 
+	 * 注意:如果不希望缓存从该 IStreamSource 解析出来的 Template 对象
+	 *      让 getKey() 返回 null 值即可  
 	 */
 	 */
 	String getKey();
 	String getKey();
 	
 	

+ 9 - 4
src/main/java/com/jfinal/template/MemoryStringSource.java

@@ -27,20 +27,25 @@ public class MemoryStringSource implements IStringSource {
 	private String key;
 	private String key;
 	private StringBuilder content;
 	private StringBuilder content;
 	
 	
-	public MemoryStringSource(String content) {
+	/**
+	 * 构造 MemoryStringSource
+	 * @param content 模板内容
+	 * @param cache true 则缓存 Template,否则不缓存
+	 */
+	public MemoryStringSource(String content, boolean cache) {
 		if (StrKit.isBlank(content)) {
 		if (StrKit.isBlank(content)) {
 			throw new IllegalArgumentException("content can not be blank");
 			throw new IllegalArgumentException("content can not be blank");
 		}
 		}
 		this.content = new StringBuilder(content);
 		this.content = new StringBuilder(content);
-		this.key = HashKit.md5(content);
+		this.key = cache ? HashKit.md5(content) : null;	// 不缓存只要将 key 值赋为 null 即可
 	}
 	}
 	
 	
-	public MemoryStringSource(StringBuilder content) {
+	public MemoryStringSource(StringBuilder content, boolean cache) {
 		if (content == null || content.length() == 0) {
 		if (content == null || content.length() == 0) {
 			throw new IllegalArgumentException("content can not be blank");
 			throw new IllegalArgumentException("content can not be blank");
 		}
 		}
 		this.content = content;
 		this.content = content;
-		this.key = HashKit.md5(content.toString());
+		this.key = cache ? HashKit.md5(content.toString()) : null;	// 不缓存只要将 key 值赋为 null 即可
 	}
 	}
 	
 	
 	public boolean isModified() {
 	public boolean isModified() {