-
-
Save rajurh/c95bdf8d9ea6a383ca1a6df9604569d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
public string UsingTemplateFromEmbedded<T>(string path, T model) | |
{ | |
// Generate the template name | |
string templateName = GetTemplateName(path); | |
string template = memoryCache.Get<string>(templateName); | |
if(string.IsNullOrEmpty(template)) | |
{ | |
// Locking this resource is necessary since there are parallel requests to this methnod | |
// and we need to cache it only once | |
lock (__lockObj) | |
{ | |
template = memoryCache.GetOrCreate(templateName, f => | |
{ | |
string resource = EmbeddedResourceHelper.GetResourceAsString(_assembly, GenerateFileAssemblyPath(path, _assembly)); | |
return resource; | |
}); | |
} | |
} | |
var result = Parse(templateName, template, model); | |
return result; | |
} | |
... | |
private async Task<string> ParseAsync<T>(string templateName, string template, T model) | |
{ | |
var project = new InMemoryRazorLightProject(); | |
var engine = new EngineFactory().Create(project); | |
//We need to set the templateName as unique Key for caching the template | |
return await engine.CompileRenderAsync<T>(templateName, template, model); | |
} | |
... | |
//This function is generating the template cache key from the template path | |
private string GetTemplateName(string path) | |
{ | |
return string.IsNullOrEmpty(path) ? "" : path.Replace(".", ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment