StackExchange.Redis加载Lua脚本进行模糊查询的批量删除和修改

前言

使用StackExchange.Redis没有直接相关的方法进行模糊查询的批量删除和修改操作,虽然可以通过Scan相关的方法进行模糊查询,例如:HashScan("hashkey", "*key*"),然后再使用相关的方法进行相关的批量操作,但是如果缓存数据量比较大,效率低下,那么可以使用Lua脚本进行模糊查询的批量操作:ScriptEvaluate(LuaScript.Prepare(...))。

 

通过keys进行模糊查询后的批量操作

批量删除

 1             var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379,allowAdmin = true");
 2             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(
 3                 //Redis的keys模糊查询:
 4                 " local ks = redis.call('KEYS', @keypattern) " + //local ks为定义一个局部变量,其中用于存储获取到的keys
 5                 " for i=1,#ks,5000 do " +    //#ks为ks集合的个数, 语句的意思: for(int i = 1; i <= ks.Count; i+=5000)
 6                 "     redis.call('del', unpack(ks, i, math.min(i+4999, #ks))) " + //Lua集合索引值从1为起始,unpack为解包,获取ks集合中的数据,每次5000,然后执行删除
 7                 " end " +
 8                 " return true "
 9                 ),
10                 new { keypattern = "mykey*" });

批量修改

1             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(
2                 " local ks = redis.call('KEYS', @keypattern) " + 
3                 " for i=1,#ks do " +    
4                 "     redis.call('set', ks[i], @value) " +
5                 " end " +
6                 " return true "),
7                 new { keypattern = "mykey*", value = "setval" });

对Hash集合下的key进行模糊查询后的批量操作

批量删除

 1             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(
 2                 " local ks = redis.call('hkeys', @hashid) " +
 3                 " local fkeys = {} " +
 4                 " for i=1,#ks do " +
 5                 //使用string.find进行匹配操作
 6                 "   if string.find(ks[i], @keypattern) then " +
 7                 "      fkeys[#fkeys + 1] = ks[i] " +
 8                 "   end " +
 9                 " end " +
10                 " for i=1,#fkeys,5000 do " +
11                 "   redis.call('hdel', @hashid, unpack(fkeys, i, math.min(i+4999, #fkeys))) " +
12                 " end " +
13                 " return true "
14                 ),
15                 new { hashid = "hkey", keypattern = "^mykey" });   //keypattern为可使用正则表达式

批量修改

 1             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(
 2                 " local ks = redis.call('hkeys', @hashid) " +
 3                 " local fkeys = {} " +
 4                 " for i=1,#ks do " +
 5                 "   if string.find(ks[i], @keypattern) then " +
 6                 "      fkeys[#fkeys + 1] = ks[i] " +
 7                 "   end " +
 8                 " end " +
 9                 " for i=1,#fkeys do " +
10                 "   redis.call('hset', @hashid, fkeys[i], @value) " +
11                 " end " +
12                 " return true "
13                 ),
14                 new { hashid = "hkey", keypattern = "^key", value = "hashValue" });   //keypattern为可使用正则表达式

对Set集合下的值进行模糊查询后的批量操作

批量删除

 1             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(
 2                 " local ks = redis.call('smembers', @keyid) " +
 3                 " local fkeys = {} " +
 4                 " for i=1,#ks do " +
 5                 "   if string.find(ks[i], @keypattern) then " +
 6                 "      fkeys[#fkeys + 1] = ks[i] " +
 7                 "   end " +
 8                 " end " +
 9                 " for i=1,#fkeys,5000 do " +
10                 "   redis.call('srem', @keyid, unpack(fkeys, i, math.min(i+4999, #fkeys))) " +
11                 " end " +
12                 " return true "
13                 ),
14                 new { keyid = "setkey", keypattern = "^myval" });   //keypattern为可使用正则表达式

注意

从 Redis 2.6.0 版本开始,才可通过内置的 Lua 解释器,使用 EVAL 命令对 Lua 脚本进行求值。

 

posted @ 2016-12-15 22:24  skig  阅读(7919)  评论(0编辑  收藏  举报