mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Lua scripts for efficient batch counting and removing keys.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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" />
|
||||
|
Reference in New Issue
Block a user