Skip to content

Commit

Permalink
[cache] enable to use filesystem cache for razorlight
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Jan 21, 2025
1 parent c17cc87 commit 862c99f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ namespace SBSharp.Core.Configuration;
public class SBSharpConfiguration
{
[Description("Input configuration.")]
public InputConfiguration Input { get; set; } = new InputConfiguration();
public InputConfiguration Input { get; set; } = new();

[Description("Build configuration.")]
public BuildConfiguration Build { get; set; } = new();

[Description("Output configuration.")]
public OutputConfiguration Output { get; set; } = new OutputConfiguration();
public OutputConfiguration Output { get; set; } = new();

[Description(
"Post-processing configuration, often enables to work on the generated content to optimize it. "
Expand All @@ -18,10 +21,10 @@ public class SBSharpConfiguration
public PostProcessingConfiguration[] PostProcessing { get; set; } = [];

[Description("Watch specific configuration - `serve`and `watch` commands only.")]
public WatchConfiguration Watch { get; set; } = new WatchConfiguration();
public WatchConfiguration Watch { get; set; } = new();

[Description("Serve specific configuration - `serve` command only.")]
public ServeConfiguration Serve { get; set; } = new ServeConfiguration();
public ServeConfiguration Serve { get; set; } = new();

[Description("Tasks ran after the generation.")]
public class PostProcessingConfiguration
Expand Down Expand Up @@ -54,6 +57,16 @@ public class WatchConfiguration
public int Debouncing { get; set; } = 250;
}


[Description("Build specific configuration.")]
public class BuildConfiguration
{
[Description(
"If not empty, where to cache compiled templates for faster launch."
)]
public string RazorLocalCache { get; set; } = string.Empty;
}

[Description("Serve command specific configuration.")]
public class ServeConfiguration
{
Expand Down
10 changes: 9 additions & 1 deletion src/SBSharp.Core/SBSharp/Core/View/ViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using RazorLight;
using RazorLight.Caching;
using RazorLight.Compilation;
using SBSharp.Core.Configuration;

Expand All @@ -27,7 +28,14 @@ public ViewRenderer(SBSharpConfiguration configuration, ILogger<ViewRenderer> lo
{
EnableDebugMode = false
});
if (!this.configuration.Serve.WatchEnabled)
if (!string.IsNullOrEmpty(configuration.Build.RazorLocalCache))
{
builder.UseCachingProvider(new FileSystemCachingProvider(
root,
this.configuration.Build.RazorLocalCache,
new FileHashCachingStrategy()));
}
else if (!this.configuration.Serve.WatchEnabled)
{
builder.UseMemoryCachingProvider();
}
Expand Down
75 changes: 59 additions & 16 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 SBSharp.Core.Configuration;
using SBSharp.Tests.Temp;

namespace SBSharp.Core.View;
Expand All @@ -12,7 +13,7 @@ public async Task Render()
{
using var baseDir = new TempFolder("ViewRendererTests-Render");
var views = Directory.CreateDirectory(Path.Combine(baseDir.Value, "views"));
File.WriteAllText(Path.Combine(views.FullName, "tpl.cshtml"), "will be @(Model.Slug).html");
await File.WriteAllTextAsync(Path.Combine(views.FullName, "tpl.cshtml"), "will be @(Model.Slug).html");

using var factory = new NullLoggerFactory();
var html = await new ViewRenderer(
Expand All @@ -27,22 +28,64 @@ public async Task Render()
new Logger<ViewRenderer>(factory)
).RenderAsync(
"tpl",
new Page( // fake a loaded model, we just use the slug here
ImmutableDictionary<string, string>.Empty,
new NAsciidoc.Model.Document(
new NAsciidoc.Model.Header(
"",
null,
null,
ImmutableDictionary<string, string>.Empty
),
new NAsciidoc.Model.Body([])
),
() => "Content",
"some-sample-file",
new Page.GlobalContext { Pages = [] }
)
NewPage()
);
Assert.Equal("will be some-sample-file.html", html);
}

[Fact]
public async Task EnableViewCache()
{
using var baseDir = new TempFolder("ViewRendererTests-EnableViewCache");
var views = Directory.CreateDirectory(Path.Combine(baseDir.Value, "views"));
await File.WriteAllTextAsync(Path.Combine(views.FullName, "tpl.cshtml"), "will be @(Model.Slug).html");

var cache = Path.Combine(baseDir.Value, ".cache");

using var factory = new NullLoggerFactory();
for (var i = 0; i < 2; i++) // ensure reusing it works
{
var html = await new ViewRenderer(
new SBSharpConfiguration
{
Build = new SBSharpConfiguration.BuildConfiguration
{
RazorLocalCache = cache
},
Input = new SBSharpConfiguration.InputConfiguration
{
Location = baseDir.Value,
View = "views"
}
},
new Logger<ViewRenderer>(factory)
).RenderAsync(
"tpl",
NewPage()
);
Assert.Equal("will be some-sample-file.html", html);

var compiled = Path.Combine(cache, "db4c8ac4aea85fd63a61216cfc41d1dc.dll");
Assert.True(File.Exists(compiled), compiled);
}
}

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(
"",
null,
null,
ImmutableDictionary<string, string>.Empty
),
new NAsciidoc.Model.Body([])
),
() => "Content",
"some-sample-file",
new Page.GlobalContext { Pages = [] }
);
}
}

0 comments on commit 862c99f

Please sign in to comment.