From 4618ffca4f27ef7323822f56e4fb76608a7fc9d7 Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 21 Jun 2024 20:56:43 +0800 Subject: [PATCH] refactoring CacheKey --- src/Templates/Caching/.cachekey.cs.txt | 38 +++++++++++++++++++------- src/Templates/Commands/AddEdit/.cs.txt | 2 +- src/Templates/Commands/Create/.cs.txt | 2 +- src/Templates/Commands/Delete/.cs.txt | 2 +- src/Templates/Commands/Import/.cs.txt | 2 +- src/Templates/Commands/Update/.cs.txt | 2 +- src/Templates/Pages/.razor.txt | 10 ++----- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Templates/Caching/.cachekey.cs.txt b/src/Templates/Caching/.cachekey.cs.txt index 01fa54f..8bfd886 100644 --- a/src/Templates/Caching/.cachekey.cs.txt +++ b/src/Templates/Caching/.cachekey.cs.txt @@ -5,7 +5,12 @@ namespace {namespace}; public static class {itemname}CacheKey { - private static readonly TimeSpan refreshInterval = TimeSpan.FromHours(3); + private static readonly TimeSpan RefreshInterval = TimeSpan.FromHours(3); + private static readonly object TokenLock = new(); + private static CancellationTokenSource _tokenSource = new (RefreshInterval); + public static MemoryCacheEntryOptions MemoryCacheEntryOptions => + new MemoryCacheEntryOptions().AddExpirationToken(new CancellationChangeToken(GetOrCreateTokenSource().Token)); + public const string GetAllCacheKey = "all-{nameofPlural}"; public static string GetPaginationCacheKey(string parameters) { return $"{itemname}CacheKey:{nameofPlural}WithPaginationQuery,{parameters}"; @@ -16,20 +21,33 @@ public static class {itemname}CacheKey public static string GetByIdCacheKey(string parameters) { return $"{itemname}CacheKey:GetByIdCacheKey,{parameters}"; } - static {itemname}CacheKey() + + + + public static CancellationTokenSource GetOrCreateTokenSource() { - _tokensource = new CancellationTokenSource(refreshInterval); + lock (TokenLock) + { + if (_tokenSource.IsCancellationRequested) + { + _tokenSource.Dispose(); + _tokenSource = new CancellationTokenSource(RefreshInterval); + } + return _tokenSource; + } } - private static CancellationTokenSource _tokensource; - public static CancellationTokenSource SharedExpiryTokenSource() + + public static void Refresh() { - if (_tokensource.IsCancellationRequested) + lock (TokenLock) { - _tokensource = new CancellationTokenSource(refreshInterval); + if (!_tokenSource.IsCancellationRequested) + { + _tokenSource.Cancel(); + _tokenSource.Dispose(); + _tokenSource = new CancellationTokenSource(RefreshInterval); + } } - return _tokensource; } - public static void Refresh() => SharedExpiryTokenSource().Cancel(); - public static MemoryCacheEntryOptions MemoryCacheEntryOptions => new MemoryCacheEntryOptions().AddExpirationToken(new CancellationChangeToken(SharedExpiryTokenSource().Token)); } diff --git a/src/Templates/Commands/AddEdit/.cs.txt b/src/Templates/Commands/AddEdit/.cs.txt index f3d4e22..d96edb3 100644 --- a/src/Templates/Commands/AddEdit/.cs.txt +++ b/src/Templates/Commands/AddEdit/.cs.txt @@ -12,7 +12,7 @@ public class AddEdit{itemname}Command: ICacheInvalidatorRequest> {dtoFieldDefinition} public string CacheKey => {itemname}CacheKey.GetAllCacheKey; - public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource(); + public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource(); private class Mapping : Profile { diff --git a/src/Templates/Commands/Create/.cs.txt b/src/Templates/Commands/Create/.cs.txt index 22e7756..aaddf74 100644 --- a/src/Templates/Commands/Create/.cs.txt +++ b/src/Templates/Commands/Create/.cs.txt @@ -12,7 +12,7 @@ public class Create{itemname}Command: ICacheInvalidatorRequest> public int Id { get; set; } {dtoFieldDefinition} public string CacheKey => {itemname}CacheKey.GetAllCacheKey; - public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource(); + public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource(); private class Mapping : Profile { public Mapping() diff --git a/src/Templates/Commands/Delete/.cs.txt b/src/Templates/Commands/Delete/.cs.txt index a2df2d8..ace30e9 100644 --- a/src/Templates/Commands/Delete/.cs.txt +++ b/src/Templates/Commands/Delete/.cs.txt @@ -10,7 +10,7 @@ namespace {namespace}; { public int[] Id { get; } public string CacheKey => {itemname}CacheKey.GetAllCacheKey; - public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource(); + public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource(); public Delete{itemname}Command(int[] id) { Id = id; diff --git a/src/Templates/Commands/Import/.cs.txt b/src/Templates/Commands/Import/.cs.txt index f706d20..621cadf 100644 --- a/src/Templates/Commands/Import/.cs.txt +++ b/src/Templates/Commands/Import/.cs.txt @@ -11,7 +11,7 @@ namespace {namespace}; public string FileName { get; set; } public byte[] Data { get; set; } public string CacheKey => {itemname}CacheKey.GetAllCacheKey; - public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource(); + public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource(); public Import{nameofPlural}Command(string fileName,byte[] data) { FileName = fileName; diff --git a/src/Templates/Commands/Update/.cs.txt b/src/Templates/Commands/Update/.cs.txt index 9da1994..484bd40 100644 --- a/src/Templates/Commands/Update/.cs.txt +++ b/src/Templates/Commands/Update/.cs.txt @@ -12,7 +12,7 @@ public class Update{itemname}Command: ICacheInvalidatorRequest> public int Id { get; set; } {dtoFieldDefinition} public string CacheKey => {itemname}CacheKey.GetAllCacheKey; - public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource(); + public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource(); private class Mapping : Profile { public Mapping() diff --git a/src/Templates/Pages/.razor.txt b/src/Templates/Pages/.razor.txt index ca28ca4..7084025 100644 --- a/src/Templates/Pages/.razor.txt +++ b/src/Templates/Pages/.razor.txt @@ -44,14 +44,14 @@
-
+
@ConstantString.Refresh + >@ConstantString.Refresh @if (_canCreate) { @ConstantString.New @ConstantString.Clone } @if (_canDelete) @@ -75,7 +73,6 @@ StartIcon="@Icons.Material.Filled.Delete" Disabled="@(!(_selectedItems.Count>0))" Size="Size.Small" - Style="margin-right: 4px; margin-bottom:4px" OnClick="OnDeleteChecked" IconColor="Color.Surface">@ConstantString.Delete } @@ -86,7 +83,6 @@ Disabled="@_loading" StartIcon="@Icons.Custom.FileFormats.FileExcel" Size="Size.Small" - Style="margin-right: 4px; margin-bottom:4px" OnClick="OnExport" IconColor="Color.Surface"> @ConstantString.Export @@ -124,7 +120,6 @@ Size="Size.Small" Disabled="@_loading" OnClick="OnCreate" - Style="margin-right: 4px; margin-bottom:4px" IconColor="Color.Surface">@ConstantString.New } @if (_canDelete) @@ -133,7 +128,6 @@ StartIcon="@Icons.Material.Filled.Delete" Disabled="@(!(_selectedItems.Count>0))" Size="Size.Small" - Style="margin-right: 4px; margin-bottom:4px" OnClick="OnDeleteChecked" IconColor="Color.Surface">@ConstantString.Delete }