Skip to content

Commit 28ca619

Browse files
Ashinokophillipj
Ashinoko
authored andcommitted
fix bug: cache actually not working (#664)
Resolve issue #663 and add test.
1 parent 85a2c2d commit 28ca619

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mustache.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,11 @@
444444
*/
445445
Writer.prototype.parse = function parse (template, tags) {
446446
var cache = this.cache;
447-
var tokens = cache[template];
447+
var cacheKey = template + ':' + (tags || mustache.tags).join(':');
448+
var tokens = cache[cacheKey];
448449

449450
if (tokens == null)
450-
tokens = cache[template + ':' + (tags || mustache.tags).join(':')] = parseTemplate(template, tags);
451+
tokens = cache[cacheKey] = parseTemplate(template, tags);
451452

452453
return tokens;
453454
};

test/parse-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ var expectations = {
5353
: [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ]
5454
};
5555

56+
beforeEach(function (){
57+
Mustache.clearCache();
58+
});
59+
5660
describe('Mustache.parse', function () {
5761

5862
for (var template in expectations) {
@@ -135,4 +139,15 @@ describe('Mustache.parse', function () {
135139
});
136140
});
137141

142+
describe('when parsing a template with the same tags second time, return the cached tokens', function () {
143+
it('returns the same tokens for the latter parse', function () {
144+
var template = '{{foo}}[bar]';
145+
var parsedResult1 = Mustache.parse(template);
146+
var parsedResult2 = Mustache.parse(template);
147+
148+
assert.deepEqual(parsedResult1, parsedResult2);
149+
assert.ok(parsedResult1 === parsedResult2);
150+
});
151+
});
152+
138153
});

0 commit comments

Comments
 (0)