-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(StackExchange.Redis): add new project and extend functionality
- Added a new project "ES.FX.StackExchange.Redis" to the solution. - Included this new project as a reference in "ES.FX.Ignite.StackExchange.Redis". - Created DatabaseExtensions class in the new project with methods for deleting keys matching a pattern in batches.
- Loading branch information
1 parent
3b89401
commit 8062bb9
Showing
5 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 14 additions & 14 deletions
28
src/ES.FX.Ignite.StackExchange.Redis/ES.FX.Ignite.StackExchange.Redis.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" /> | ||
<PackageReference Include="StackExchange.Redis" Version="2.8.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="AspNetCore.HealthChecks.Redis" Version="8.0.1" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ES.FX.Ignite.Spark\ES.FX.Ignite.Spark.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\ES.FX.Ignite.Spark\ES.FX.Ignite.Spark.csproj" /> | ||
<ProjectReference Include="..\ES.FX.StackExchange.Redis\ES.FX.StackExchange.Redis.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using JetBrains.Annotations; | ||
using StackExchange.Redis; | ||
|
||
namespace ES.FX.StackExchange.Redis; | ||
|
||
/// <summary> | ||
/// Extensions for <see cref="IDatabase" /> | ||
/// </summary> | ||
[PublicAPI] | ||
public static class DatabaseExtensions | ||
{ | ||
/// <summary> | ||
/// LUA script to delete all keys matching a pattern in batches | ||
/// </summary> | ||
private const string DeleteAllWithPatternBatchedScript = @" | ||
local cursor = '0' | ||
local batchSize = tonumber(ARGV[2]) | ||
local totalDeleted = 0 | ||
local keys | ||
repeat | ||
keys = {} | ||
local result = redis.call('SCAN', cursor, 'MATCH', ARGV[1], 'COUNT', batchSize) | ||
cursor = result[1] | ||
for i, key in ipairs(result[2]) do | ||
table.insert(keys, key) | ||
end | ||
if #keys > 0 then | ||
redis.call('DEL', unpack(keys)) | ||
totalDeleted = totalDeleted + #keys | ||
end | ||
until cursor == '0' | ||
return totalDeleted | ||
"; | ||
|
||
/// <summary> | ||
/// Deletes all keys matching a pattern in batches | ||
/// </summary> | ||
/// <param name="database">The <see cref="IDatabase" /></param> | ||
/// <param name="pattern">Pattern to MATCH keys</param> | ||
/// <param name="batchSize">The batch size</param> | ||
/// <returns>The deleted key count</returns> | ||
public static long KeysDelete(this IDatabase database, string pattern, uint batchSize = 1000) | ||
{ | ||
var result = database.ScriptEvaluate(DeleteAllWithPatternBatchedScript, values: [pattern, batchSize]); | ||
return long.Parse(result.ToString()); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes all keys matching a pattern in batches | ||
/// </summary> | ||
/// <param name="database">The <see cref="IDatabase" /></param> | ||
/// <param name="pattern">Pattern to MATCH keys</param> | ||
/// <param name="batchSize">The batch size</param> | ||
/// <returns>The deleted key count</returns> | ||
public static async Task<long> KeysDeleteAsync(this IDatabase database, string pattern, uint batchSize = 1000) | ||
{ | ||
var result = | ||
await database.ScriptEvaluateAsync(DeleteAllWithPatternBatchedScript, values: [pattern, batchSize]); | ||
return long.Parse(result.ToString()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/ES.FX.StackExchange.Redis/ES.FX.StackExchange.Redis.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" /> | ||
<PackageReference Include="StackExchange.Redis" Version="2.8.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |