Przeglądaj źródła

Move caching strategy replacement to .mjs

pull/731/head
Andrew Leedham 6 lat temu
rodzic
commit
1ca3193f45
3 zmienionych plików z 30 dodań i 20 usunięć
  1. +5
    -11
      mustache.js
  2. +1
    -1
      mustache.min.js
  3. +24
    -8
      mustache.mjs

+ 5
- 11
mustache.js Wyświetl plik

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

// All high-level mustache.* functions use this writer.
@@ -686,15 +689,6 @@
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
* array of tokens it contains. Doing this ahead of time avoids the need to


+ 1
- 1
mustache.min.js
Plik diff jest za duży
Wyświetl plik


+ 24
- 8
mustache.mjs Wyświetl plik

@@ -479,14 +479,25 @@ Context.prototype.lookup = function lookup (name) {
* avoid the need to parse the same template twice.
*/
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.
*/
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.
*/
Writer.prototype.parse = function parse (template, tags) {
var cache = this.cache;
var cache = this.templateCache;
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;
};

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

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


Ładowanie…
Anuluj
Zapisz