From ebeccad54278921df36ef84b4ce64e818dc76cde Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Thu, 26 Sep 2024 17:22:06 -0500 Subject: [PATCH] Add RemoveAsync cache --- .../Infrastructures/ICacheService.cs | 1 + .../Infrastructures/SharpCacheAttribute.cs | 13 ++++++++++++- .../Infrastructures/MemoryCacheService.cs | 5 +++++ .../Infrastructures/RedisCacheService.cs | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs index 692e9e4a..ef9c7d8e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs @@ -5,4 +5,5 @@ public interface ICacheService Task GetAsync(string key); Task GetAsync(string key, Type type); Task SetAsync(string key, T value, TimeSpan? expiry); + Task RemoveAsync(string key); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs index 17ddb830..dd30f491 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs @@ -31,7 +31,13 @@ public class SharpCacheAttribute : MoAttribute var value = cache.GetAsync(key, context.TaskReturnType).Result; if (value != null) { - context.ReplaceReturnValue(this, value); + // check if the cache is out of date + var isOutOfDate = IsOutOfDate(context, value).Result; + + if (!isOutOfDate) + { + context.ReplaceReturnValue(this, value); + } } } @@ -58,6 +64,11 @@ public class SharpCacheAttribute : MoAttribute } } + public virtual Task IsOutOfDate(MethodContext context, object value) + { + return Task.FromResult(false); + } + private string GetCacheKey(SharpCacheSettings settings, MethodContext context) { var key = settings.Prefix + "-" + context.Method.Name; diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs index 2d2fe13b..a775ab6a 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs @@ -32,4 +32,9 @@ public class MemoryCacheService : ICacheService AbsoluteExpirationRelativeToNow = expiry }); } + + public async Task RemoveAsync(string key) + { + _cache.Remove(key); + } } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs index 279f9014..bde2708e 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs @@ -76,4 +76,20 @@ public class RedisCacheService : ICacheService var db = redis.GetDatabase(); await db.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry); } + + public async Task RemoveAsync(string key) + { + if (string.IsNullOrEmpty(_settings.Redis)) + { + return; + } + + if (redis == null) + { + redis = ConnectionMultiplexer.Connect(_settings.Redis); + } + + var db = redis.GetDatabase(); + await db.KeyDeleteAsync(key); + } }