Skip to content

Commit e630fb4

Browse files
author
Warren Buckley
committed
More trivial tidying up
1 parent 68bc9e9 commit e630fb4

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using Our.Umbraco.TagHelpers.CacheKeys;
22
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
73
using Umbraco.Cms.Core.Cache;
84
using Umbraco.Cms.Core.Events;
95
using Umbraco.Cms.Core.Notifications;
106

117
namespace Our.Umbraco.TagHelpers.Notifications
128
{
13-
// For Use with the Our Cache TagHelper - to store a last cache updated datetime to use in the varyby key for the Cache TagHelper, to naively break the cache on publish
9+
// For Use with the Our Cache TagHelper - to store a last cache updated datetime to use
10+
// in the varyby key for the Cache TagHelper, to naively break the cache on publish.
11+
// Used for ContentCacheRefresher (Load balanced scernarios not just Published event)
12+
// Same for Dictionary Items and Media item
1413
public class HandleContentCacheRefresherNotification : INotificationHandler<ContentCacheRefresherNotification>
1514
{
1615
private readonly IAppPolicyCache _runtimeCache;
@@ -20,52 +19,55 @@ public HandleContentCacheRefresherNotification(AppCaches appCaches)
2019
_runtimeCache = appCaches.RuntimeCache;
2120

2221
}
22+
2323
public void Handle(ContentCacheRefresherNotification notification)
2424
{
2525
// fired when content published
2626
// store DateTime, as the cachekey
27-
2827
var lastCacheRefreshDate = DateTime.UtcNow.ToString("s");
29-
//insert and override existing value in appcache
28+
29+
// insert and override existing value in appcache
3030
_runtimeCache.Insert(CacheKeyConstants.LastCacheRefreshDateKey, () => lastCacheRefreshDate, null, false, null);
3131

3232
}
3333
}
34+
3435
public class HandleDictionaryCacheRefresherNotification : INotificationHandler<DictionaryCacheRefresherNotification>
3536
{
3637
private readonly IAppPolicyCache _runtimeCache;
3738
public HandleDictionaryCacheRefresherNotification(AppCaches appCaches)
3839
{
3940
_runtimeCache = appCaches.RuntimeCache;
4041
}
42+
4143
public void Handle(DictionaryCacheRefresherNotification notification)
4244
{
4345
// fired when Dictionary item updated
4446
// store DateTime, as the cachekey
45-
4647
var lastCacheRefreshDate = DateTime.UtcNow.ToString("s");
47-
//insert and override existing value in appcache
48+
49+
// insert and override existing value in appcache
4850
_runtimeCache.Insert(CacheKeyConstants.LastCacheRefreshDateKey, () => lastCacheRefreshDate, null, false, null);
4951

5052
}
5153
}
54+
5255
public class HandleMediaCacheRefresherNotification : INotificationHandler<MediaCacheRefresherNotification>
5356
{
5457
private readonly IAppPolicyCache _runtimeCache;
5558
public HandleMediaCacheRefresherNotification(AppCaches appCaches)
5659
{
5760
_runtimeCache = appCaches.RuntimeCache;
5861
}
62+
5963
public void Handle(MediaCacheRefresherNotification notification)
6064
{
6165
// fired when media updated
6266
// store DateTime, as the cachekey
63-
6467
var lastCacheRefreshDate = DateTime.UtcNow.ToString("s");
65-
//insert and override existing value in appcache
66-
_runtimeCache.Insert(CacheKeyConstants.LastCacheRefreshDateKey, () => lastCacheRefreshDate, null, false, null);
6768

69+
// insert and override existing value in appcache
70+
_runtimeCache.Insert(CacheKeyConstants.LastCacheRefreshDateKey, () => lastCacheRefreshDate, null, false, null);
6871
}
6972
}
70-
7173
}

Our.Umbraco.TagHelpers/UmbracoCacheTagHelper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public UmbracoCacheTagHelper(CacheTagHelperMemoryCacheFactory factory, HtmlEncod
3737
_umbracoContextFactory = umbracoContextFactory;
3838
_runtimeCache = appCaches.RuntimeCache;
3939
}
40+
4041
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
4142
{
4243
using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
4344
{
4445
var umbracoContext = umbracoContextReference.UmbracoContext;
46+
4547
// we don't want to enable the cache tag helper if Umbraco is in Preview, or in Debug mode
4648
if (umbracoContext.InPreviewMode || umbracoContext.IsDebug)
4749
{
@@ -66,8 +68,10 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
6668
{
6769
// ironically read the last cache refresh date from runtime cache, and set it to now if it's not there...
6870
var umbLastCacheRefreshCacheKey = _runtimeCache.GetCacheItem(CacheKeyConstants.LastCacheRefreshDateKey, () => GetFallbackCacheRefreshDate()).ToString();
71+
6972
// append to VaryBy key incase VaryBy key is set to some other parameter too
7073
this.VaryBy = umbLastCacheRefreshCacheKey + "|" + this.VaryBy;
74+
7175
// 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
7276
if (this.ExpiresAfter == null)
7377
{
@@ -80,16 +84,15 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
8084
}
8185

8286
}
87+
8388
private string GetFallbackCacheRefreshDate()
8489
{
85-
//this fires if the 'appcache' doesn't have a LastCacheRefreshDate set by a publish
90+
// this fires if the 'appcache' doesn't have a LastCacheRefreshDate set by a publish
8691
// eg after an app pool recycle
8792
// it doesn't really matter that this isn't the last datetime that something was actually published
8893
// because time tends to always move forwards
8994
// the next publish will set a new future LastCacheRefreshDate...
9095
return DateTime.UtcNow.ToString("s");
91-
9296
}
93-
9497
}
9598
}

0 commit comments

Comments
 (0)