diff --git a/API/GenHTTP.API/GenHTTP.Api.csproj b/API/GenHTTP.API/GenHTTP.Api.csproj index 523d666a..c296ec92 100644 --- a/API/GenHTTP.API/GenHTTP.Api.csproj +++ b/API/GenHTTP.API/GenHTTP.Api.csproj @@ -7,9 +7,9 @@ 8.0 enable - 2.1.0.0 - 2.1.0.0 - 2.1.0 + 2.2.0.0 + 2.2.0.0 + 2.2.0 Andreas Nägeli diff --git a/API/GenHTTP.API/Modules/IResourceProvider.cs b/API/GenHTTP.API/Modules/IResourceProvider.cs index 685565c9..97baf9a7 100644 --- a/API/GenHTTP.API/Modules/IResourceProvider.cs +++ b/API/GenHTTP.API/Modules/IResourceProvider.cs @@ -19,6 +19,16 @@ namespace GenHTTP.Api.Modules public interface IResourceProvider { + /// + /// Specifies, whether a consumer of this resource is allowed to + /// cache content derived from the resource. + /// + /// + /// For content which may change externally (e.g. files), this property + /// should return false. + /// + bool AllowCache { get; } + /// /// Returns the read-only stream of the resource to be accessed. /// diff --git a/Core/GenHTTP.Core/GenHTTP.Core.csproj b/Core/GenHTTP.Core/GenHTTP.Core.csproj index 4965c57a..e5039213 100644 --- a/Core/GenHTTP.Core/GenHTTP.Core.csproj +++ b/Core/GenHTTP.Core/GenHTTP.Core.csproj @@ -7,9 +7,9 @@ 8.0 enable - 2.1.0.0 - 2.1.0.0 - 2.1.0 + 2.2.0.0 + 2.2.0.0 + 2.2.0 Andreas Nägeli diff --git a/Modules/GenHTTP.Modules.Core/GenHTTP.Modules.Core.csproj b/Modules/GenHTTP.Modules.Core/GenHTTP.Modules.Core.csproj index 88abf281..7ca3d761 100644 --- a/Modules/GenHTTP.Modules.Core/GenHTTP.Modules.Core.csproj +++ b/Modules/GenHTTP.Modules.Core/GenHTTP.Modules.Core.csproj @@ -7,9 +7,9 @@ 8.0 enable - 2.1.0.0 - 2.1.0.0 - 2.1.0 + 2.2.0.0 + 2.2.0.0 + 2.2.0 Andreas Nägeli diff --git a/Modules/GenHTTP.Modules.Core/Resource/FileDataProvider.cs b/Modules/GenHTTP.Modules.Core/Resource/FileDataProvider.cs index b88d9b5f..12a40416 100644 --- a/Modules/GenHTTP.Modules.Core/Resource/FileDataProvider.cs +++ b/Modules/GenHTTP.Modules.Core/Resource/FileDataProvider.cs @@ -15,6 +15,8 @@ public class FileDataProvider : IResourceProvider public FileInfo File { get; } + public bool AllowCache => false; + #endregion #region Initialization diff --git a/Modules/GenHTTP.Modules.Core/Resource/ResourceDataProvider.cs b/Modules/GenHTTP.Modules.Core/Resource/ResourceDataProvider.cs index 925a382b..020f6dbd 100644 --- a/Modules/GenHTTP.Modules.Core/Resource/ResourceDataProvider.cs +++ b/Modules/GenHTTP.Modules.Core/Resource/ResourceDataProvider.cs @@ -19,6 +19,8 @@ public class ResourceDataProvider : IResourceProvider public string QualifiedName { get; } + public bool AllowCache => true; + #endregion #region Initialization diff --git a/Modules/GenHTTP.Modules.Core/Resource/StringDataProvider.cs b/Modules/GenHTTP.Modules.Core/Resource/StringDataProvider.cs index 3a57dfbf..2bbbfe65 100644 --- a/Modules/GenHTTP.Modules.Core/Resource/StringDataProvider.cs +++ b/Modules/GenHTTP.Modules.Core/Resource/StringDataProvider.cs @@ -15,6 +15,8 @@ public class StringDataProvider : IResourceProvider public string Content { get; } + public bool AllowCache => true; + #endregion #region Initialization diff --git a/Modules/GenHTTP.Modules.Core/Templating/PlaceholderRender.cs b/Modules/GenHTTP.Modules.Core/Templating/PlaceholderRender.cs index 1bbc746d..8d1efdf5 100644 --- a/Modules/GenHTTP.Modules.Core/Templating/PlaceholderRender.cs +++ b/Modules/GenHTTP.Modules.Core/Templating/PlaceholderRender.cs @@ -14,6 +14,8 @@ public class PlaceholderRender : IRenderer where T : IBaseModel { private readonly static Regex PLACEHOLDER = new Regex(@"\[([a-zA-Z0-9]+)\]"); + private string? _Template; + #region Get-/Setters public IResourceProvider TemplateProvider { get; } @@ -33,7 +35,7 @@ public PlaceholderRender(IResourceProvider templateProvider) public string Render(T model) { - var template = TemplateProvider.GetResourceAsString(); + var template = GetTemplate(); var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase; @@ -52,6 +54,21 @@ public string Render(T model) }); } + private string GetTemplate() + { + if (TemplateProvider.AllowCache) + { + return _Template ?? (_Template = LoadTemplate()); + } + + return LoadTemplate(); + } + + private string LoadTemplate() + { + return TemplateProvider.GetResourceAsString(); + } + #endregion } diff --git a/Modules/GenHTTP.Modules.Scriban/GenHTTP.Modules.Scriban.csproj b/Modules/GenHTTP.Modules.Scriban/GenHTTP.Modules.Scriban.csproj index 6e4c765b..e8b91011 100644 --- a/Modules/GenHTTP.Modules.Scriban/GenHTTP.Modules.Scriban.csproj +++ b/Modules/GenHTTP.Modules.Scriban/GenHTTP.Modules.Scriban.csproj @@ -7,9 +7,9 @@ 8.0 enable - 2.1.0.0 - 2.1.0.0 - 2.1.0 + 2.2.0.0 + 2.2.0.0 + 2.2.0 Andreas Nägeli diff --git a/Modules/GenHTTP.Modules.Scriban/ScribanPageProvider.cs b/Modules/GenHTTP.Modules.Scriban/ScribanPageProvider.cs index ce7eef02..1628cac8 100644 --- a/Modules/GenHTTP.Modules.Scriban/ScribanPageProvider.cs +++ b/Modules/GenHTTP.Modules.Scriban/ScribanPageProvider.cs @@ -22,6 +22,8 @@ public class ScribanPageProvider : IContentProvider where T : PageModel public ModelProvider ModelProvider { get; } + public ScribanRenderer Renderer { get; } + public string? Title { get; } #endregion @@ -33,6 +35,8 @@ public ScribanPageProvider(IResourceProvider templateProvider, ModelProvider TemplateProvider = templateProvider; ModelProvider = modelProvider; Title = title; + + Renderer = new ScribanRenderer(TemplateProvider); } #endregion @@ -43,11 +47,9 @@ public IResponseBuilder Handle(IRequest request) { if (request.HasType(RequestMethod.HEAD, RequestMethod.GET, RequestMethod.POST)) { - var renderer = new ScribanRenderer(TemplateProvider); - var model = ModelProvider(request); - var content = renderer.Render(model); + var content = Renderer.Render(model); var templateModel = new TemplateModel(request, model.Title ?? Title ?? "Untitled Page", content); diff --git a/Modules/GenHTTP.Modules.Scriban/ScribanRenderer.cs b/Modules/GenHTTP.Modules.Scriban/ScribanRenderer.cs index b50a574e..660e5247 100644 --- a/Modules/GenHTTP.Modules.Scriban/ScribanRenderer.cs +++ b/Modules/GenHTTP.Modules.Scriban/ScribanRenderer.cs @@ -14,6 +14,7 @@ namespace GenHTTP.Modules.Scriban public class ScribanRenderer : IRenderer where T : IBaseModel { + private Template? _Template; #region Get-/Setters @@ -34,9 +35,7 @@ public ScribanRenderer(IResourceProvider templateProvider) public string Render(T model) { - var content = TemplateProvider.GetResourceAsString(); - - var template = Template.Parse(content); + var template = GetTemplate(); var obj = new ScriptObject(); @@ -46,6 +45,21 @@ public string Render(T model) return template.Render(obj); } + private Template GetTemplate() + { + if (TemplateProvider.AllowCache) + { + return _Template ?? (_Template = LoadTemplate()); + } + + return LoadTemplate(); + } + + private Template LoadTemplate() + { + return Template.Parse(TemplateProvider.GetResourceAsString()); + } + #endregion }