|
| 1 | +using Microsoft.AspNetCore.Mvc.TagHelpers; |
| 2 | +using Microsoft.AspNetCore.Razor.TagHelpers; |
| 3 | +using Our.Umbraco.TagHelpers.CacheKeys; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Encodings.Web; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Umbraco.Cms.Core; |
| 11 | +using Umbraco.Cms.Core.Cache; |
| 12 | +using Umbraco.Cms.Core.Web; |
| 13 | +using Umbraco.Extensions; |
| 14 | + |
| 15 | +namespace Our.Umbraco.TagHelpers |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// A wrapper around .net core CacheTagHelper so you remember not to cache in preview or Umbraco debug mode |
| 19 | + /// Also can create a new variance of the cache when anything is published in Umbraco if that is desirable |
| 20 | + /// </summary> |
| 21 | + [HtmlTargetElement("our-cache")] |
| 22 | + public class UmbracoCacheTagHelper : CacheTagHelper |
| 23 | + { |
| 24 | + private readonly IUmbracoContextFactory _umbracoContextFactory; |
| 25 | + private readonly IAppPolicyCache _runtimeCache; |
| 26 | + // default to true, a very 'Umbraco' convention. |
| 27 | + private bool _updateCacheKeyOnPublish = true; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Whether to update the cache key when any content, media, dictionary item is published in Umbraco. |
| 31 | + /// </summary> |
| 32 | + public bool UpdateCacheKeyOnPublish |
| 33 | + { |
| 34 | + get { return _updateCacheKeyOnPublish; } |
| 35 | + set { _updateCacheKeyOnPublish = value; } |
| 36 | + } |
| 37 | + |
| 38 | + public UmbracoCacheTagHelper(CacheTagHelperMemoryCacheFactory factory, HtmlEncoder htmlEncoder, AppCaches appCaches, IUmbracoContextFactory umbracoContextFactory) : base(factory, htmlEncoder) |
| 39 | + { |
| 40 | + _umbracoContextFactory = umbracoContextFactory; |
| 41 | + _runtimeCache = appCaches.RuntimeCache; |
| 42 | + } |
| 43 | + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
| 44 | + { |
| 45 | + using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext()) |
| 46 | + { |
| 47 | + var umbracoContext = umbracoContextReference.UmbracoContext; |
| 48 | + // we don't want to enable the cache tag helper if Umbraco is in Preview, or in Debug mode |
| 49 | + if (umbracoContext.InPreviewMode || umbracoContext.IsDebug) |
| 50 | + { |
| 51 | + this.Enabled = false; |
| 52 | + await output.GetChildContentAsync(); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + // with the CacheTagHelper we are wrapping here it's really difficult to clear the cache of the Tag Helper output 'on demand' |
| 57 | + // eg when a page is published, with Umbraco's CachedPartial that's what happens, so if you change the name of a page, and the site navigation |
| 58 | + // is cached with a cached partial, the cache is automatically cleared. |
| 59 | + // it seems for CacheTagHelper the .net core advice when you want to break the cache is to update the varyby key, the previous key in the cache will be forgotten |
| 60 | + // and fall out of the cache naturally... (but I did later on find this article: https://www.umbrajobs.com/blog/posts/2021/june/umbraco-9-net-core-caching-part-1-cashing-shared-partial-views/ |
| 61 | + // where it talks about being able to clear the actual cache tag using reflection.. - it won't work on loadbalanced servers - using wrong notifications! |
| 62 | + // ... so maybe that's the way people will ultimately prefer to go... |
| 63 | + // but for this tag helper we just track the last time a peace of content, dictionary item or media was published, and use that datetime in the varyby cachekey |
| 64 | + // so everytime something is published in Umbraco, the cache tag helper will have a different cache key and this will produce a new cached result |
| 65 | + // this might be a bad thing? |
| 66 | + // so we have a setting to turn this off, so the tag helper is still usable as the existing .net core cache tag helper, without caching in preview or umbraco debug |
| 67 | + // which is still handyish |
| 68 | + if (_updateCacheKeyOnPublish) |
| 69 | + { |
| 70 | + // ironically read the last cache refresh date from runtime cache, and set it to now if it's not there... |
| 71 | + var umbLastCacheRefreshCacheKey = _runtimeCache.GetCacheItem(CacheKeyConstants.LastCacheRefreshDateKey, () => GetFallbackCacheRefreshDate()).ToString(); |
| 72 | + // append to VaryBy key incase VaryBy key is set to some other parameter too |
| 73 | + this.VaryBy = umbLastCacheRefreshCacheKey + "|" + this.VaryBy; |
| 74 | + // if an expiry date isn't set when using the CacheTagHelper, let's add one to be 24hrs, so when mulitple publishes occur, the versions of this taghelper don't hang around forever |
| 75 | + if (this.ExpiresAfter == null) |
| 76 | + { |
| 77 | + this.ExpiresAfter = new TimeSpan(24, 0, 0); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + await base.ProcessAsync(context, output); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + private string GetFallbackCacheRefreshDate() |
| 87 | + { |
| 88 | + //this fires if the 'appcache' doesn't have a LastCacheRefreshDate set by a publish |
| 89 | + // eg after an app pool recycle |
| 90 | + // it doesn't really matter that this isn't the last datetime that something was actually published |
| 91 | + // because time tends to always move forwards |
| 92 | + // the next publish will set a new future LastCacheRefreshDate... |
| 93 | + return DateTime.UtcNow.ToString("s"); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | +} |
0 commit comments