How to save template in cache?
See original GitHub issueDiscussed in https://github.com/toddams/RazorLight/discussions/484
<div type='discussions-op-text'>Originally posted by alekdavisintel May 31, 2022
The README example shows how to render template from cache, but how do you add it to cache? I.e. what should happen if the cacheResult.Success
is false
?
var cacheResult = engine.Handler.Cache.RetrieveTemplate("templateKey");
if(cacheResult.Success)
{
var templatePage = cacheResult.Template.TemplatePageFactory();
string result = await engine.RenderTemplateAsync(templatePage, model);
}
Here is my code outline (my templates come from strings):
string tempateKey = "XYZ";
string templateHtml = "SOME RASOR TEXT"; // Original text has @ placeholders
dynamic model = new ExpandoObject(); // Set some properties below not showing for briefness
var engine = new RazorLightEngineBuilder()
.UseMemoryCachingProvider()
.Build();
TemplateCacheLookupResult cacheResult = engine.Handler.Cache.RetrieveTemplate(templateKey);
if (cacheResult.Success)
{
// This code never executes.
var templatePage = cacheResult.Template.TemplatePageFactory();
result = await engine.RenderTemplateAsync(templatePage, model);
}
else
{
// This code always executes.
result = await engine.CompileRenderStringAsync(templateKey, templateHtml, model);
}
The code never gets into the if (cacheResult.Success)
block. What am I doing wrong or not doing at all?
Thanks!
</div>Issue Analytics
- State:
- Created a year ago
- Comments:22
Top Results From Across the Web
Saving a Workspace as a Template
Open a workspace and select File > Save As Template. The Save as Template dialog opens. You can edit the default location file...
Read more >How does template caching work?
The primary files to look at are Cache_Node.php in app/etc/templating/twigextensions and TemplateCacheService.php in app/services .
Read more >Documentation: Ultimate Guide to Template Module Caching
To do so, click the check box labeled “Module Caching” under the “Module Options” section. Then click “Save Settings.” Cache Options. Cache Expiration....
Read more >Extract Output template from Cache
If you switch to the Output Templates tab is the current set up shown there so you can rebuild it and save with...
Read more >C++ How to cache a variable of template type T in a class?
You need to create a cache object that is able to store instances of different types. Just as you have bar = bars->get<T>()...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So what I was seeing was that every generated html document added to the memory. Our simple fix was to register the memorycache as singleton, inject it into our service class and change .UseMemoryCachingProvider() to .UseCachingProvider(_provider). We are using Autofac
Your problem is you didn’t use a RazorLightProject. I thought I configured it to throw an exception if people did things this way. Long story short, lots of people copy-pasted code from some medium.com article that caused a lot of people bugs, so I “fixed” it to continue without the cache.
See the README - it is the first FAQ in the FAQ: https://github.com/toddams/RazorLight#how-to-use-templates-from-memory-without-setting-a-project