Skip to content

Commit

Permalink
Added template caching for the view engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiamond committed Mar 6, 2012
1 parent f9c6aa2 commit 466b6e0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Nustache.Mvc3/NustacheView.cs
@@ -1,4 +1,5 @@
using System.IO;
using System.Web.Caching;
using System.Web.Mvc;
using Nustache.Core;

Expand Down Expand Up @@ -57,11 +58,20 @@ public void Render(ViewContext viewContext, TextWriter writer)

private Template GetTemplate(string path)
{
// TODO: Add caching?
var key = "Nustache:" + path;

if (_controllerContext.HttpContext.Cache[key] != null)
{
return (Template)_controllerContext.HttpContext.Cache[key];
}

var templatePath = _controllerContext.HttpContext.Server.MapPath(path);
var templateSource = File.ReadAllText(templatePath);
var template = new Template();
template.Load(new StringReader(templateSource));

_controllerContext.HttpContext.Cache.Insert(key, template, new CacheDependency(templatePath));

return template;
}
}
Expand Down

0 comments on commit 466b6e0

Please sign in to comment.