toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.52k stars 259 forks source link

About caching #478

Closed cadilhac closed 2 years ago

cadilhac commented 2 years ago

Hi. This is not clear in the doc:

The quickstart section seems to imply that CompileRenderStringAsync must be used to first compile the template (and get the result). Then, if I want to benefit from the cache, I have to check if the template page exists and then render it. However, if I only use CompileRenderStringAsync, it is slow the first time and then fast in subsequent calls which means it internally checks for the cache. So, why not use CompileRenderStringAsync only? What is the use of RenderTemplateAsync?

Also, seems like to use a cache, I have to call UseMemoryCachingProvider. But if I don't call it, I get the same behaviour. First call is slows and then subsequent calls are fast. What if I don't call UseMemoryCachingProvider? Does it use another kind of cache?

Thanks

jzabroski commented 2 years ago

The cache is a compilation cache, so that items are not repeatedly compiled.

If you compile strings and those strings are not well-defined static input, you may cause an indirect memory leak / consume a very large cache footprint.

You are free to implement your own cache.

cadilhac commented 2 years ago

Thank you. But I think it does not answer my question. Whether I call UseMemoryCachingProvider or not, the first call is always costly and the subsequent ones are not, as if a cache was always used anyway.

Also I don't understand "If you compile strings and those strings are not well-defined static input".

jzabroski commented 2 years ago

C# is a language that targets a just-in-time runtime. The first call is always costly.

Also I don't understand "If you compile strings and those strings are not well-defined static input".

You should not use RazorLight by building string templates dynamically, and then passing that to RazorLight. String templates should be static content. You may store it in various ways, like in a database, but please don't do insane things like dynamically constructing strings.

cadilhac commented 2 years ago

ok, thanks for the advice. My templates are so small that it seems to be very fast. Let's be practical: what nasty "insane" things could happen if I do it dynamically?

jzabroski commented 2 years ago

@cadilhac None of your questions seem to be productively worded. Let's be practical and not keep asking basic questions. This is not a place for people to learn how to program.