Skip to content

Commit

Permalink
refactoring CacheKey
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Jun 21, 2024
1 parent 090ffd6 commit 4618ffc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
38 changes: 28 additions & 10 deletions src/Templates/Caching/.cachekey.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand All @@ -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));
}

2 changes: 1 addition & 1 deletion src/Templates/Commands/AddEdit/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AddEdit{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{dtoFieldDefinition}

public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.SharedExpiryTokenSource();
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();

private class Mapping : Profile
{
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Commands/Create/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Create{itemname}Command: ICacheInvalidatorRequest<Result<int>>
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()
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Commands/Delete/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Commands/Import/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Commands/Update/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Update{itemname}Command: ICacheInvalidatorRequest<Result<int>>
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()
Expand Down
10 changes: 2 additions & 8 deletions src/Templates/Pages/.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,27 @@
<div class="flex-grow-1" />

<div class="d-flex flex-column justify-end">
<div class="d-flex">
<div class="d-flex gap-1">
<MudHidden Breakpoint="Breakpoint.SmAndDown">
<MudButton DisableElevation Variant="Variant.Outlined"
Size="Size.Small"
Disabled="@_loading"
OnClick="@(()=>OnRefresh())"
StartIcon="@Icons.Material.Filled.Refresh" IconColor="Color.Surface" Color="Color.Primary"
Style="margin-right: 4px; margin-bottom:4px">@ConstantString.Refresh</MudButton>
>@ConstantString.Refresh</MudButton>
@if (_canCreate)
{
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Add"
Size="Size.Small"
Disabled="@_loading"
OnClick="OnCreate"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.New</MudButton>
<MudButton DisableElevation Variant="Variant.Outlined" Color="Color.Primary"
StartIcon="@Icons.Material.Filled.ContentCopy"
Size="Size.Small"
Disabled="@(_selectedItems.Count!=1)"
OnClick="OnClone"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.Clone</MudButton>
}
@if (_canDelete)
Expand All @@ -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</MudButton>
}
Expand All @@ -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
Expand Down Expand Up @@ -124,7 +120,6 @@
Size="Size.Small"
Disabled="@_loading"
OnClick="OnCreate"
Style="margin-right: 4px; margin-bottom:4px"
IconColor="Color.Surface">@ConstantString.New</MudButton>
}
@if (_canDelete)
Expand All @@ -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</MudButton>
}
Expand Down

0 comments on commit 4618ffc

Please sign in to comment.