Skip to content

Commit d80f89b

Browse files
committed
fix: Readded cache invalidation (but globally)
1 parent 6d4014c commit d80f89b

File tree

12 files changed

+27
-45
lines changed

12 files changed

+27
-45
lines changed

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPost.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@
334334
await OnBlogPostCreated.InvokeAsync(model.ToBlogPost());
335335
if (model.ShouldInvalidateCache)
336336
{
337-
CacheInvalidator.Cancel();
337+
await CacheInvalidator.ClearCacheAsync();
338338
}
339339

340340
InstantJobRegistry.RunInstantJob<SimilarBlogPostJob>(parameter: true);

src/LinkDotNet.Blog.Web/Features/Admin/Settings/SettingsPage.razor

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
</thead>
2323
<tbody>
2424
<tr>
25-
<td>Cache Duration (First Page)</td>
26-
<td>Defines how long the first page remains cached before a refresh is required.<br/>
27-
The longer the cache lives, the longer it takes for the user to see updated content on the first page, but the faster the page loads.<br/>
28-
You can manually invalidate the cache.</td>
29-
<td>@ApplicationConfiguration.Value.FirstPageCacheDurationInMinutes Minutes</td>
30-
<td><button class="btn btn-warning" id="invalidate-cache" @onclick="InvalidateCache">Invalidate Cache</button></td>
25+
<td>Cache</td>
26+
<td>Clears all cached data including the first page, sitemap, and other cached content.<br/>
27+
Use this to immediately reflect changes across the blog.</td>
28+
<td>-</td>
29+
<td><button class="btn btn-warning" id="invalidate-cache" @onclick="InvalidateCacheAsync">Clear Cache</button></td>
3130
</tr>
3231
<tr>
3332
<td>Run Visit Counter Data Collection</td>
@@ -41,10 +40,10 @@
4140
</div>
4241

4342
@code {
44-
private void InvalidateCache()
43+
private async Task InvalidateCacheAsync()
4544
{
46-
CacheInvalidator.Cancel();
47-
ToastService.ShowInfo("Cache was invalidated.");
45+
await CacheInvalidator.ClearCacheAsync();
46+
ToastService.ShowInfo("Cache was cleared.");
4847
}
4948

5049
private void RunVisitTransformer()

src/LinkDotNet.Blog.Web/Features/BlogPostPublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private async Task<int> PublishScheduledBlogPostsAsync()
4747

4848
if (blogPostsToPublish.Count > 0)
4949
{
50-
cacheInvalidator.Cancel();
50+
await cacheInvalidator.ClearCacheAsync();
5151
}
5252

5353
return blogPostsToPublish.Count;

src/LinkDotNet.Blog.Web/Features/Home/Index.razor

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
@using LinkDotNet.Blog.Infrastructure
66
@using LinkDotNet.Blog.Infrastructure.Persistence
77
@using LinkDotNet.Blog.Web.Features.Home.Components
8-
@using LinkDotNet.Blog.Web.Features.Services
9-
@using Microsoft.Extensions.Caching.Memory
10-
@using Microsoft.Extensions.Primitives
118
@using ZiggyCreatures.Caching.Fusion
129
@inject IFusionCache FusionCache
13-
@inject ICacheTokenProvider CacheTokenProvider
1410
@inject IRepository<BlogPost> BlogPostRepository
1511
@inject IOptions<Introduction> Introduction
1612
@inject IOptions<ApplicationConfiguration> AppConfiguration
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
using System;
2-
using System.Threading;
1+
using System.Threading.Tasks;
2+
using ZiggyCreatures.Caching.Fusion;
33

44
namespace LinkDotNet.Blog.Web.Features.Services;
55

6-
public sealed class CacheService : ICacheTokenProvider, ICacheInvalidator, IDisposable
6+
public sealed class CacheService : ICacheInvalidator
77
{
8-
private CancellationTokenSource cancellationTokenSource = new();
8+
private readonly IFusionCache fusionCache;
99

10-
public CancellationToken Token => cancellationTokenSource.Token;
11-
12-
public void Cancel()
10+
public CacheService(IFusionCache fusionCache)
1311
{
14-
cancellationTokenSource.Cancel();
15-
cancellationTokenSource = new();
12+
this.fusionCache = fusionCache;
1613
}
1714

18-
public void Dispose() => cancellationTokenSource.Dispose();
15+
public Task ClearCacheAsync() => fusionCache.ClearAsync().AsTask();
1916
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System.Threading.Tasks;
2+
13
namespace LinkDotNet.Blog.Web.Features.Services;
24

35
public interface ICacheInvalidator
46
{
5-
void Cancel();
7+
Task ClearCacheAsync();
68
}

src/LinkDotNet.Blog.Web/Features/Services/ICacheTokenProvider.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/LinkDotNet.Blog.Web/ServiceExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
2727
services.AddScoped<ICurrentUserService, CurrentUserService>();
2828

2929
services.AddSingleton<CacheService>();
30-
services.AddSingleton<ICacheTokenProvider>(s => s.GetRequiredService<CacheService>());
3130
services.AddSingleton<ICacheInvalidator>(s => s.GetRequiredService<CacheService>());
3231

3332
services.AddBackgroundServices();

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/BlogPostPublisherTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public async Task ShouldInvalidateCacheWhenPublishing()
4949

5050
await sut.RunAsync(Substitute.For<IJobExecutionContext>(), CancellationToken.None);
5151

52-
cacheInvalidator.Received().Cancel();
52+
await cacheInvalidator.Received().ClearCacheAsync();
5353
}
5454

5555
[Fact]
5656
public async Task ShouldNotInvalidateCacheWhenThereIsNothingToPublish()
5757
{
5858
await sut.RunAsync(Substitute.For<IJobExecutionContext>(), CancellationToken.None);
5959

60-
cacheInvalidator.DidNotReceive().Cancel();
60+
await cacheInvalidator.DidNotReceive().ClearCacheAsync();
6161
}
6262
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Home/IndexTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using LinkDotNet.Blog.Web.Features.Bookmarks;
88
using LinkDotNet.Blog.Web.Features.Components;
99
using LinkDotNet.Blog.Web.Features.Home;
10-
using LinkDotNet.Blog.Web.Features.Services;
1110
using Microsoft.Extensions.DependencyInjection;
1211
using Microsoft.Extensions.Options;
1312
using ZiggyCreatures.Caching.Fusion;
@@ -225,7 +224,6 @@ private void RegisterComponents(BunitContext ctx, string? profilePictureUri = nu
225224
ctx.Services.AddScoped(_ => Repository);
226225
ctx.Services.AddScoped(_ => Options.Create(CreateSampleAppConfiguration(profilePictureUri, useMultiAuthorMode).ApplicationConfiguration));
227226
ctx.Services.AddScoped(_ => Options.Create(CreateSampleAppConfiguration(profilePictureUri, useMultiAuthorMode).Introduction));
228-
ctx.Services.AddScoped(_ => Substitute.For<ICacheTokenProvider>());
229227
ctx.Services.AddScoped(_ => Substitute.For<IBookmarkService>());
230228
ctx.Services.AddScoped<IFusionCache>(_ => new FusionCache(new FusionCacheOptions(), memoryLocker: new AsyncKeyedMemoryLocker()));
231229
}

0 commit comments

Comments
 (0)