remove Aspects.Cache

This commit is contained in:
nick.yi 2025-01-22 21:47:16 +08:00
parent 6d21ba2c0a
commit 4a68bca981
7 changed files with 48 additions and 22 deletions

View file

@ -6,4 +6,5 @@ public interface ICacheService
Task<object> GetAsync(string key, Type type);
Task SetAsync<T>(string key, T value, TimeSpan? expiry);
Task RemoveAsync(string key);
Task ClearCacheAsync(string prefix);
}

View file

@ -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<ICacheService>();
_settings = Services.GetRequiredService<SharpCacheSettings>();
}
public override async ValueTask OnEntryAsync(MethodContext context)
{
var settings = Services.GetRequiredService<SharpCacheSettings>();
if (!settings.Enabled)
if (!_settings.Enabled)
{
return;
}
var cache = Services.GetRequiredService<ICacheService>();
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<SharpCacheSettings>();
if (!settings.Enabled)
if (!_settings.Enabled)
{
return;
}
@ -59,12 +60,10 @@ public class SharpCacheAttribute : AsyncMoAttribute
return;
}
var cache = Services.GetRequiredService<ICacheService>();
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);
}
}

View file

@ -188,7 +188,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.8.0" />
<PackageReference Include="Fluid.Core" Version="2.11.1" />

View file

@ -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<MemoryCacheOptions>(new MemoryCacheOptions()));
public MemoryCacheService(IMemoryCache memoryCache)
public MemoryCacheService()
{
this._cache = memoryCache;
}
public async Task<T?> GetAsync<T>(string key)
@ -34,4 +34,9 @@ public class MemoryCacheService : ICacheService
{
_cache.Remove(key);
}
public async Task ClearCacheAsync(string prefix)
{
_cache.Compact(1.0);
}
}

View file

@ -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);
}
}
}

View file

@ -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)

View file

@ -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;