diff --git a/Fluid.ViewEngine/FluidViewEngineOptions.cs b/Fluid.ViewEngine/FluidViewEngineOptions.cs index 2a2122b4..d17b4e1d 100644 --- a/Fluid.ViewEngine/FluidViewEngineOptions.cs +++ b/Fluid.ViewEngine/FluidViewEngineOptions.cs @@ -74,5 +74,15 @@ public class FluidViewEngineOptions /// Gets or sets the delegate to execute when a view is rendered. /// public RenderingViewDelegate RenderingViewAsync { get; set; } + + /// + /// Represents the method that will generate a cache key for a supplied template path. Allows templates to be cached based on any external factor. + /// + public delegate string TemplateCacheKeyProviderDelegate(string path); + + /// + /// Gets or sets the delegate to execute when a template cache key is required + /// + public TemplateCacheKeyProviderDelegate TemplateCacheKeyProvider { get; set; } } } diff --git a/Fluid.ViewEngine/FluidViewRenderer.cs b/Fluid.ViewEngine/FluidViewRenderer.cs index 7d037794..784d8f13 100644 --- a/Fluid.ViewEngine/FluidViewRenderer.cs +++ b/Fluid.ViewEngine/FluidViewRenderer.cs @@ -15,7 +15,6 @@ namespace Fluid.ViewEngine public class FluidViewRenderer : IFluidViewRenderer { private static readonly char[] PathSeparators = { '/', '\\' }; - private record struct LayoutKey (string ViewPath, string LayoutPath); private class CacheEntry @@ -95,7 +94,7 @@ protected virtual List FindViewStarts(string viewPath, IFileProvider fil { var viewStarts = new List(); int index = viewPath.Length - 1; - + while (!String.IsNullOrEmpty(viewPath)) { if (index == -1) @@ -217,14 +216,21 @@ protected virtual async ValueTask GetFluidTemplateAsync(string p return cacheEntry; }); - if (cache.TemplateCache.TryGetValue(path, out var template)) + // Allow templates to be cached by external factors + string cacheKey = path; + if (_fluidViewEngineOptions.TemplateCacheKeyProvider != null) + { + cacheKey = _fluidViewEngineOptions.TemplateCacheKeyProvider.Invoke(path); + } + + if (cache.TemplateCache.TryGetValue(cacheKey, out var template)) { return template; } template = await ParseLiquidFileAsync(path, fileProvider, includeViewStarts); - cache.TemplateCache[path] = template; + cache.TemplateCache[cacheKey] = template; return template; } @@ -239,7 +245,7 @@ protected virtual async ValueTask ParseLiquidFileAsync(string pa } var subTemplates = new List(); - + if (includeViewStarts) { // Add ViewStart files