Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set template cache key #486

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Fluid.ViewEngine/FluidViewEngineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ public class FluidViewEngineOptions
/// Gets or sets the delegate to execute when a view is rendered.
/// </summary>
public RenderingViewDelegate RenderingViewAsync { get; set; }

/// <summary>
/// Represents the method that will generate a cache key for a supplied template path. Allows templates to be cached based on any external factor.
/// </summary>
public delegate string TemplateCacheKeyProviderDelegate(string path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's a good idea to have a delegate, I'm thinking of providing another API for caching or using IMemoryCache where the cache key could be provided by the user


/// <summary>
/// Gets or sets the delegate to execute when a template cache key is required
/// </summary>
public TemplateCacheKeyProviderDelegate TemplateCacheKeyProvider { get; set; }
}
}
16 changes: 11 additions & 5 deletions Fluid.ViewEngine/FluidViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Fluid.ViewEngine
public class FluidViewRenderer : IFluidViewRenderer
{
private static readonly char[] PathSeparators = { '/', '\\' };

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

private record struct LayoutKey (string ViewPath, string LayoutPath);

private class CacheEntry
Expand Down Expand Up @@ -95,7 +94,7 @@ protected virtual List<string> FindViewStarts(string viewPath, IFileProvider fil
{
var viewStarts = new List<string>();
int index = viewPath.Length - 1;

while (!String.IsNullOrEmpty(viewPath))
{
if (index == -1)
Expand Down Expand Up @@ -217,14 +216,21 @@ protected virtual async ValueTask<IFluidTemplate> 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;
}
Expand All @@ -239,7 +245,7 @@ protected virtual async ValueTask<IFluidTemplate> ParseLiquidFileAsync(string pa
}

var subTemplates = new List<IFluidTemplate>();

if (includeViewStarts)
{
// Add ViewStart files
Expand Down