-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add possiblity to configure and invalidate first page cache
- Loading branch information
1 parent
313c53e
commit c41e18e
Showing
15 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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
38 changes: 38 additions & 0 deletions
38
src/LinkDotNet.Blog.Web/Features/Admin/Settings/SettingsPage.razor
This file contains 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,38 @@ | ||
@page "/settings" | ||
@using LinkDotNet.Blog.Web.Features.Services | ||
@inject IOptions<ApplicationConfiguration> ApplicationConfiguration | ||
@inject ICacheInvalidator CacheInvalidator | ||
@inject IToastService ToastService | ||
@attribute [Authorize] | ||
|
||
<div class="container-fluid ms-3"> | ||
<h3>Settings</h3> | ||
<table class="table table-responsive table-hover"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Configuration Name</th> | ||
<th scope="col">Description</th> | ||
<th scope="col">Value</th> | ||
<th scope="col">Actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Cache Duration (First Page)</td> | ||
<td>Defines how long the first page remains cached before a refresh is required.<br/> | ||
When a scheduled blog post is published, the cache is always invalidated.<br/> | ||
The longer the cache lives, the longer it takes for the user to see updated content on the first page.</td> | ||
<td>@ApplicationConfiguration.Value.FirstPageCacheDurationInMinutes Minutes</td> | ||
<td><button class="btn btn-warning" id="invalidate-cache" @onclick="InvalidateCache">Invalidate Cache</button></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
@code { | ||
private void InvalidateCache() | ||
{ | ||
CacheInvalidator.Cancel(); | ||
ToastService.ShowInfo("Cache was invalidated."); | ||
} | ||
} |
This file contains 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 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 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 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 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,19 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace LinkDotNet.Blog.Web.Features.Services; | ||
|
||
public sealed class CacheService : ICacheTokenProvider, ICacheInvalidator, IDisposable | ||
{ | ||
private CancellationTokenSource cancellationTokenSource = new(); | ||
|
||
public CancellationToken Token => cancellationTokenSource.Token; | ||
|
||
public void Cancel() | ||
{ | ||
cancellationTokenSource.Cancel(); | ||
cancellationTokenSource = new(); | ||
} | ||
|
||
public void Dispose() => cancellationTokenSource.Dispose(); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/LinkDotNet.Blog.Web/Features/Services/ICacheInvalidator.cs
This file contains 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,6 @@ | ||
namespace LinkDotNet.Blog.Web.Features.Services; | ||
|
||
public interface ICacheInvalidator | ||
{ | ||
void Cancel(); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/LinkDotNet.Blog.Web/Features/Services/ICacheTokenProvider.cs
This file contains 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,8 @@ | ||
using System.Threading; | ||
|
||
namespace LinkDotNet.Blog.Web.Features.Services; | ||
|
||
public interface ICacheTokenProvider | ||
{ | ||
CancellationToken Token { get; } | ||
} |
This file contains 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 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 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 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
26 changes: 26 additions & 0 deletions
26
tests/LinkDotNet.Blog.UnitTests/Web/Features/Admin/Settings/SettingsPageTests.cs
This file contains 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 @@ | ||
using Blazored.Toast.Services; | ||
using LinkDotNet.Blog.Web; | ||
using LinkDotNet.Blog.Web.Features.Admin.Settings; | ||
using LinkDotNet.Blog.Web.Features.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace LinkDotNet.Blog.UnitTests.Web.Features.Admin.Settings; | ||
|
||
public class SettingsPageTests : TestContext | ||
{ | ||
[Fact] | ||
public void GivenSettingsPage_WhenClicking_InvalidateCacheButton_TokenIsCancelled() | ||
{ | ||
var cacheInvalidator = Substitute.For<ICacheInvalidator>(); | ||
Services.AddScoped(_ => cacheInvalidator); | ||
Services.AddScoped(_ => Options.Create<ApplicationConfiguration>(new())); | ||
Services.AddScoped(_ => Substitute.For<IToastService>()); | ||
var cut = RenderComponent<SettingsPage>(); | ||
var invalidateCacheButton = cut.Find("#invalidate-cache"); | ||
|
||
invalidateCacheButton.Click(); | ||
|
||
cacheInvalidator.Received(1).Cancel(); | ||
} | ||
} |