Skip to content

Commit

Permalink
[cache] use simple file strategy since hash one is buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Jan 21, 2025
1 parent dfc2a1b commit 9b8f061
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 12 additions & 7 deletions src/SBSharp.Core/SBSharp/Core/View/ViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ namespace SBSharp.Core.View;

public class ViewRenderer
{
private readonly SBSharpConfiguration configuration;
private readonly RazorLightEngine engine;
private readonly ConcurrentDictionary<string, Task<Func<ITemplatePage>>> cache = new();

public ViewRenderer(SBSharpConfiguration configuration, ILogger<ViewRenderer> logger)
{
this.configuration = configuration;

var root = Path.Combine(Directory.GetCurrentDirectory(), configuration.Input.Location,
configuration.Input.View);
logger.LogInformation("Using view directory '{Directory}'", root);
Expand All @@ -26,16 +23,18 @@ public ViewRenderer(SBSharpConfiguration configuration, ILogger<ViewRenderer> lo
.UseFileSystemProject(root)
.UseOptions(new RazorLightOptions
{
EnableDebugMode = false
EnableDebugMode = false,
PreRenderCallbacks = []
});
if (!string.IsNullOrEmpty(configuration.Build.RazorLocalCache))
{
builder.UseCachingProvider(new FileSystemCachingProvider(
root,
this.configuration.Build.RazorLocalCache,
new FileHashCachingStrategy()));
configuration.Build.RazorLocalCache,
// hash strategy assumes file exists so we can't use it the first time
new SimpleFileCachingStrategy()));
}
else if (!this.configuration.Serve.WatchEnabled)
else if (!configuration.Serve.WatchEnabled)
{
builder.UseMemoryCachingProvider();
}
Expand All @@ -50,6 +49,12 @@ public async Task<string> RenderAsync(string view, Page page)
{
var promise = cache.GetOrAdd(view, async k =>
{
// try in the cache first (filesystem case)
if (engine.Handler.IsCachingEnabled &&
engine.Handler.Cache.RetrieveTemplate(view) is { Success: true } result)
{
return result.Template.TemplatePageFactory;
}
var templateDescriptor = await engine.Handler.Compiler.CompileAsync(k);
return engine.Handler.FactoryProvider.CreateFactory(templateDescriptor);
});
Expand Down
13 changes: 7 additions & 6 deletions test/SBSharp.Tests/SBSharp/Core/View/ViewRendererTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NAsciidoc.Model;
using SBSharp.Core.Configuration;
using SBSharp.Tests.Temp;

Expand All @@ -17,9 +18,9 @@ public async Task Render()

using var factory = new NullLoggerFactory();
var html = await new ViewRenderer(
new Configuration.SBSharpConfiguration
new SBSharpConfiguration
{
Input = new Configuration.SBSharpConfiguration.InputConfiguration
Input = new SBSharpConfiguration.InputConfiguration
{
Location = baseDir.Value,
View = "views"
Expand Down Expand Up @@ -65,7 +66,7 @@ public async Task EnableViewCache()
);
Assert.Equal("will be some-sample-file.html", html);

var compiled = Path.Combine(cache, "db4c8ac4aea85fd63a61216cfc41d1dc.dll");
var compiled = Path.Combine(cache, "tpl.cshtml.dll");
Assert.True(File.Exists(compiled), compiled);
}
}
Expand All @@ -74,14 +75,14 @@ private static Page NewPage()
{
return new Page( // fake a loaded model, we just use the slug here
ImmutableDictionary<string, string>.Empty,
new NAsciidoc.Model.Document(
new NAsciidoc.Model.Header(
new Document(
new Header(
"",
null,
null,
ImmutableDictionary<string, string>.Empty
),
new NAsciidoc.Model.Body([])
new Body([])
),
() => "Content",
"some-sample-file",
Expand Down

0 comments on commit 9b8f061

Please sign in to comment.