question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to save template in cache?

See original GitHub issue

Discussed 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:closed
  • Created a year ago
  • Comments:22

github_iconTop GitHub Comments

1reaction
dallasbeekcommented, Aug 19, 2022

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

register it
builder.RegisterType<MemoryCachingProvider>().As<ICachingProvider>().SingleInstance(); 

inject it
private readonly ICachingProvider _provider;

public DocumentService(ICachingProvider provider)
{
    _provider = provider;
}

use it
var engine = new RazorLightEngineBuilder()
        .UseFileSystemProject($"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/Views", string.Empty)
        .UseCachingProvider(_provider)
        .Build();
1reaction
jzabroskicommented, Aug 16, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found