Skip to content

Commit d79ca4e

Browse files
committed
⚡️🎨调整缓存获取All Key,使用Redis 原生指令Scan提高性能
1 parent 2c1c098 commit d79ca4e

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

Blog.Core.Common/Caches/Caching.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ public async Task<bool> ExistsAsync(string cacheKey)
6464
return res != null;
6565
}
6666

67-
public List<string> GetAllCacheKeys(string key = default)
67+
public List<string> GetAllCacheKeys(string pattern = default)
6868
{
6969
if (_redisOptions.Enable)
7070
{
7171
var redis = App.GetService<IConnectionMultiplexer>(false);
7272
var endpoints = redis.GetEndPoints();
7373
var server = redis.GetServer(endpoints[0]);
74-
var keys = server.Keys(pattern: key);
75-
return keys.Select(u => u.ToString()).ToList();
74+
75+
// 使用 SCAN 命令来增量获取符合条件的键,避免 KEYS 的性能问题
76+
return server.Keys(pattern: pattern, pageSize: 100).Select(key => key.ToString()).ToList();
7677
}
7778

7879
var memoryCache = App.GetService<IMemoryCache>();
@@ -86,7 +87,7 @@ public List<string> GetAllCacheKeys(string key = default)
8687
return [];
8788
}
8889

89-
return memoryCacheManager.GetAllKeys().WhereIf(!key.IsNullOrEmpty(), s => s.StartsWith(key!)).ToList();
90+
return memoryCacheManager.GetAllKeys().WhereIf(!pattern.IsNullOrEmpty(), s => s.StartsWith(pattern!)).ToList();
9091
}
9192

9293
public T Get<T>(string cacheKey)

Blog.Core.Common/Caches/Extensions/MemoryCacheExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ public static class MemoryCacheExtensions
99
{
1010
#region Microsoft.Extensions.Caching.Memory_6_OR_OLDER
1111

12+
/// <summary>
13+
/// 6.x <br/>
14+
/// 6.0.2 调整了字段名,使用 StringKeyEntriesCollection
15+
/// </summary>
1216
private static readonly Lazy<Func<MemoryCache, object>> GetEntries6 = new(() =>
1317
(Func<MemoryCache, object>)Delegate.CreateDelegate(typeof(Func<MemoryCache, object>),
14-
typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)
15-
?.GetGetMethod(true) ?? throw new InvalidOperationException("Cannot find property 'EntriesCollection' on MemoryCache."),
16-
throwOnBindFailure: true));
18+
typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)?.GetGetMethod(true)
19+
?? typeof(MemoryCache).GetProperty("StringKeyEntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)?.GetGetMethod(true)
20+
?? throw new InvalidOperationException("Cannot find property 'EntriesCollection' or 'StringKeyEntriesCollection' on MemoryCache."),
21+
true));
1722

1823
#endregion
1924

Blog.Core.Common/Caches/Interface/ICaching.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface ICaching
1515
bool Exists(string cacheKey);
1616
Task<bool> ExistsAsync(string cacheKey);
1717

18-
List<string> GetAllCacheKeys(string key = default);
18+
List<string> GetAllCacheKeys(string pattern = default);
1919

2020
T Get<T>(string cacheKey);
2121
Task<T> GetAsync<T>(string cacheKey);

Blog.Core.Common/Caches/MemoryCacheManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class MemoryCacheManager : IMemoryCache
1010
{
1111
private readonly IOptions<MemoryCacheOptions> _optionsAccessor;
1212

13-
private IMemoryCache _inner;
13+
private MemoryCache _inner;
1414

1515
public MemoryCacheManager(IOptions<MemoryCacheOptions> optionsAccessor)
1616
{

0 commit comments

Comments
 (0)