使用StackExchange.Redis和Lua脚本,如何迭代作为参数/参数传递的键?
我想这样做:(但不起作用)
private RedisResult GetTabularData(List<string> rowIds)
{
const string myScript = "local myTable = {} "
+ "for key in @keys " // Obviously @keys isn't defined here.
// KEYS doesn't seem to work either.
+ "do "
+ "table.insert(myTable, redis.call('smembers', key))"
+ "end "
+ "return myTable ";
var prepared = LuaScript.Prepare(myScript);
return _redisDb.ScriptEvaluate(prepared, rowIds.Select(x => (RedisKey)x));
}
还是我只传递1个值并将其拆分,例如(工作解决方案):
private static async Task<RedisResult[]> GetSetsAsync(string[] setNames)
{
const string myScript = "local resTable = {}"
+ " for key in string.gmatch(@keys, '([^,]+)')"
+ " do"
+ " table.insert(resTable, redis.call('smembers', key))"
+ " end"
+ " return resTable";
var prepared = LuaScript.Prepare(myScript);
var keys = new {keys = (RedisKey) string.Join(",", setNames)};
var res = await _redisDb.ScriptEvaluateAsync(prepared, keys);
return (RedisResult[])res;
}
参考文献: