-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: independent cache helper from repository
- Loading branch information
1 parent
47b84a7
commit 785c017
Showing
13 changed files
with
206 additions
and
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using GZCTF.Utils; | ||
using MemoryPack; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
|
||
namespace GZCTF.Extensions; | ||
|
||
public static class CacheExtensions | ||
{ | ||
/// <summary> | ||
/// 获取缓存或重新构建,如果缓存不存在会阻塞 | ||
/// 使用 CacheMaker 和 CacheRequest 代替处理耗时更久的缓存 | ||
/// </summary> | ||
public static async Task<T> GetOrCreateAsync<T, L>(this IDistributedCache cache, | ||
ILogger<L> logger, | ||
string key, | ||
Func<DistributedCacheEntryOptions, Task<T>> func, | ||
CancellationToken token = default) | ||
where T : class | ||
{ | ||
var value = await cache.GetAsync(key, token); | ||
T? result = default; | ||
|
||
if (value is not null) | ||
{ | ||
try | ||
{ | ||
result = MemoryPackSerializer.Deserialize<T>(value); | ||
} | ||
catch | ||
{ } | ||
if (result is not null) | ||
return result; | ||
} | ||
|
||
var cacheOptions = new DistributedCacheEntryOptions(); | ||
result = await func(cacheOptions); | ||
var bytes = MemoryPackSerializer.Serialize(result); | ||
|
||
await cache.SetAsync(key, bytes, cacheOptions, token); | ||
logger.SystemLog($"重建缓存:{key} @ {bytes.Length} bytes", TaskStatus.Success, LogLevel.Debug); | ||
|
||
return result; | ||
} | ||
} |
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
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
Oops, something went wrong.