-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Website Rendering: Add configurable output caching for template rendered pages #22338
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ab81f38
Configuration for website output cache settings.
AndyButland b77d9d4
Interfaces and default implementation for extension points.
AndyButland 27011c8
Configure the output cache policy.
AndyButland 2c38b15
Evict cached documents through updates to related documents, media an…
AndyButland 67253df
Feedback from code review.
AndyButland 4b6e350
Merge branch 'main' into v17/feature/configurable-output-cache
AndyButland 8869b15
Update description of service registration in IWebsiteOutputCacheDura…
AndyButland 20cc6d9
Merge branch 'v17/feature/configurable-output-cache' of https://githu…
AndyButland 385da09
Use output cache over service provider.
AndyButland cfd2973
Optimise and DRY-up eviction handlers.
AndyButland 3fbee32
Only register IWebsiteOutputCacheManager when the feature is enabled.
AndyButland 1d4f864
Remove unnecessary check on applying output cache to Umbraco pipeline.
AndyButland 958bec9
Add extension point for determining if requests should be cached.
AndyButland 4619e32
Merge branch 'main' into v17/feature/configurable-output-cache
AndyButland c5e2c2a
Broken up large method in DocumentOutputCacheEvictionHandler, put ena…
AndyButland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Umbraco.Core/Cache/IWebsiteOutputCacheDurationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Provides custom cache durations per content item for website output caching. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Single registration — customers replace the default implementation to vary duration | ||
| /// by content type, path, or content properties. | ||
| /// </para> | ||
| /// <para> | ||
| /// Return <c>null</c> to use the configured default duration. | ||
| /// Return <see cref="TimeSpan.Zero"/> to disable caching for the content item. | ||
| /// </para> | ||
|
AndyButland marked this conversation as resolved.
Outdated
|
||
| /// </remarks> | ||
| public interface IWebsiteOutputCacheDurationProvider | ||
| { | ||
| /// <summary> | ||
| /// Returns a custom cache duration for the given content, or <c>null</c> to use the configured default. | ||
| /// </summary> | ||
| /// <param name="content">The published content being cached.</param> | ||
| /// <returns> | ||
| /// A custom cache duration, <see cref="TimeSpan.Zero"/> to disable caching for this content, | ||
| /// or <c>null</c> to use the configured default duration. | ||
| /// </returns> | ||
| TimeSpan? GetDuration(IPublishedContent content); | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
src/Umbraco.Core/Cache/IWebsiteOutputCacheEvictionProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Provides additional cache tags to evict when content changes. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Multiple implementations can be registered. The eviction handler iterates all providers | ||
| /// to collect additional tags to evict beyond the built-in content key tag. | ||
| /// </para> | ||
| /// <para> | ||
| /// Works as a pair with <see cref="IWebsiteOutputCacheTagProvider"/>: the tag provider adds | ||
| /// custom tags when caching a response, and this provider maps content changes back to those | ||
| /// tags at eviction time. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface IWebsiteOutputCacheEvictionProvider | ||
| { | ||
| /// <summary> | ||
| /// Returns additional cache tags to evict when the specified content changes. | ||
| /// </summary> | ||
| /// <param name="context">Details of the content change.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| /// <returns>Additional cache tags to evict.</returns> | ||
| Task<IEnumerable<string>> GetAdditionalEvictionTagsAsync(OutputCacheContentChangedContext context, CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Provides programmatic eviction of website output cache entries. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Use this to evict cached pages from custom code, for example when external data changes | ||
| /// that affects a rendered page. All methods are no-ops when output caching is not enabled. | ||
| /// </remarks> | ||
| public interface IWebsiteOutputCacheManager | ||
| { | ||
| /// <summary> | ||
| /// Evicts the cached page for a specific content item. | ||
| /// </summary> | ||
| /// <param name="contentKey">The key of the content item to evict.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| Task EvictContentAsync(Guid contentKey, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Evicts all cached pages matching a custom tag. | ||
| /// </summary> | ||
| /// <param name="tag">The cache tag to evict.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| Task EvictByTagAsync(string tag, CancellationToken cancellationToken = default); | ||
|
|
||
| /// <summary> | ||
| /// Evicts all cached website pages. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| Task EvictAllAsync(CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Provides additional cache tags when caching a website content page. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Multiple implementations can be registered; the output cache policy iterates all of them | ||
| /// to collect tags at cache-write time. Works as a pair with <see cref="IWebsiteOutputCacheEvictionProvider"/>: | ||
| /// the tag provider adds custom tags when caching, and the eviction provider maps content changes | ||
| /// back to those tags at eviction time. | ||
| /// </remarks> | ||
| public interface IWebsiteOutputCacheTagProvider | ||
| { | ||
| /// <summary> | ||
| /// Returns additional cache tags for the given published content. | ||
| /// </summary> | ||
| /// <param name="content">The published content being cached.</param> | ||
| /// <returns>Additional cache tags to associate with the cached response.</returns> | ||
| IEnumerable<string> GetTags(IPublishedContent content); | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/Umbraco.Core/Cache/OutputCacheContentChangedContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| namespace Umbraco.Cms.Core.Cache; | ||
|
|
||
| /// <summary> | ||
| /// Describes a content change that triggered output cache eviction. | ||
| /// </summary> | ||
| public class OutputCacheContentChangedContext | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="OutputCacheContentChangedContext"/> class. | ||
| /// </summary> | ||
| /// <param name="contentId">The integer ID of the content that changed.</param> | ||
| /// <param name="contentKey">The key of the content that changed.</param> | ||
| /// <param name="publishedCultures">The cultures that were published, if applicable.</param> | ||
| /// <param name="unpublishedCultures">The cultures that were unpublished, if applicable.</param> | ||
| public OutputCacheContentChangedContext(int contentId, Guid contentKey, IReadOnlyCollection<string> publishedCultures, IReadOnlyCollection<string> unpublishedCultures) | ||
| { | ||
| ContentId = contentId; | ||
| ContentKey = contentKey; | ||
| PublishedCultures = publishedCultures; | ||
| UnpublishedCultures = unpublishedCultures; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the integer ID of the content that changed. | ||
| /// </summary> | ||
| public int ContentId { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the key of the content that changed. | ||
| /// </summary> | ||
| public Guid ContentKey { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the cultures that were published, if applicable. Empty for invariant content. | ||
| /// </summary> | ||
| public IReadOnlyCollection<string> PublishedCultures { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the cultures that were unpublished, if applicable. Empty for invariant content. | ||
| /// </summary> | ||
| public IReadOnlyCollection<string> UnpublishedCultures { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System.ComponentModel; | ||
|
|
||
| namespace Umbraco.Cms.Core.Configuration.Models; | ||
|
|
||
| /// <summary> | ||
| /// Typed configuration options for website rendering settings. | ||
| /// </summary> | ||
| [UmbracoOptions(Constants.Configuration.ConfigWebsite)] | ||
| public class WebsiteSettings | ||
| { | ||
| private const bool StaticEnabled = false; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the output cache settings for website rendering. | ||
| /// </summary> | ||
| public OutputCacheSettings OutputCache { get; set; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// Typed configuration options for output caching of website rendering. | ||
| /// </summary> | ||
| public class OutputCacheSettings | ||
| { | ||
| private const string StaticDuration = "00:00:10"; // Ten seconds. | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value indicating whether website output caching is enabled. | ||
| /// </summary> | ||
| /// <value><c>true</c> if website output caching should be enabled; otherwise, <c>false</c>.</value> | ||
| /// <remarks> | ||
| /// The default value is <c>false</c>. | ||
| /// </remarks> | ||
| [DefaultValue(StaticEnabled)] | ||
| public bool Enabled { get; set; } = StaticEnabled; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the duration for which rendered content pages are cached. | ||
| /// </summary> | ||
| /// <value>Cache lifetime.</value> | ||
| /// <remarks> | ||
| /// The default cache duration is ten seconds, if this configuration value is not provided. | ||
| /// Can be overridden per content item via <c>IWebsiteOutputCacheDurationProvider</c>. | ||
| /// </remarks> | ||
| [DefaultValue(StaticDuration)] | ||
| public TimeSpan ContentDuration { get; set; } = TimeSpan.Parse(StaticDuration); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| namespace Umbraco.Cms.Core; | ||
|
|
||
| public static partial class Constants | ||
| { | ||
| /// <summary> | ||
| /// Constants related to the website rendering pipeline. | ||
| /// </summary> | ||
| public static class Website | ||
| { | ||
| /// <summary> | ||
| /// Constants related to website output caching. | ||
| /// </summary> | ||
| public static class OutputCache | ||
| { | ||
| /// <summary> | ||
| /// The named output cache policy for Umbraco website content. | ||
| /// </summary> | ||
| public const string ContentCachePolicy = "UmbracoWebsiteContent"; | ||
|
|
||
| /// <summary> | ||
| /// Tag prefix for a specific content item (followed by the content key). | ||
| /// </summary> | ||
| public const string ContentTagPrefix = "umb-content-"; | ||
|
|
||
| /// <summary> | ||
| /// Tag prefix for ancestor-based eviction (followed by the ancestor content key). | ||
| /// </summary> | ||
| public const string AncestorTagPrefix = "umb-content-ancestor-"; | ||
|
|
||
| /// <summary> | ||
| /// Tag prefix for content type-based eviction (followed by the content type alias). | ||
| /// </summary> | ||
| public const string ContentTypeTagPrefix = "umb-content-type-"; | ||
|
|
||
| /// <summary> | ||
| /// Tag that matches all cached website content pages. | ||
| /// </summary> | ||
| public const string AllContentTag = "umb-content-all"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Umbraco.Web.Website/Caching/ContentTypeOutputCacheTagProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Cache; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| namespace Umbraco.Cms.Web.Website.Caching; | ||
|
|
||
| /// <summary> | ||
| /// Tags cached pages with their content type alias, enabling eviction by content type. | ||
| /// </summary> | ||
| internal sealed class ContentTypeOutputCacheTagProvider : IWebsiteOutputCacheTagProvider | ||
| { | ||
| /// <inheritdoc /> | ||
| public IEnumerable<string> GetTags(IPublishedContent content) | ||
| { | ||
| yield return Constants.Website.OutputCache.ContentTypeTagPrefix + content.ContentType.Alias; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Umbraco.Web.Website/Caching/DefaultWebsiteOutputCacheDurationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Umbraco.Cms.Core.Cache; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
|
|
||
| namespace Umbraco.Cms.Web.Website.Caching; | ||
|
|
||
| /// <summary> | ||
| /// Default implementation of <see cref="IWebsiteOutputCacheDurationProvider"/> that always returns <c>null</c>, | ||
| /// deferring to the configured <c>ContentDuration</c> for all content. | ||
| /// </summary> | ||
| internal sealed class DefaultWebsiteOutputCacheDurationProvider : IWebsiteOutputCacheDurationProvider | ||
| { | ||
| /// <inheritdoc /> | ||
| public TimeSpan? GetDuration(IPublishedContent content) => null; | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Umbraco.Web.Website/Caching/IWebsiteOutputCacheVaryByProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.OutputCaching; | ||
|
|
||
| namespace Umbraco.Cms.Web.Website.Caching; | ||
|
|
||
| /// <summary> | ||
| /// Configures vary-by rules for website output caching. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// Multiple implementations can be registered; the output cache policy invokes all of them | ||
| /// to configure vary-by rules at cache-write time. | ||
| /// </para> | ||
| /// <para> | ||
| /// Providers have direct access to <see cref="CacheVaryByRules"/> and can configure any aspect | ||
| /// including <see cref="CacheVaryByRules.QueryKeys"/>, <see cref="CacheVaryByRules.HeaderNames"/>, | ||
| /// and <see cref="CacheVaryByRules.VaryByValues"/>. | ||
| /// </para> | ||
| /// </remarks> | ||
| public interface IWebsiteOutputCacheVaryByProvider | ||
| { | ||
| /// <summary> | ||
| /// Configures vary-by rules for the given request. | ||
| /// </summary> | ||
| /// <param name="context">The HTTP context for the current request.</param> | ||
| /// <param name="rules">The vary-by rules to configure.</param> | ||
| void ConfigureVaryBy(HttpContext context, CacheVaryByRules rules); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.