redis lua 脚本与单次调用 [英] redis lua script vs. single calls
本文介绍了redis lua 脚本与单次调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下设置:
- 2 种不同的数据结构:集合、字符串
- 它们在不同的命名空间
*:collections:*
,*:resources:*
- 客户不知道这一点,我每次都尝试获取两个命名空间.
- 基于
exists
,我决定最终要获得哪种数据结构. - 对 redis 的所有调用都是异步完成的 (vert.x redis-mod)
- 2 different datastructures: Sets, Strings
- They are in different namespaces
*:collections:*
,*:resources:*
- The client doesn't know about this and I try to get both namespaces every time.
- Based on
exists
I decide which datastructure to get finally. - all calls to redis are done asynchronous (vert.x redis-mod)
现在我必须决定是将它作为 lua 脚本还是作为单个命令来执行.
Now I have to decide if I execute this as lua script or as single commands.
我想出的lua脚本:
local path = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]
if redis.call('exists',resourcesPrefix..path) == 1 then
return redis.call('get',resourcesPrefix..path)
elseif redis.call('exists',collectionsPrefix..path) == 1 then
return redis.call('smembers',collectionsPrefix..path)
else
return "notFound"
end
单次调用或lua脚本有什么优缺点吗?
Are there any pros and cons for single calls or lua script?
推荐答案
是的,LUA
脚本是 EVALSHA
调用的最佳解决方案:
Yes, LUA
script is a best solution in case of EVALSHA
call:
- 您正在使用 redis 异步工作.所以
LUA
可以帮助您减少代码数量和代码可读性. LUA
情况更快,因为减少了网络通信.
- You are working woth redis asynchronous. So
LUA
helps you to reduce number of code and code readability. LUA
case is faster becouse of reduce network communication.
我认为您只需使用 2 个命令即可编写代码.您的代码中不需要 exists
.
I think you can write your code with just 2 commands. You do not need exists
in your code.
local path = KEYS[1]
local resourcesPrefix = ARGV[1]
local collectionsPrefix = ARGV[2]
local ret
set ret = redis.call('get',resourcesPrefix..path)
if ret then
return ret
end
set ret = redis.call('smembers',collectionsPrefix..path)
if ret then
return ret
end
return "notFound"
这篇关于redis lua 脚本与单次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文