国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Redis > 正文

Redis和Lua使用過程中遇到的小問題

2020-10-28 21:28:48
字體:
來源:轉載
供稿:網友

問題

在 Redis 里執行 get 或 hget 不存在的 key 或 field 時返回值在終端顯式的是 (nil),類似于下面這樣

127.0.0.1:6379> get test_version(nil)

如果在 Lua 腳本中判斷獲取到的值是否為空值時,就會產生比較迷惑的問題,以為判斷空值的話就用 nil 就可以了,然鵝事實卻并不是這樣的,如下所示:

127.0.0.1:6379> get test_version(nil)127.0.0.1:6379> EVAL "local a = redis.call('get',KEYS[1]) print(a) if a == 'nil' then return 1 else return 0 end" 1 test_version test_version(integer) 0

我們來看下執行 Lua 腳本返回結果的數據類型是什么

127.0.0.1:6379> get test_version(nil)127.0.0.1:6379> EVAL "local a = redis.call('get',KEYS[1]) return type(a)" 1 test_version test_version"boolean"

通過上面的腳本可以看到,當 Redis 返回的結果為 (nil) 時候,其真實的數據類型為 boolean,因此我們直接判斷 nil 是有問題的。

Redis 官方文檔

通過翻閱官方文檔,找到下面所示的一段話,

Redis to Lua conversion table.

  • Redis integer reply -> Lua number
  • Redis bulk reply -> Lua string
  • Redis multi bulk reply -> Lua table (may have other Redis data types nested)
  • Redis status reply -> Lua table with a single ok field containing the status
  • Redis error reply -> Lua table with a single err field containing the error
  • Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type

Lua to Redis conversion table.

  • Lua number -> Redis integer reply (the number is converted into an integer)
  • Lua string -> Redis bulk reply
  • Lua table (array) -> Redis multi bulk reply (truncated to the first nil inside the Lua array if any)
  • Lua table with a single ok field -> Redis status reply
  • Lua table with a single err field -> Redis error reply
  • Lua boolean false -> Redis Nil bulk reply.

解決方案

通過官方文檔,我們知道判斷 Lua 腳本返回空值使用,應該直接判斷 true/false,修改判斷腳本如下所示

127.0.0.1:6379> get test_version(nil)127.0.0.1:6379> EVAL "local a = redis.call('get',KEYS[1]) if a == false then return 'empty' else return 'not empty' end" 1 test_version test_version"empty"

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭山县| 霍林郭勒市| 塘沽区| 三亚市| 屏山县| 安宁市| 丰顺县| 那曲县| 许昌县| 霍城县| 长海县| 江北区| 疏附县| 剑河县| 萨嘎县| 桑植县| 天峨县| 南漳县| 宣化县| 离岛区| 永善县| 祁阳县| 横峰县| 张北县| 固始县| 清徐县| 来宾市| 犍为县| 乳山市| 沙洋县| 双桥区| 江陵县| 万州区| 时尚| 东乌| 毕节市| 绥中县| 永寿县| 本溪市| 原阳县| 平乐县|