优化缓存

master
宇天 2020-04-07 01:01:41 +08:00
parent 1fd64ee360
commit ba25d33f99
2 changed files with 11 additions and 9 deletions

View File

@ -25,8 +25,7 @@ class Smarty {
this.__REG__ = new RegExp(this.opt.ext + '$') this.__REG__ = new RegExp(this.opt.ext + '$')
this.tool = new Tool(this.opt) this.tool = new Tool(this.opt)
this.__DATA__ = Object.create(null) // 预定义的变量储存 this.__DATA__ = Object.create(null) // 预定义的变量储存
this.__TMP__ = Object.create(null) // 模板缓存 this.__CACHE__ = Object.create(null) // 渲染缓存
this.__CACHE__ = Object.create(null) // 渲染缓存(模板被解析后)
} }
config(key, val) { config(key, val) {
@ -61,6 +60,7 @@ class Smarty {
*/ */
render(tpl = '', noParse = false) { render(tpl = '', noParse = false) {
var key = null var key = null
var cache
if (!this.opt.path) { if (!this.opt.path) {
throw new Error('Smarty engine must define path option') throw new Error('Smarty engine must define path option')
} }
@ -79,17 +79,19 @@ class Smarty {
return Promise.resolve(this.__CACHE__[key]) return Promise.resolve(this.__CACHE__[key])
} }
this.__TMP__[key] = this.tool.__readFile__(tpl, noParse) cache = this.tool.__readFile__(tpl, noParse)
if (noParse) { if (noParse) {
this.__CACHE__[key] = this.__TMP__[key] this.__CACHE__[key] = cache
delete this.__TMP__[key] return Promise.resolve(cache)
return Promise.resolve(this.__CACHE__[key])
} }
try { try {
this.__CACHE__[key] = this.tool.parse(this.__TMP__[key], this.__DATA__) cache = this.tool.parse(cache, this.__DATA__)
return Promise.resolve(this.__CACHE__[key]) if (this.opt.cache) {
this.__CACHE__[key] = cache
}
return Promise.resolve(cache)
} catch (err) { } catch (err) {
return Promise.reject(err) return Promise.reject(err)
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "smartyx", "name": "smartyx",
"version": "1.3.0", "version": "1.3.1",
"description": "nodeJS模板引擎理念源自于PHP的smarty模板引擎", "description": "nodeJS模板引擎理念源自于PHP的smarty模板引擎",
"keywords": ["fivejs", "smarty", "template", "ejs", "jade"], "keywords": ["fivejs", "smarty", "template", "ejs", "jade"],
"author": "宇天 <yutent@doui.cc>", "author": "宇天 <yutent@doui.cc>",