redis使用lua脚本实现一次查询多个值

/ 后端 / 没有评论 / 141浏览

记录一个查询多个值是否存在的脚本:

    eval
    "local result = {}
    
    for i, id in ipairs(ARGV) do
        if redis.call('EXISTS', id) == 1 then
            table.insert(result, {id, 1})
        else
            table.insert(result, {id, 0})
        end
    end
    
    return result" 0 a b c

 try (Jedis jedis = jedisPool.getResource()) {
            jedis.select(8);
            String script = "" +
                    "local result = {}\n" +
                    "local keys = ARGV\n" +
                    "\n" +
                    "for i, id in ipairs(keys) do\n" +
                    "    if redis.call('EXISTS', id) == 1 then\n" +
                    "        table.insert(result, {id, 1})\n" +
                    "    else  \n" +
                    "        table.insert(result, {id, 0})\n" +
                    "    end  \n" +
                    "end  \n" +
                    "\n" +
                    "return result";
            List<List<String>> result = (List) jedis.eval(script, Collections.singletonList(""), Arrays.asList("a","b","c"));
        }```

- Jedis自带功能
```java
jedis.scriptExists()