Skip to content

Commit 4a68bca

Browse files
committed
remove Aspects.Cache
1 parent 6d21ba2 commit 4a68bca

File tree

7 files changed

+48
-22
lines changed

7 files changed

+48
-22
lines changed

src/Infrastructure/BotSharp.Abstraction/Infrastructures/ICacheService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public interface ICacheService
66
Task<object> GetAsync(string key, Type type);
77
Task SetAsync<T>(string key, T value, TimeSpan? expiry);
88
Task RemoveAsync(string key);
9+
Task ClearCacheAsync(string prefix);
910
}

src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ public class SharpCacheAttribute : AsyncMoAttribute
1313

1414
private readonly int _minutes;
1515
private readonly bool _perInstanceCache;
16+
private readonly ICacheService _cache;
17+
private readonly SharpCacheSettings _settings;
1618

1719
public SharpCacheAttribute(int minutes = 60, bool perInstanceCache = false)
1820
{
1921
_minutes = minutes;
2022
_perInstanceCache = perInstanceCache;
23+
_cache = Services.GetRequiredService<ICacheService>();
24+
_settings = Services.GetRequiredService<SharpCacheSettings>();
2125
}
2226

2327
public override async ValueTask OnEntryAsync(MethodContext context)
2428
{
25-
var settings = Services.GetRequiredService<SharpCacheSettings>();
26-
if (!settings.Enabled)
29+
if (!_settings.Enabled)
2730
{
2831
return;
2932
}
3033

31-
var cache = Services.GetRequiredService<ICacheService>();
32-
var key = GetCacheKey(settings, context);
33-
var value = await cache.GetAsync(key, context.TaskReturnType);
34+
var key = GetCacheKey(context);
35+
var value = await _cache.GetAsync(key, context.TaskReturnType);
3436
if (value != null)
3537
{
3638
// check if the cache is out of date
@@ -45,8 +47,7 @@ public override async ValueTask OnEntryAsync(MethodContext context)
4547

4648
public override async ValueTask OnSuccessAsync(MethodContext context)
4749
{
48-
var settings = Services.GetRequiredService<SharpCacheSettings>();
49-
if (!settings.Enabled)
50+
if (!_settings.Enabled)
5051
{
5152
return;
5253
}
@@ -59,12 +60,10 @@ public override async ValueTask OnSuccessAsync(MethodContext context)
5960
return;
6061
}
6162

62-
var cache = Services.GetRequiredService<ICacheService>();
63-
6463
if (context.ReturnValue != null)
6564
{
66-
var key = GetCacheKey(settings, context);
67-
await cache.SetAsync(key, context.ReturnValue, new TimeSpan(0, _minutes, 0));
65+
var key = GetCacheKey(context);
66+
await _cache.SetAsync(key, context.ReturnValue, new TimeSpan(0, _minutes, 0));
6867
}
6968
}
7069

@@ -74,9 +73,9 @@ public virtual Task<bool> IsOutOfDate(MethodContext context, object value)
7473
}
7574

7675

77-
private string GetCacheKey(SharpCacheSettings settings, MethodContext context)
76+
private string GetCacheKey(MethodContext context)
7877
{
79-
var prefixKey = settings.Prefix + ":" + context.Method.Name;
78+
var prefixKey = GetPrefixKey(context.Method.Name);
8079
var argsKey = string.Join("_", context.Arguments.Select(arg => GetCacheKeyByArg(arg)));
8180

8281
if (_perInstanceCache && context.Target != null)
@@ -89,6 +88,11 @@ private string GetCacheKey(SharpCacheSettings settings, MethodContext context)
8988
}
9089
}
9190

91+
private string GetPrefixKey(string name)
92+
{
93+
return _settings.Prefix + ":" + name;
94+
}
95+
9296
private string GetCacheKeyByArg(object? arg)
9397
{
9498
if (arg is null)
@@ -104,4 +108,9 @@ private string GetCacheKeyByArg(object? arg)
104108
return arg.GetHashCode().ToString();
105109
}
106110
}
111+
112+
public async Task ClearCacheAsync()
113+
{
114+
await _cache.ClearCacheAsync(_settings.Prefix);
115+
}
107116
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@
188188
</ItemGroup>
189189

190190
<ItemGroup>
191-
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
192191
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
193192
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.8.0" />
194193
<PackageReference Include="Fluid.Core" Version="2.11.1" />

src/Infrastructure/BotSharp.Core/Infrastructures/Cache/MemoryCacheService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using BotSharp.Abstraction.Infrastructures;
22
using Microsoft.Extensions.Caching.Memory;
3+
using Microsoft.Extensions.Options;
34

45
namespace BotSharp.Core.Infrastructures;
56

67
public class MemoryCacheService : ICacheService
78
{
8-
private readonly IMemoryCache _cache;
9+
private static readonly MemoryCache _cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions()));
910

10-
public MemoryCacheService(IMemoryCache memoryCache)
11+
public MemoryCacheService()
1112
{
12-
this._cache = memoryCache;
1313
}
1414

1515
public async Task<T?> GetAsync<T>(string key)
@@ -34,4 +34,9 @@ public async Task RemoveAsync(string key)
3434
{
3535
_cache.Remove(key);
3636
}
37+
38+
public async Task ClearCacheAsync(string prefix)
39+
{
40+
_cache.Compact(1.0);
41+
}
3742
}

src/Infrastructure/BotSharp.Core/Infrastructures/Cache/RedisCacheService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Infrastructures;
22
using Newtonsoft.Json;
33
using StackExchange.Redis;
4+
using System.Linq;
45

56
namespace BotSharp.Core.Infrastructures;
67

@@ -51,4 +52,18 @@ public async Task RemoveAsync(string key)
5152
var db = _redis.GetDatabase();
5253
await db.KeyDeleteAsync(key);
5354
}
55+
56+
public async Task ClearCacheAsync(string prefix)
57+
{
58+
var db = _redis.GetDatabase();
59+
var server = _redis.GetServer(_redis.GetEndPoints().First());
60+
const int pageSize = 1000;
61+
var keys = server.Keys(pattern: $"{prefix}*", pageSize: pageSize).ToList();
62+
63+
for (int i = 0; i < keys.Count; i += pageSize)
64+
{
65+
var batch = keys.Skip(i).Take(pageSize).ToArray();
66+
await db.KeyDeleteAsync(batch);
67+
}
68+
}
5469
}

src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ public static (string, string, string) SplitAsTuple(this string str, string sep)
4848
public static void ClearCache()
4949
{
5050
// Clear whole cache.
51-
if (new MemoryCacheAttribute(0).Cache is MemoryCache memcache)
52-
{
53-
memcache.Compact(100);
54-
}
51+
var sharpCache = new SharpCacheAttribute(0);
52+
sharpCache.ClearCacheAsync().ConfigureAwait(false).GetAwaiter().GetResult();
5553
}
5654

5755
public static string HideMiddleDigits(string input, bool isEmail = false)

src/Infrastructure/BotSharp.Core/Using.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@
4242
global using BotSharp.Core.Conversations.Services;
4343
global using BotSharp.Core.Infrastructures;
4444
global using BotSharp.Core.Users.Services;
45-
global using Aspects.Cache;
4645
global using BotSharp.Abstraction.Infrastructures.Events;

0 commit comments

Comments
 (0)