Comparing version 0.5.0 to 0.5.1
42
index.js
@@ -66,5 +66,7 @@ /** | ||
options.renderCacheOptions = options.renderCacheOptions || {}; | ||
var max = options.renderCacheOptions.max || 1000; | ||
var pruneRate = options.renderCacheOptions.pruneRate || 10; | ||
var renderCaches = LRU({ | ||
max: options.renderCacheOptions.max || 5000, | ||
length: function (n, key) { | ||
max: max, | ||
length: function(n, key) { | ||
return n.length; | ||
@@ -74,2 +76,3 @@ }, | ||
}); | ||
renderCaches.setCount = 0; | ||
if (swigInstance) { | ||
@@ -109,4 +112,13 @@ debuglog('use swig instance cache'); | ||
get: renderCaches.get.bind(renderCaches), | ||
set: renderCaches.set.bind(renderCaches), | ||
clean: function () { | ||
set: function (key, value) { | ||
// 设置过多缓存时,考虑清理老缓存请求次数 | ||
if (renderCaches.setCount > pruneRate * max) { | ||
renderCaches.prune(); | ||
renderCaches.setCount = 0; | ||
debuglog('prune widget cache'); | ||
} | ||
renderCaches.setCount++; | ||
return renderCaches.set(key, value); | ||
}, | ||
clean: function() { | ||
console.log('items', renderCaches.itemCount, renderCaches.length); | ||
@@ -122,4 +134,3 @@ renderCaches.reset(); | ||
this.swig.renderCache.clean && this.swig.renderCache.clean(); | ||
} catch (e) { | ||
} | ||
} catch (e) {} | ||
}; | ||
@@ -145,11 +156,20 @@ | ||
debuglog('load render cache by [%s]', cacheKey); | ||
return cacheContent; | ||
return function (locals) { | ||
return cacheContent; | ||
}; | ||
} | ||
} | ||
var res = this.compileFile(pathname, options); | ||
if (cacheKey) { | ||
debuglog('set render cache [%s]', cacheKey); | ||
self.renderCache.set(cacheKey, res); | ||
var newRes = function(locals) { | ||
var contents = res(locals); | ||
if (cacheKey) { | ||
debuglog('set render cache [%s]', cacheKey); | ||
self.renderCache.set(cacheKey, contents); | ||
} | ||
return contents; | ||
} | ||
return res; | ||
newRes.tokens = res.tokens; | ||
newRes.parent = res.parent; | ||
newRes.blocks = res.blocks; | ||
return newRes; | ||
} | ||
@@ -156,0 +176,0 @@ |
{ | ||
"name": "yog-swig", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "custom swig template, add some tag or filter ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
25922
666