Просмотр исходного кода

Move caching strategy replacement to .mjs

pull/731/head
Andrew Leedham 6 лет назад
Родитель
Сommit
1ca3193f45
3 измененных файлов: 30 добавлений и 20 удалений
  1. +5
    -11
      mustache.js
  2. +1
    -1
      mustache.min.js
  3. +24
    -8
      mustache.mjs

+ 5
- 11
mustache.js Просмотреть файл

@@ -517,7 +517,7 @@
var cacheKey = template + ':' + (tags || mustache.tags).join(':'); var cacheKey = template + ':' + (tags || mustache.tags).join(':');
var isCacheEnabled = typeof cache !== 'undefined'; var isCacheEnabled = typeof cache !== 'undefined';
var tokens = isCacheEnabled ? cache.get(cacheKey) : undefined; var tokens = isCacheEnabled ? cache.get(cacheKey) : undefined;
if (tokens == undefined) { if (tokens == undefined) {
tokens = parseTemplate(template, tags); tokens = parseTemplate(template, tags);
isCacheEnabled && cache.set(cacheKey, tokens); isCacheEnabled && cache.set(cacheKey, tokens);
@@ -673,7 +673,10 @@
to_html: undefined, to_html: undefined,
Scanner: undefined, Scanner: undefined,
Context: undefined, Context: undefined,
Writer: undefined
Writer: undefined,
set templateCache (cache) {
defaultWriter.templateCache = cache;
}
}; };


// All high-level mustache.* functions use this writer. // All high-level mustache.* functions use this writer.
@@ -686,15 +689,6 @@
return defaultWriter.clearCache(); return defaultWriter.clearCache();
}; };


/**
* Replaces the template cache in the default writer.
*/
Object.defineProperty(mustache, 'templateCache', {
set: function setTemplateCache (cache) {
defaultWriter.templateCache = cache;
}
});

/** /**
* Parses and caches the given template in the default writer and returns the * Parses and caches the given template in the default writer and returns the
* array of tokens it contains. Doing this ahead of time avoids the need to * array of tokens it contains. Doing this ahead of time avoids the need to


+ 1
- 1
mustache.min.js
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 24
- 8
mustache.mjs Просмотреть файл

@@ -479,14 +479,25 @@ Context.prototype.lookup = function lookup (name) {
* avoid the need to parse the same template twice. * avoid the need to parse the same template twice.
*/ */
function Writer () { function Writer () {
this.cache = {};
this.templateCache = {
_cache: {},
set: function set (key, value) {
this._cache[key] = value;
},
get: function get (key) {
return this._cache[key];
},
clear: function clear () {
this._cache = {};
}
};
} }


/** /**
* Clears all cached templates in this writer. * Clears all cached templates in this writer.
*/ */
Writer.prototype.clearCache = function clearCache () { Writer.prototype.clearCache = function clearCache () {
this.cache = {};
this.templateCache !== undefined && this.templateCache.clear();
}; };


/** /**
@@ -495,13 +506,15 @@ Writer.prototype.clearCache = function clearCache () {
* that is generated from the parse. * that is generated from the parse.
*/ */
Writer.prototype.parse = function parse (template, tags) { Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache;
var cache = this.templateCache;
var cacheKey = template + ':' + (tags || mustache.tags).join(':'); var cacheKey = template + ':' + (tags || mustache.tags).join(':');
var tokens = cache[cacheKey];

if (tokens == null)
tokens = cache[cacheKey] = parseTemplate(template, tags);
var isCacheEnabled = typeof cache !== 'undefined';
var tokens = isCacheEnabled ? cache.get(cacheKey) : undefined;


if (tokens == undefined) {
tokens = parseTemplate(template, tags);
isCacheEnabled && cache.set(cacheKey, tokens);
}
return tokens; return tokens;
}; };


@@ -653,7 +666,10 @@ var mustache = {
to_html: undefined, to_html: undefined,
Scanner: undefined, Scanner: undefined,
Context: undefined, Context: undefined,
Writer: undefined
Writer: undefined,
set templateCache (cache) {
defaultWriter.templateCache = cache;
}
}; };


// All high-level mustache.* functions use this writer. // All high-level mustache.* functions use this writer.


Загрузка…
Отмена
Сохранить