redis执行Lua脚本报"attempt to compare number with string"错误的解决方法
来源:互联网 发布:手柄要下载什么软件 编辑:程序博客网 时间:2024/05/01 00:22
最近在一段项目代码里执行一个redis脚本,脚本的作用是获取一个list键中的元素个数,并与设定值(输入参数)比较,如果元素个数小于设定值则返回。测试时发现该脚本会报"attempt to compare number with string"错误,简化的脚本如下:
localhost:6379> eval "local key_len = redis.call('LLEN', KEYS[1]) if key_len < ARGV[1] then return key_len end" 1 myKey 3其中myKey就是要检测的list,设定的list长度为3。执行脚本后redis报错,说试图将数值与字符串相比较:
(error) ERR Error running script (call to f_a51f990b393ac7ebcc1132c458c355afbf8ccfa0):
@user_script:1: user_script:1: attempt to compare number with string
脚本中只有一处比较运算,"LLEN myKey"命令的返回值肯定是整数,那么这就说明不管你是想输入数值还是字符串,redis将所有的输入参数都设为了string类型。其实这也是高效的设计,因为只有程序员自己才知道自己设计脚本的目的,eval命令无法判断程序员到底是想输入数值3还是字符串"3"。所以,在执行与ARVG[1]的比较前,需要显式地调用Lua的tonumber()内建函数将ARGV[1]转化为number类型,脚本修改如下:
localhost:6379> eval "local key_len = redis.call('LLEN', KEYS[1]) if key_len < tonumber(ARGV[1]) then return 0 end" 1 myKey 3再次运行脚本执行正确。
0 0
- redis执行Lua脚本报"attempt to compare number with string"错误的解决方法
- Lua中table.sort() 报错 attempt to compare number with nil和invalid order function for sorting
- redis执行Lua脚本
- redis执行lua脚本
- lua报错,attempt to call global 'setmetatable'(a nil value)或者类似错误
- IE下报 Expected identifier,string or number 错误的解决方法
- AS报错:failed to find target with hash string 'Google Inc.:Google APIs:21'的报错解决方法
- 关于报错:IllegalStateException: attempt to re-open an already-closed 的解决方法
- c#中用lua脚本执行redis命令
- Redis执行Lua脚本详细实例
- spring data redis 执行 lua 脚本
- [LUA ERROR] [string "cocos/cocos2d/Cocos2dConstants.lua"]:620: attempt to index a nil value解决办法
- [LUA ERROR] [string ".\cocos/cocos2d/Cocos2dConstants.lua"]:620: attempt to index a nil value
- 而在主线程中调用yield,会导致 “lua: attempt to yield across metamethod/C-call boundary”的错误。
- 关于AndroidStudio中报Error:Failed to find target with hash string 'Google Inc.:Google APIs:23' 的错误
- Beyond Compare应用程序发生错误的解决方法
- Beyond Compare应用程序发生错误的解决方法
- Beyond Compare应用程序发生错误的解决方法
- 不使用第三方变量,交换两个变量的值
- Jmeter第一次尝试
- How to change debian desktop wallpaper
- [RK3288][Android6.0] Camera HAL ION 使用小结
- 2017年2月规划
- redis执行Lua脚本报"attempt to compare number with string"错误的解决方法
- QTP自动化测试之VBScript基础(下)
- 流量控制与令牌桶算法
- WordPress 无法使用the_content()方法输出内容
- python重载
- 自定义view的构造方法和样式主题
- html技巧总结
- pat-basic-1036-c语言
- Anaconda使用总结