Redis Lua 腳本調試是一種強大的工具,可以幫助您快速發現和解決Lua腳本中的問題。它允許您在運行腳本時逐步執行腳本,并檢查每個步驟的結果。
從Redis 3.2開始,內置了 Lua debugger(簡稱LDB),使用Lua debugger可以很方便的對我們編寫的Lua腳本進行調試
開啟 lua dubegger ,將會進入debug命令行。這個模式下 redis 會 fork 一個進程進入隔離環境,不會影響 redis 正常提供服務,但調試期間,原始 redis 執行命令、腳本的結果也不會體現到 fork 之后的隔離環境之中
同步模式,這個模式下,會阻塞 redis 上所有的命令、腳本,直到腳本退出,完全模擬了正式環境使用時候的情況,使用的時候務必注意這點。
/data/lua # redis-cli -a 123456 --ldb --eval /data/lua/pong.luaWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.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 foo = redis.call('ping') lua debugger>
pong.lua 腳本
local foo = redis.call('ping')return foo
圖片
demo1.lua
local src = KEYS[1]local dst = KEYS[2]local count = tonumber(ARGV[1])return true
圖片
lua debugger> print KEYS<value> {"list_a"; "list_b"; "10"}
打印所有的ARGV
lua debugger> print ARGV<value> {"10"}
local src = KEYS[1]local dst = KEYS[2]local count = tonumber(ARGV[1])-- 移除 list_a 列表的最后一個元素,返回值為移除的元素,即:elocal item = redis.call('rpop', src)-- 將 e 值插入到 list_b 列表表頭部redis.call('lpush', dst, item)-- 返回 list_b 列表長度return redis.call('llen', dst)
準備數據
圖片
圖片
注意:KEYS 和 ARGV 使用 , 逗號分隔
local src = KEYS[1]local dst = KEYS[2]local count = tonumber(ARGV[1])while count > 0 do local item = redis.call('rpop', src) redis.debug("value of item: ",item); if item ~= false then redis.call('lpush', dst, item) end count = count - 1 endreturn redis.call('llen', dst)
圖片
如果移除代碼 count = count - 1 ,則會進入系循環
local src = KEYS[1]local dst = KEYS[2]local count = tonumber(ARGV[1])while count > 0 do local username = redis.call('get',src) redis.debug('username : ',username) local age = redis.call('get',dst) redis.debug('age : ',age) count = count - 1end
圖片
lua debugger> b 7 6 local username = redis.call('get',src) #7 redis.debug('username : ',username) 8 local age = redis.call('get',dst)
圖片
圖片
圖片
- 第 9 行打斷點(b 9 ),查看所有代碼,第9行已經被打傷斷點了
圖片
圖片
圖片
-- Copyright (C) ShaoBo Wan (Tinywan)local KEYS1 = KEYS[1]local KEYS2 = KEYS[2]local ARGV1 = ARGV[1]local ARGV2 = ARGV[2]local ARGV3 = ARGV[3]local ARGV4 = ARGV[4]local status, type = next(redis.call('TYPE', KEYS[1])) -- type=none status=ok if status ~= nil and status == 'ok' then if type == 'zset' then -- list = {"10090"; "10089"; "10088"; "10087"; "10086"} local list = redis.call('ZREVRANGEBYSCORE', KEYS[1], ARGV[1], ARGV[2], 'LIMIT', ARGV[3], ARGV[4]) -- 獲取數組table長度:#list local kk = #list -- unpack它接受一個數組(table)作為參數,并默認從下標1開始返回數組的所有元素 local k1, k2, k3, k4 ,k5 = unpack(list) redis.debug('k1 ', k1) -- 10090 redis.debug('k2 ', k2) -- 10089 redis.debug('k3 ', k3) -- 10088 redis.debug('k4 ', k4) -- 10087 redis.debug('k5 ', k5) -- 10087 if list ~= nil and #list > 0 then -- ZREM key member [member ...] redis.call('ZREM', KEYS[1], unpack(list)) -- unpack(list) 返回過期數組的所有元素 -- HMGET key field [field ...] local result = redis.call('HMGET', KEYS[2], unpack(list)) -- ["username:Tinywan,age:24"] -- HDEL key field [field ...] redis.call('HDEL', KEYS[2], unpack(list)) -- 1 return result end endendreturn nil
如果該key不存在,則返回none
如果該key存在,則返回該key數據結構類型,如上返回 zset,表示有序集合。
圖片
重要: 以上消費者腳本會直接刪除有序集合key和所對應的哈希值。所以為了消息的可靠性。通過以上腳本返回的值會存儲在一個stream流中,如果在stream消費失敗(沒有進行ACK機制),則會進入待辦Pending隊列重復消費(知道ACK機制或者刪除該消息隊列)
本文鏈接:http://www.www897cc.com/showinfo-26-59690-0.html深入理解 Redis Lua 腳本調試技巧和最佳實踐
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 淺析五種 React 組件設計模式