imperugo / StackExchange.Redis.Extensions

MIT License
601 stars 179 forks source link

The SearchKeysAsync method does not behave as expected in the recent NuGet version 10.1.0. #586

Open Madhushankha opened 5 months ago

Madhushankha commented 5 months ago

Describe the bug

Update the StackExchange.Redis.Extensions.Core version from 9.1.0 to 10.1.0. However, the existing unit tests failed after the upgrade.

To Reproduce

Revert the NuGet package to version 9.1.0, debug the code to ascertain the count of keys matching a specific pattern, and confirm that it provides the accurate result. However, when using the newer version of the NuGet package, it consistently returns a count of 0 for keys matching the specified pattern.

Additional context

In the previous version 9.1.0, the code in RedisDatabase.cs at inside SearchKeysAsync method line 424 utilized the Database instance to execute a scan command: var redisResult = await Database.ExecuteAsync("SCAN", nextCursor.ToString(), "MATCH", pattern, "COUNT", "1000").ConfigureAwait(false);

However, in the updated version, a modification is observed. Instead of using the Database instance, the code now employs an instance named unused within a loop iterating over servers:

var servers = ServerIteratorFactory .GetServers(connectionPoolManager.GetConnection(), serverEnumerationStrategy) .ToArray();

foreach (var unused in servers) { var redisResult = await unused.ExecuteAsync("SCAN", nextCursor.ToString(), "MATCH", pattern, "COUNT", "1000").ConfigureAwait(false); // Additional code handling the redisResult may be present here. }

imperugo commented 5 months ago

Hi @Madhushankha

first thanks for the feedback. If I look the code now, it looks good to me and wrong the old version.

The scan now is executed only on the servers that have been retrieved from the enumerate strategy. If u use the Database.ExecuteAsync instead of unused.ExecuteAsync (I know that must be renamed with something that makes sense) there is no sense to iterate the servers.

clinta74 commented 2 months ago

I even found this is true with 10.2.0 as well.

Calling _redisClient.Db0.SearchKeysAsync("YourFirstKey") does return keys. But a second call _redisClient.Db0.SearchKeysAsync("YourSecondKey") returns no results.

If I call _redisClient.ExecuteAsync("SCAN", 0, "MATCH", "YourSecondKey", "COUNT", "1000") before I _redisClient.Db0.SearchKeysAsync("YourSecondKey") they both return the expected results.