jlamfers/RazorMachine

Question: Caching

Closed this issue · 5 comments

How do you cache a templates? Do you make the RazorMachine a static field?

The compiled template types (not the template instances) are cached within the RazorMachine instance. As a result that cache is instance bound. To get the best performance, i.e., reusing the cached compiled template types, you would use a shared or singleton RazorMachine instance. Indeed you could create a static field for that purpose.

Thanks for the response. I came up with the following code. The 2nd and 3rd
calls looks like it is using the precompiled template "~/mytemplate", is
that correct? So your engine does not recompile calls 2 and 3.

    static void TestRazorCache()
    {
        Xipton.Razor.RazorMachine rm =

CreateRazorMachineWithoutContentProviders();
rm.RegisterTemplate("~/mytemplate", "Razor says: Hello
@Model.FirstName @Model.LastName");

// Call 1
var template = rm.Execute("/mytemplate", new { FirstName
= "John", LastName = "Smith" });
Console.WriteLine(template.Result);
// Call 2
template = rm.Execute("
/mytemplate", new { FirstName =
"Ralph", LastName = "Lauren" });
Console.WriteLine(template.Result);

// Call 3

        template = rm.Execute("~/mytemplate", new { FirstName =

"Micheal", LastName = "Jackson" });
Console.WriteLine(template.Result);

    }

Thanks,

Manit

On Fri, Mar 8, 2013 at 3:03 AM, Jaap Lamfers notifications@github.comwrote:

The compiled template types (not the template instances) are cached within
the RazorMachine instance. As a result that cache is instance bound. To get
the best performance, i.e., reusing the cached compiled template types, you
would use a shared or singleton RazorMachine instance. Indeed you could
create a static field for that purpose.


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-14610097
.

Yes, you are fully correct. JIT compiling is applied all through the RazorMachine.This also applies for layouts, and even scripts that have not been registered by an url. All JIT compiled types are being cached and reused whenever their correponding templates are requested. On any script change the compiled type for that registered template is removed and recompiled on the first following request.

  var rm = new RazorMachine();
  var result = rm.Execute("Hello @Model.FirstName @Model.LastName", new { FirstName = "John", LastName = "Smith" }); // JIT compiled
  Console.WriteLine(result);
  result = rm.Execute("Hello @Model.FirstName @Model.LastName", new { FirstName = "Jimmy", LastName = "Hollyday" }); // no type compilation
  Console.WriteLine(result);

Works great. This is a very useful component. Thank you for your hard work.

  • Manit

On Sat, Mar 9, 2013 at 4:26 AM, Jaap Lamfers notifications@github.comwrote:

Yes, you are fully correct. JIT compiling is applied all through the
RazorMachine.This also applies for layouts, and even scripts that have not
been registered by an url. All JIT compiled types are being cached and
reused whenever their correponding templates are requested.

var rm = new RazorMachine();
var result = rm.Execute("Hello @firstname @lastname", new {FirstName="John", LastName="Smith"}); // JIT compiled
Console.Writeline(result);
result = rm.Execute("Hello @firstname @lastname", new {FirstName="Jimmy", LastName="Hollyday"}); // no type compilation
Console.Writeline(result);


Reply to this email directly or view it on GitHubhttps://github.com//issues/10#issuecomment-14660934
.

Thanks.