|
9 | 9 |
|
10 | 10 | namespace Our.Umbraco.TagHelpers
|
11 | 11 | {
|
12 |
| - /// <summary> |
13 |
| - /// A wrapper around .net core CacheTagHelper so you remember not to cache in preview or Umbraco debug mode |
14 |
| - /// Also can create a new variance of the cache when anything is published in Umbraco if that is desirable |
15 |
| - /// </summary> |
16 |
| - [HtmlTargetElement("our-cache")] |
17 |
| - public class UmbracoCacheTagHelper : CacheTagHelper |
| 12 | + /// <summary> |
| 13 | + /// A wrapper around .net core CacheTagHelper so you remember not to cache in preview or Umbraco debug mode |
| 14 | + /// Also can create a new variance of the cache when anything is published in Umbraco if that is desirable |
| 15 | + /// </summary> |
| 16 | + [HtmlTargetElement("our-cache")] |
| 17 | + public class UmbracoCacheTagHelper : CacheTagHelper |
18 | 18 | {
|
19 |
| - private readonly IUmbracoContextFactory _umbracoContextFactory; |
20 |
| - private readonly IUmbracoTagHelperCacheKeys _cacheKeys; |
| 19 | + private readonly IUmbracoContextFactory _umbracoContextFactory; |
| 20 | + private readonly IUmbracoTagHelperCacheKeys _cacheKeys; |
21 | 21 |
|
22 |
| - /// <summary> |
23 |
| - /// Whether to update the cache key when any content, media, dictionary item is published in Umbraco. |
24 |
| - /// </summary> |
25 |
| - public bool UpdateCacheKeyOnPublish { get; set; } = true; |
| 22 | + /// <summary> |
| 23 | + /// Whether to update the cache key when any content, media, dictionary item is published in Umbraco. |
| 24 | + /// </summary> |
| 25 | + public bool UpdateCacheKeyOnPublish { get; set; } = true; |
26 | 26 |
|
27 |
| - public UmbracoCacheTagHelper(CacheTagHelperMemoryCacheFactory factory, |
28 |
| - HtmlEncoder htmlEncoder, |
29 |
| - IUmbracoContextFactory umbracoContextFactory, |
30 |
| - IUmbracoTagHelperCacheKeys cacheKeys) |
31 |
| - : base(factory, htmlEncoder) |
32 |
| - { |
33 |
| - _umbracoContextFactory = umbracoContextFactory; |
34 |
| - _cacheKeys = cacheKeys; |
35 |
| - } |
| 27 | + public UmbracoCacheTagHelper(CacheTagHelperMemoryCacheFactory factory, |
| 28 | + HtmlEncoder htmlEncoder, |
| 29 | + IUmbracoContextFactory umbracoContextFactory, |
| 30 | + IUmbracoTagHelperCacheKeys cacheKeys) |
| 31 | + : base(factory, htmlEncoder) |
| 32 | + { |
| 33 | + _umbracoContextFactory = umbracoContextFactory; |
| 34 | + _cacheKeys = cacheKeys; |
| 35 | + } |
36 | 36 |
|
37 |
| - public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
38 |
| - { |
39 |
| - using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext()) |
40 |
| - { |
41 |
| - var umbracoContext = umbracoContextReference.UmbracoContext; |
| 37 | + public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) |
| 38 | + { |
| 39 | + using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext()) |
| 40 | + { |
| 41 | + var umbracoContext = umbracoContextReference.UmbracoContext; |
42 | 42 |
|
43 |
| - // we don't want to enable the cache tag helper if Umbraco is in Preview, or in Debug mode |
44 |
| - if (umbracoContext.InPreviewMode || umbracoContext.IsDebug) |
45 |
| - { |
46 |
| - // Set the endabled flag to false & lest base class |
47 |
| - // of the cache tag helper do the same stuff as before |
48 |
| - this.Enabled = false; |
49 |
| - } |
50 |
| - else |
51 |
| - { |
52 |
| - // Defaults to true - have to explicitly opt out with attribute set to false in Razor |
53 |
| - if (UpdateCacheKeyOnPublish) |
54 |
| - { |
| 43 | + // we don't want to enable the cache tag helper if Umbraco is in Preview, or in Debug mode |
| 44 | + if (umbracoContext.InPreviewMode || umbracoContext.IsDebug) |
| 45 | + { |
| 46 | + // Set the endabled flag to false & lest base class |
| 47 | + // of the cache tag helper do the same stuff as before |
| 48 | + this.Enabled = false; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + // Defaults to true - have to explicitly opt out with attribute set to false in Razor |
| 53 | + if (UpdateCacheKeyOnPublish) |
| 54 | + { |
55 | 55 | // So before we go into our base class
|
56 | 56 | // Grab the cache key so we can keep track of it & put into some dictionary or collection
|
57 | 57 | // and clear all items out in that collection with our notifications on publish
|
58 | 58 |
|
59 | 59 | var cacheKey = new CacheTagKey(this, context);
|
60 | 60 | var key = cacheKey.GenerateKey();
|
61 | 61 | var hashedKey = cacheKey.GenerateHashedKey();
|
62 |
| - _cacheKeys.CacheKeys.TryAdd(key, cacheKey); |
| 62 | + _cacheKeys.CacheKeys.TryAdd(key, cacheKey); |
63 | 63 | }
|
64 |
| - } |
| 64 | + } |
65 | 65 |
|
66 | 66 | await base.ProcessAsync(context, output);
|
67 | 67 | }
|
68 |
| - } |
69 |
| - } |
| 68 | + } |
| 69 | + } |
70 | 70 | }
|
0 commit comments