Redis Eval Script

栏目: Lua · 发布时间: 5年前

内容简介:从Redis 2.6 版本开始,内嵌支持 Lua 环境。通过使用语法:script:Lua脚本 。numkeys:key的参数个数。访问脚本获取key值:

简介

Redis 2.6 版本开始,内嵌支持 Lua 环境。通过使用 EVALEVALSHA 命令可以使用 Lua 解释器来执行脚本。 EVAL和EVALSHA的使用是差不多的(下面有讲区别)。

EVAL命令

语法: EVAL script numkeys key [ key ...] arg [ arg ...] 。

script:Lua脚本 。numkeys:key的参数个数。访问脚本获取key值: , , ... (KEYS必须大写),获取arg值: , , ...(ARGV必须大写)。

127.0.0.1:6379> eval "return {KEYS[1],ARGV[1],ARGV[2]}" 1 1 ONE TWO
1) "1"
2) "ONE"
3) "TWO"

在Lua脚本中可以通过使用 redis.call()或 redis.pcall() 函数调用redis命令。

127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> eval "return redis.call('set','foo','bibi')" 0
OK
127.0.0.1:6379> get foo
"bibi"
127.0.0.1:6379> eval "return redis.call('set',KEYS[1],'bar')" 1 foo
OK

至于redis.call(),redis.pcall()的不同,在于错误提示不一样

127.0.0.1:6379>  eval "return redis.call('setset','yyyy','oo')" 0
(error) ERR Error running script (call to f_9b37f897c55c73737f181184c9781281dc9b45e3): @user_script:1: @user_script: 1: Unknown Redis command called from Lua script
127.0.0.1:6379>  eval "return redis.pcall('setset','yyyy','oo')" 0
(error) @user_script: 1: Unknown Redis command called from Lua script
127.0.0.1:6379>

Redis与Lua数据类型的转换

在Lua脚本通过redis.call()或redis.pcall()执行redis命令,发生数据类型转换:redis命令返回值--------------->lua数据类型。

Lua脚本在Redis内嵌的解释器运行,发生数据类型珠海:lua数据类型-------------->redis命令返回值。

Lua数据类型转Redis类型

  • Lua number  -> Redis integer reply (the number is converted into an integer)
    127.0.0.1:6379> eval "return 9" 0
    (integer) 9
  • Lua string -> Redis bulk reply  bulk类型支持多种数据类型批量返回
    127.0.0.1:6379> eval "return 'hengheng'" 0
    "hengheng"
  • Lua table (array) -> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)
    127.0.0.1:6379> eval "return{'3','rr',4}" 0
    1) "3"
    2) "rr"
    3) (integer) 4
  • Lua table with a single ok field -> Redis status reply ,status类型就是很随意的状态
    127.0.0.1:6379> eval "return {ok='oppp'}" 0
    oppp
  • Lua table with a single err field -> Redis error reply  ,error类型,前面带有括号(error)注明是error类型,区分status类型
    127.0.0.1:6379>  eval "return {err='eeeeeeeeeeee'}" 0
    (error) eeeeeeeeeeee
    127.0.0.1:6379> eval "return {ok='ooooooooooooooo'}" 0
    ooooooooooooooo
  • Lua boolean false -> Redis Nil bulk reply.
    127.0.0.1:6379> eval "return false" 0   
    (nil)

Redis数据类型转lua数据类型:

ok
err

Redis数据类型和lua数据类型并不完全对应:

Lua 的编号如果非整数,应作为字符串返回,因为总是转为Redis整数类型

127.0.0.1:6379> eval "return 90.4231" 0

(integer) 90

在Lua数组中使用nils,会停止转换直到数组的最后。

127.0.0.1:6379> eval "return {2332,'rrr','fff',nil,'gg'}"  0

1) (integer) 2332

2) "rrr"

3) "fff"      

运行Lua脚本的原子性

Redis使用Lua解释器解释Lua脚本过程中,会保证脚本是以原子性的方式执行,也就是脚本的效果要么仍然不可见,要么已经完成 既然是原子性,那当脚本还没运行完时,其他客户端是无法执行命令的。

所以,该脚本就别搞那么复杂吧,当然,看清况,该复杂也没辙。

EVAL和EVALSHA区别

Eval执行的脚本不从缓存里拿,而Evalsha执行的脚本从缓存拿,跟sha1校验码从服务器缓存里拿。(sha1就好像身份证)

命令: EVALSHA sha1 numkeys key [ key ...] arg [ arg ...]

127.0.0.1:6379> script load "return 5"    #往缓存里加入脚本 , 返回sha1校验码 
"4ca238f611c9d0ae4e9a75a5dbac22aedc379801"
127.0.0.1:6379> script exists 4ca238f611c9d0ae4e9a75a5dbac22aedc379801 #是否存在某脚本  
1) (integer) 1
127.0.0.1:6379> evalsha 4ca238f611c9d0ae4e9a75a5dbac22aedc379801 0 #执行脚本
(integer) 5
127.0.0.1:6379> script flush
OK
127.0.0.1:6379> script exists 4ca238f611c9d0ae4e9a75a5dbac22aedc379801 
1) (integer) 0     #0表示不存在了
127.0.0.1:6379>  evalsha 4ca238f611c9d0ae4e9a75a5dbac22aedc379801 0
(error) NOSCRIPT No matching script. Please use EVAL.  #异常了

先写到这,待深入。


以上所述就是小编给大家介绍的《Redis Eval Script》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Struts 2 in Action

Struts 2 in Action

Don Brown、Chad Davis、Scott Stanlick / Manning Publications / 2008.3 / $44.99

The original Struts project revolutionized Java web development and its rapid adoption resulted in the thousands of Struts-based applications deployed worldwide. Keeping pace with new ideas and trends......一起来看看 《Struts 2 in Action》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具