From a235047d18965a61e79ccbe15f72feb4151b7fdd Mon Sep 17 00:00:00 2001 From: catcherwong Date: Sat, 9 Apr 2022 10:00:29 +0800 Subject: [PATCH] fix: SearchKeys with optional count and add SearchKeys Async for IRedisCachingProvider --- build/releasenotes.props | 12 +++--- build/version.props | 40 +++++++++---------- .../DefaultCSRedisCachingProvider.Keys.cs | 17 ++++++++ src/EasyCaching.Core/IRedisCachingProvider.cs | 8 ++++ .../DefaultRedisCachingProvider.Keys.cs | 22 +++++++--- .../EasyCaching.Redis.csproj | 1 + 6 files changed, 68 insertions(+), 32 deletions(-) diff --git a/build/releasenotes.props b/build/releasenotes.props index 93f2c552..e928c30e 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,14 +1,14 @@ - 1. Upgrading dependencies. - + 1. IRedisCachingProvider Support SearchKeysAsync. + 1. Upgrading dependencies. - 1. Fix removebyprefix when enable keyprefix. - + 1. IRedisCachingProvider Support SearchKeysAsync. + 1. Upgrading dependencies. @@ -37,8 +37,8 @@ 1. Upgrading dependencies. - 1. Upgrading dependencies. - + 1. IRedisCachingProvider Support SearchKeysAsync. + 1. Upgrading dependencies. diff --git a/build/version.props b/build/version.props index 2ad801b0..307c5c50 100644 --- a/build/version.props +++ b/build/version.props @@ -1,24 +1,24 @@ - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 - 1.5.1 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 + 1.5.2 diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs index 72c542de..fbba8564 100755 --- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs +++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Keys.cs @@ -106,5 +106,22 @@ public List SearchKeys(string cacheKey, int? count) return keys; } + + public async Task> SearchKeysAsync(string cacheKey, int? count) + { + var keys = new List(); + + long nextCursor = 0; + do + { + var scanResult = await _cache.ScanAsync(nextCursor, cacheKey, count ?? 250); + nextCursor = scanResult.Cursor; + var items = scanResult.Items; + keys.AddRange(items); + } + while (nextCursor != 0); + + return keys; + } } } diff --git a/src/EasyCaching.Core/IRedisCachingProvider.cs b/src/EasyCaching.Core/IRedisCachingProvider.cs index c8183813..7b482194 100644 --- a/src/EasyCaching.Core/IRedisCachingProvider.cs +++ b/src/EasyCaching.Core/IRedisCachingProvider.cs @@ -73,6 +73,14 @@ public interface IRedisCachingProvider /// /// List SearchKeys(string cacheKey, int? count = null); + + /// + /// Searchs the keys. + /// + /// + /// + /// + Task> SearchKeysAsync(string cacheKey, int? count = null); #endregion #region String diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs index 0539ab65..5179078c 100755 --- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs +++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Keys.cs @@ -132,19 +132,29 @@ public async Task EvalAsync(string script, string cacheKey, List public List SearchKeys(string cacheKey, int? count) { - var keys = new List(); + var data = new List(); + var server = _servers.ToArray()[0]; + var keys = server.Keys(_cache.Database, pattern: cacheKey, count ?? 250).ToList(); + foreach (var item in keys) + { + data.Add(item.ToString()); + } - keys = server.Keys(_cache.Database, pattern: cacheKey, count.HasValue?250: count.Value).ToList(); + return data; + } + public async Task> SearchKeysAsync(string cacheKey, int? count) + { var data = new List(); - if (keys.Count <= 0) - return data; - - foreach (var item in keys) + + var server = _servers.ToArray()[0]; + var keys = server.KeysAsync(_cache.Database, pattern: cacheKey, count ?? 250); + await foreach (var item in keys) { data.Add(item.ToString()); } + return data; } } diff --git a/src/EasyCaching.Redis/EasyCaching.Redis.csproj b/src/EasyCaching.Redis/EasyCaching.Redis.csproj index 292d88e9..fd0c6316 100644 --- a/src/EasyCaching.Redis/EasyCaching.Redis.csproj +++ b/src/EasyCaching.Redis/EasyCaching.Redis.csproj @@ -16,6 +16,7 @@ https://github.com/dotnetcore/EasyCaching https://github.com/dotnetcore/EasyCaching nuget-icon.png + latest $(EasyCachingRedisPackageNotes)