Lua scripts for efficient batch counting and removing keys.

This commit is contained in:
Piotr Szmyd
2014-10-15 09:39:06 +02:00
parent 5feb717dc4
commit 044746d511
2 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
using System;
using StackExchange.Redis;
namespace Orchard.Redis.Extensions
{
public static class RedisDatabaseExtensions
{
public static void KeyDeleteWithPrefix(this IDatabase database, string prefix)
{
if (database == null) {
throw new ArgumentException("Database cannot be null", "database");
}
if (string.IsNullOrWhiteSpace(prefix)) {
throw new ArgumentException("Prefix cannot be empty", "database");
}
database.ScriptEvaluate(@"
local keys = redis.call('keys', ARGV[1])
for i=1,#keys,5000 do
redis.call('del', unpack(keys, i, math.min(i+4999, #keys)))
end", values: new RedisValue[] { prefix });
}
public static int KeyCount(this IDatabase database, string prefix) {
if (database == null) {
throw new ArgumentException("Database cannot be null", "database");
}
if (string.IsNullOrWhiteSpace(prefix)) {
throw new ArgumentException("Prefix cannot be empty", "database");
}
var retVal = database.ScriptEvaluate("return table.getn(redis.call('keys', ARGV[1]))", values: new RedisValue[] { prefix });
if (retVal.IsNull)
return 0;
return (int)retVal;
}
}
}

View File

@@ -101,6 +101,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Caching\RedisDatabaseExtensions.cs" />
<Compile Include="Configuration\IRedisConnectionProvider.cs" />
<Compile Include="Configuration\RedisConnectionProvider.cs" />
<Compile Include="MessageBus\RedisMessageBusBroker.cs" />