How to use cache with scriban when reading from a files

33 views Asked by At

I am currently working with .NET Core to render an HTML template from a file. How do use ITemplateLoader or CachedTemplates ? There are no full example of the implementation. Currently, I am using memory cache to load file to memory and process it like this:

    private async Task<string> GetTemplates()
    {
        string fileName = "email-template.html";
        return await _memoryCache.GetOrCreateAsync<string>("Templates", async f =>
        {
            f.AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1);
            return await File.ReadAllTextAsync($".\\templates\\{fileName}");
        }) ?? throw new FileNotFoundException(fileName);
    }

    private async Task<string> ApplyTemplateAsync(SomeModel model)
    {
        var templateContent = await GetTemplates();

        var template = Template.Parse(templateContent);
        return template.Render(model);
    }
0

There are 0 answers