Redis Lua編程與調(diào)試工具使用

前言

Redis自2.6.0版本開(kāi)始內(nèi)置Lua解釋器。

Lua,輕量級(jí)腳本語(yǔ)言,號(hào)稱最快的腳本語(yǔ)言。

兩者結(jié)合將爆發(fā)出巨大的威力。

簡(jiǎn)介

Redis Lua腳本可以調(diào)用原生的Redis命令,也可以包含自定義的控制流、邏輯運(yùn)算、數(shù)學(xué)運(yùn)算等,將復(fù)雜的業(yè)務(wù)邏輯封裝起來(lái),形成一個(gè)原子事務(wù)。

這些特性使我們可以自由地?cái)U(kuò)展Redis,封裝“自定義命令”。

與MULTI+EXEC對(duì)比

使用MULTI+EXEC及相關(guān)組合命令,也可以將多個(gè)命令封裝成事務(wù),但靈活性不如Lua腳本。

除此之外,MULTI+EXEC需要多次向Redis server發(fā)送事務(wù)命令,每次發(fā)送都會(huì)有RTT(Round Trip Time)消耗,性能低于Lua腳本。

Redis Lua Scripts Debugger (LDB)

Redis自3.2版本開(kāi)始包含LDB,用于調(diào)試Lua腳本。

LDB支持設(shè)置斷點(diǎn),逐行執(zhí)行等。

一個(gè)簡(jiǎn)單的例子

執(zhí)行set命令,并設(shè)置新的ttl

編寫(xiě)Lua腳本,文件名為set_ttl.lua

local set_msg = false
-- 執(zhí)行redis命令的返回結(jié)果被封裝成了table,形如{'ok':'OK'}
local key_type = redis.call('TYPE' , KEYS[1])['ok']
-- key的數(shù)據(jù)類型為string或key不存在時(shí),繼續(xù)執(zhí)行
if key_type == 'string' or key_type == 'none' then
    local ttl = tonumber(ARGV[2])
    set_msg = redis.call('SET', KEYS[1], ARGV[1], 'EX', ttl)['ok']
end
return set_msg  -- 正常情況下會(huì)返回"OK"

調(diào)試Lua腳本

命令如下

$ redis-cli --ldb --eval set_ttl.lua fruit , apple 100

其中,set_ttl.lua為腳本名稱,
逗號(hào)前的參數(shù)將存入變量KEYS,逗號(hào)后的參數(shù)將存入變量ARGV,多個(gè)變量用空格隔開(kāi)。

默認(rèn)情況下,LDB將執(zhí)行腳本第一行并停住。
輸入n并回車,執(zhí)行下一行,重復(fù)直到腳本執(zhí)行結(jié)束。

調(diào)試輸出如下。
從調(diào)試輸出的最后部分可以看出

  • 腳本返回了"OK"。
  • 調(diào)試修改的數(shù)據(jù)集被回滾了,調(diào)試并不會(huì)真的修改數(shù)據(jù)庫(kù)
$ redis-cli --ldb --eval set_ttl.lua fruit , apple 100
Lua debugging session started, please use:
quit    -- End the session.
restart -- Restart the script in debug mode again.
help    -- Show Lua script debugging commands.

* Stopped at 1, stop reason = step over
-> 1   local set_msg = false
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3   local key_type = redis.call('TYPE' , KEYS[1])['ok']
lua debugger> n
<redis> TYPE fruit
<reply> "+none"
* Stopped at 5, stop reason = step over
-> 5   if key_type == 'string' or key_type == 'none' then
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6       local ttl = tonumber(ARGV[2])
lua debugger> n
* Stopped at 7, stop reason = step over
-> 7       set_msg = redis.call('SET', KEYS[1], ARGV[1], 'EX', ttl)['ok']
lua debugger> n
<redis> SET fruit apple EX 100
<reply> "+OK"
* Stopped at 9, stop reason = step over
-> 9   return set_msg  -- 正常情況下會(huì)返回"OK"
lua debugger> n

"OK"

(Lua debugging session ended -- dataset changes rolled back)

一個(gè)復(fù)雜的例子

批量獲取并更新hash類型的key,做必要的類型檢查,獲取key的ttl,最終返回每個(gè)key的執(zhí)行結(jié)果

編寫(xiě)lua腳本,文件名getset_hash.lua

local results = {}
for index , key in ipairs(KEYS) do
    local old_value = false
    local mset_msg = false
    local ttl = false
    local key_type = redis.call('TYPE' , key)['ok']
    if key_type == 'hash' then
        old_value = redis.call('HMGET' , key , ARGV[1], ARGV[2])
        ttl = redis.call('TTL', key)
    end
    if key_type == 'hash' or key_type == 'none' then
        mset_msg = redis.call('HMSET' , key , ARGV[1], ARGV[3], ARGV[2], ARGV[4])['ok']
    end
    results[index] = { key, old_value, ttl, mset_msg }
end
return results

調(diào)試輸出

$ redis-cli --ldb --eval getset_hash.lua person1 person2 , name age Alice 22
Lua debugging session started, please use:
quit    -- End the session.
restart -- Restart the script in debug mode again.
help    -- Show Lua script debugging commands.

* Stopped at 1, stop reason = step over
-> 1   local results = {}
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2   for index , key in ipairs(KEYS) do
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3       local old_value = false
lua debugger> n
* Stopped at 4, stop reason = step over
-> 4       local mset_msg = false
lua debugger> n
* Stopped at 5, stop reason = step over
-> 5       local ttl = false
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6       local key_type = redis.call('TYPE' , key)['ok']
lua debugger> n
<redis> TYPE person1
<reply> "+none"
* Stopped at 7, stop reason = step over
-> 7       if key_type == 'hash' then
lua debugger> n
* Stopped at 11, stop reason = step over
-> 11      if key_type == 'hash' or key_type == 'none' then
lua debugger> n
* Stopped at 12, stop reason = step over
-> 12          mset_msg = redis.call('HMSET' , key , ARGV[1], ARGV[3], ARGV[2], ARGV[4])['ok']
lua debugger> n
<redis> HMSET person1 name Alice age 22
<reply> "+OK"
* Stopped at 14, stop reason = step over
-> 14      results[index] = { key, old_value, ttl, mset_msg }

# 略去循環(huán)執(zhí)行的輸出

-> 16  return results
lua debugger> n

1) 1) "person1"
   2) (nil)
   3) (nil)
   4) "OK"
2) 1) "person2"
   2) (nil)
   3) (nil)
   4) "OK"

(Lua debugging session ended -- dataset changes rolled back)

執(zhí)行腳本

命令

$ redis-cli --eval getset_hash.lua person1 person2 , name age Alice 22

查看數(shù)據(jù)

$ redis-cli 
127.0.0.1:6379> keys *
1) "person2"
2) "person1"
127.0.0.1:6379> hgetall person1
1) "name"
2) "Alice"
3) "age"
4) "22"
127.0.0.1:6379> hgetall person2
1) "name"
2) "Alice"
3) "age"
4) "22"

參考鏈接

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容