From 4a68bca981bd439092ebc7e991f8992538ffcae0 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Wed, 22 Jan 2025 21:47:16 +0800 Subject: [PATCH] remove Aspects.Cache --- .../Infrastructures/ICacheService.cs | 1 + .../Infrastructures/SharpCacheAttribute.cs | 35 ++++++++++++------- .../BotSharp.Core/BotSharp.Core.csproj | 1 - .../Cache/MemoryCacheService.cs | 11 ++++-- .../Cache/RedisCacheService.cs | 15 ++++++++ .../Infrastructures/Utilities.cs | 6 ++-- src/Infrastructure/BotSharp.Core/Using.cs | 1 - 7 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs index ef9c7d8e..af510da3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs @@ -6,4 +6,5 @@ public interface ICacheService Task GetAsync(string key, Type type); Task SetAsync(string key, T value, TimeSpan? expiry); Task RemoveAsync(string key); + Task ClearCacheAsync(string prefix); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs index a64529dc..301c6b83 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs @@ -13,24 +13,26 @@ public class SharpCacheAttribute : AsyncMoAttribute private readonly int _minutes; private readonly bool _perInstanceCache; + private readonly ICacheService _cache; + private readonly SharpCacheSettings _settings; public SharpCacheAttribute(int minutes = 60, bool perInstanceCache = false) { _minutes = minutes; _perInstanceCache = perInstanceCache; + _cache = Services.GetRequiredService(); + _settings = Services.GetRequiredService(); } public override async ValueTask OnEntryAsync(MethodContext context) { - var settings = Services.GetRequiredService(); - if (!settings.Enabled) + if (!_settings.Enabled) { return; } - var cache = Services.GetRequiredService(); - var key = GetCacheKey(settings, context); - var value = await cache.GetAsync(key, context.TaskReturnType); + var key = GetCacheKey(context); + var value = await _cache.GetAsync(key, context.TaskReturnType); if (value != null) { // check if the cache is out of date @@ -45,8 +47,7 @@ public class SharpCacheAttribute : AsyncMoAttribute public override async ValueTask OnSuccessAsync(MethodContext context) { - var settings = Services.GetRequiredService(); - if (!settings.Enabled) + if (!_settings.Enabled) { return; } @@ -59,12 +60,10 @@ public class SharpCacheAttribute : AsyncMoAttribute return; } - var cache = Services.GetRequiredService(); - if (context.ReturnValue != null) { - var key = GetCacheKey(settings, context); - await cache.SetAsync(key, context.ReturnValue, new TimeSpan(0, _minutes, 0)); + var key = GetCacheKey(context); + await _cache.SetAsync(key, context.ReturnValue, new TimeSpan(0, _minutes, 0)); } } @@ -74,9 +73,9 @@ public class SharpCacheAttribute : AsyncMoAttribute } - private string GetCacheKey(SharpCacheSettings settings, MethodContext context) + private string GetCacheKey(MethodContext context) { - var prefixKey = settings.Prefix + ":" + context.Method.Name; + var prefixKey = GetPrefixKey(context.Method.Name); var argsKey = string.Join("_", context.Arguments.Select(arg => GetCacheKeyByArg(arg))); if (_perInstanceCache && context.Target != null) @@ -89,6 +88,11 @@ public class SharpCacheAttribute : AsyncMoAttribute } } + private string GetPrefixKey(string name) + { + return _settings.Prefix + ":" + name; + } + private string GetCacheKeyByArg(object? arg) { if (arg is null) @@ -104,4 +108,9 @@ public class SharpCacheAttribute : AsyncMoAttribute return arg.GetHashCode().ToString(); } } + + public async Task ClearCacheAsync() + { + await _cache.ClearCacheAsync(_settings.Prefix); + } } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index bedcd52c..37eee3ac 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -188,7 +188,6 @@ - diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/MemoryCacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/MemoryCacheService.cs index ede8a380..f5bae36a 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/MemoryCacheService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/MemoryCacheService.cs @@ -1,15 +1,15 @@ using BotSharp.Abstraction.Infrastructures; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Options; namespace BotSharp.Core.Infrastructures; public class MemoryCacheService : ICacheService { - private readonly IMemoryCache _cache; + private static readonly MemoryCache _cache = new MemoryCache(new OptionsWrapper(new MemoryCacheOptions())); - public MemoryCacheService(IMemoryCache memoryCache) + public MemoryCacheService() { - this._cache = memoryCache; } public async Task GetAsync(string key) @@ -34,4 +34,9 @@ public class MemoryCacheService : ICacheService { _cache.Remove(key); } + + public async Task ClearCacheAsync(string prefix) + { + _cache.Compact(1.0); + } } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs index 78c62acd..149722d7 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Infrastructures; using Newtonsoft.Json; using StackExchange.Redis; +using System.Linq; namespace BotSharp.Core.Infrastructures; @@ -51,4 +52,18 @@ public class RedisCacheService : ICacheService var db = _redis.GetDatabase(); await db.KeyDeleteAsync(key); } + + public async Task ClearCacheAsync(string prefix) + { + var db = _redis.GetDatabase(); + var server = _redis.GetServer(_redis.GetEndPoints().First()); + const int pageSize = 1000; + var keys = server.Keys(pattern: $"{prefix}*", pageSize: pageSize).ToList(); + + for (int i = 0; i < keys.Count; i += pageSize) + { + var batch = keys.Skip(i).Take(pageSize).ToArray(); + await db.KeyDeleteAsync(batch); + } + } } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs index db5a9a7d..6293c1f6 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs @@ -48,10 +48,8 @@ public static class Utilities public static void ClearCache() { // Clear whole cache. - if (new MemoryCacheAttribute(0).Cache is MemoryCache memcache) - { - memcache.Compact(100); - } + var sharpCache = new SharpCacheAttribute(0); + sharpCache.ClearCacheAsync().ConfigureAwait(false).GetAwaiter().GetResult(); } public static string HideMiddleDigits(string input, bool isEmail = false) diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index 4a74f1d0..0aaaf01a 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -42,5 +42,4 @@ global using BotSharp.Core.Agents.Services; global using BotSharp.Core.Conversations.Services; global using BotSharp.Core.Infrastructures; global using BotSharp.Core.Users.Services; -global using Aspects.Cache; global using BotSharp.Abstraction.Infrastructures.Events; \ No newline at end of file