marko/taglibs/caching/cached-fragment-tag.js
Phil Gates-Idem f45d89b18c Code cleanup
2014-07-17 00:22:28 -04:00

51 lines
1.4 KiB
JavaScript

'use strict';
var FRAGMENT_CACHE_CONFIG = {
store: 'memory'
};
var raptorCache;
module.exports = {
render: function (input, context) {
if (raptorCache === undefined) {
try {
raptorCache = require('raptor-cache');
}
catch(e) {
throw new Error('The "raptor-cache" module should be installed as an application-level dependency when using caching tags');
}
}
var cacheKey = input.cacheKey;
if (!cacheKey) {
throw new Error('cache-key is required for <cached-fragment>');
}
// use the default cache manager
var cacheManager = raptorCache.getDefaultCacheManager(context);
var cache = cacheManager.getCache(input.cacheName, FRAGMENT_CACHE_CONFIG);
var asyncContext = context.beginAsync();
cache.get(cacheKey,
{
builder: function() {
var result = context.captureString(function () {
if (input.invokeBody) {
input.invokeBody();
}
});
return result;
}
}, function(err, result) {
if (err) {
return asyncContext.error(err);
}
asyncContext.end(result);
});
}
};