Skip to content

Commit

Permalink
Merge pull request #97 from neozhu/fusioncache
Browse files Browse the repository at this point in the history
Fusioncache
  • Loading branch information
neozhu authored Nov 13, 2024
2 parents 0d3ac49 + a67df8f commit 787a446
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 55 deletions.
47 changes: 5 additions & 42 deletions src/Templates/Caching/.cachekey.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,23 @@ namespace {namespace};
/// </summary>
public static class {itemname}CacheKey
{
// Defines the refresh interval for the cache expiration token
private static readonly TimeSpan RefreshInterval = TimeSpan.FromMinutes(30);
// Object used for locking to ensure thread safety
private static readonly object TokenLock = new();
// CancellationTokenSource used for managing cache expiration
private static CancellationTokenSource _tokenSource = new (RefreshInterval);
/// <summary>
/// Gets the memory cache entry options with an expiration token.
/// </summary>
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}";
}
public static string GetExportCacheKey(string parameters) {
return $"{itemname}CacheKey:ExportCacheKey,{parameters}";
}
public static string GetByNameCacheKey(string parameters) {
return $"{itemname}CacheKey:GetByNameCacheKey,{parameters}";
}
public static string GetByIdCacheKey(string parameters) {
return $"{itemname}CacheKey:GetByIdCacheKey,{parameters}";
}


/// <summary>
/// Gets or creates a new <see cref="CancellationTokenSource"/> with the specified refresh interval.
/// </summary>
/// <returns>The current or new <see cref="CancellationTokenSource"/>.</returns>
public static CancellationTokenSource GetOrCreateTokenSource()
{
lock (TokenLock)
{
if (_tokenSource.IsCancellationRequested)
{
_tokenSource.Dispose();
_tokenSource = new CancellationTokenSource(RefreshInterval);
}
return _tokenSource;
}
}
/// <summary>
/// Refreshes the cache expiration token by cancelling and recreating the <see cref="CancellationTokenSource"/>.
/// </summary>
public static IEnumerable<string>? Tags => new string[] { "{itemnamelowercase}" };
public static void Refresh()
{
lock (TokenLock)
{
if (!_tokenSource.IsCancellationRequested)
{
_tokenSource.Cancel();
_tokenSource.Dispose();
_tokenSource = new CancellationTokenSource(RefreshInterval);
}
}
FusionCacheFactory.RemoveByTags(Tags);
}
}

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

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

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

// Usage:
// This validator enforces constraints on the AddEditContactCommand, such as
// This validator enforces constraints on the AddEdit{itemname}Command, such as
// maximum field length for ...

namespace {namespace};
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 @@ -36,7 +36,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.GetOrCreateTokenSource();
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
}

public class Create{itemname}CommandHandler : IRequestHandler<Create{itemname}Command, Result<int>>
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 @@ -35,7 +35,7 @@ public class Delete{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{
public int[] Id { get; }
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
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 @@ -37,7 +37,7 @@ namespace {namespace};
public string FileName { get; set; }
public byte[] Data { get; set; }
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
public Import{nameofPlural}Command(string fileName,byte[] data)
{
FileName = fileName;
Expand Down
4 changes: 2 additions & 2 deletions src/Templates/Commands/Update/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class Update{itemname}Command: ICacheInvalidatorRequest<Result<int>>
[Description("Id")]
public int Id { get; set; }
{dtoFieldDefinition}
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;

}

Expand Down
9 changes: 8 additions & 1 deletion src/Templates/Queries/Export/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

using {selectns}.{nameofPlural}.DTOs;
using {selectns}.{nameofPlural}.Mappers;
using {selectns}.{nameofPlural}.Caching;
using {selectns}.{nameofPlural}.Specifications;

namespace {namespace};

public class Export{nameofPlural}Query : {itemname}AdvancedFilter, IRequest<Result<byte[]>>
public class Export{nameofPlural}Query : {itemname}AdvancedFilter, ICacheableRequest<Result<byte[]>>
{
public {itemname}AdvancedSpecification Specification => new {itemname}AdvancedSpecification(this);
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
public override string ToString()
{
return $"Listview:{ListView}:{CurrentUser?.UserId}-{LocalTimezoneOffset.TotalHours}, Search:{Keyword}, {OrderBy}, {SortDirection}";
}
public string CacheKey => {itemname}CacheKey.GetExportCacheKey($"{this}");
}

public class Export{nameofPlural}QueryHandler :
Expand Down
2 changes: 1 addition & 1 deletion src/Templates/Queries/GetAll/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace {namespace};
public class GetAll{nameofPlural}Query : ICacheableRequest<IEnumerable<{itemname}Dto>>
{
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
}

public class GetAll{nameofPlural}QueryHandler :
Expand Down
4 changes: 2 additions & 2 deletions src/Templates/Queries/GetById/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Get{itemname}ByIdQuery : ICacheableRequest<Result<{itemname}Dto>>
{
public required int Id { get; set; }
public string CacheKey => {itemname}CacheKey.GetByIdCacheKey($"{Id}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
}

public class Get{itemname}ByIdQueryHandler :
Expand All @@ -43,6 +43,6 @@ public class Get{itemname}ByIdQueryHandler :
var data = await _context.{nameofPlural}.ApplySpecification(new {itemname}ByIdSpecification(request.Id))
.ProjectTo()
.FirstAsync(cancellationToken);
return await Result<ContactDto>.SuccessAsync(data);
return await Result<{itemname}Dto>.SuccessAsync(data);
}
}
4 changes: 2 additions & 2 deletions src/Templates/Queries/Pagination/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class {nameofPlural}WithPaginationQuery : {itemname}AdvancedFilter, ICach
{
public override string ToString()
{
return $"Listview:{ListView}-{LocalTimezoneOffset.TotalHours}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
return $"Listview:{ListView}:{CurrentUser?.UserId}-{LocalTimezoneOffset.TotalHours}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
}
public string CacheKey => {itemname}CacheKey.GetPaginationCacheKey($"{this}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
public IEnumerable<string>? Tags => {itemname}CacheKey.Tags;
public {itemname}AdvancedSpecification Specification => new {itemname}AdvancedSpecification(this);
}

Expand Down

0 comments on commit 787a446

Please sign in to comment.