toddams/RazorLight

About caching

cadilhac opened this issue · 5 comments

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

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.

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".

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.

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?

@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.