Skip to content

Commit

Permalink
⚡️🎨调整缓存获取All Key,使用Redis 原生指令Scan提高性能
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNoCry committed Nov 9, 2024
1 parent 2c1c098 commit d79ca4e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
9 changes: 5 additions & 4 deletions Blog.Core.Common/Caches/Caching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ public async Task<bool> ExistsAsync(string cacheKey)
return res != null;
}

public List<string> GetAllCacheKeys(string key = default)
public List<string> GetAllCacheKeys(string pattern = default)
{
if (_redisOptions.Enable)
{
var redis = App.GetService<IConnectionMultiplexer>(false);
var endpoints = redis.GetEndPoints();
var server = redis.GetServer(endpoints[0]);
var keys = server.Keys(pattern: key);
return keys.Select(u => u.ToString()).ToList();

// 使用 SCAN 命令来增量获取符合条件的键,避免 KEYS 的性能问题
return server.Keys(pattern: pattern, pageSize: 100).Select(key => key.ToString()).ToList();
}

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

return memoryCacheManager.GetAllKeys().WhereIf(!key.IsNullOrEmpty(), s => s.StartsWith(key!)).ToList();
return memoryCacheManager.GetAllKeys().WhereIf(!pattern.IsNullOrEmpty(), s => s.StartsWith(pattern!)).ToList();
}

public T Get<T>(string cacheKey)
Expand Down
11 changes: 8 additions & 3 deletions Blog.Core.Common/Caches/Extensions/MemoryCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ public static class MemoryCacheExtensions
{
#region Microsoft.Extensions.Caching.Memory_6_OR_OLDER

/// <summary>
/// 6.x <br/>
/// 6.0.2 调整了字段名,使用 StringKeyEntriesCollection
/// </summary>
private static readonly Lazy<Func<MemoryCache, object>> GetEntries6 = new(() =>
(Func<MemoryCache, object>)Delegate.CreateDelegate(typeof(Func<MemoryCache, object>),
typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)
?.GetGetMethod(true) ?? throw new InvalidOperationException("Cannot find property 'EntriesCollection' on MemoryCache."),
throwOnBindFailure: true));
typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)?.GetGetMethod(true)
?? typeof(MemoryCache).GetProperty("StringKeyEntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance)?.GetGetMethod(true)
?? throw new InvalidOperationException("Cannot find property 'EntriesCollection' or 'StringKeyEntriesCollection' on MemoryCache."),
true));

#endregion

Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Common/Caches/Interface/ICaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ICaching
bool Exists(string cacheKey);
Task<bool> ExistsAsync(string cacheKey);

List<string> GetAllCacheKeys(string key = default);
List<string> GetAllCacheKeys(string pattern = default);

T Get<T>(string cacheKey);
Task<T> GetAsync<T>(string cacheKey);
Expand Down
2 changes: 1 addition & 1 deletion Blog.Core.Common/Caches/MemoryCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MemoryCacheManager : IMemoryCache
{
private readonly IOptions<MemoryCacheOptions> _optionsAccessor;

private IMemoryCache _inner;
private MemoryCache _inner;

public MemoryCacheManager(IOptions<MemoryCacheOptions> optionsAccessor)
{
Expand Down

0 comments on commit d79ca4e

Please sign in to comment.