redis如何设置内存淘汰策略 (redis六种数据结构和使用场景)

调试redis+lua

学了lua的基本语法,了解了redis+lua的配套用法,但是却 不知道怎么断点调试 。学就学全面点, 官网中有dubug相关说明。地址:Redis Lua scripts debugger

基础

--script.lua
localping=redis.call('ping')
returnping

#执行脚本--ldb--eval
➜7Debuggit:(main)✗redis-cli--ldb--eval/Users/mw/Desktop/Github/Integration/Redis/7Debug/script.lua
Luadebuggingsessionstarted,pleaseuse:
quit--Endthesession.
restart--Restartthescriptindebugmodeagain.
help--ShowLuascriptdebuggingcommands.

*Stoppedat1,stopreason=stepover
#调试位置
->1localping=redis.call('ping')
#next
luadebugger>n
<redis>ping
<reply>"+PONG"
*Stoppedat2,stopreason=stepover
#调试位置
->2returnping
#打印lua变量
luadebugger>printping
<value>{["ok"]="PONG"}
#next
luadebugger>n
#lua返回的结果
PONG
#调试结束
(Luadebuggingsessionended--datasetchangesrolledback)
#退出redis-cli
127.0.0.1:6379>exit
➜7Debuggit:(main)✗

--script1.lua
--测试接受参数
localsrc=KEYS[1]
localdst=KEYS[2]

localcount=tonumber(ARGV[1])

参数

KEYS & ARGV

传递参数时的注意事项

#传参数:list_alist_b10
➜7Debuggit:(main)✗redis-cli--ldb--eval/Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lualist_alist_b10
Luadebuggingsessionstarted,pleaseuse:
quit--Endthesession.
restart--Restartthescriptindebugmodeagain.
help--ShowLuascriptdebuggingcommands.

*Stoppedat1,stopreason=stepover
->1localsrc=KEYS[1]
#
luadebugger>print
Nolocalvariablesinthecurrentcontext.
#
luadebugger>n
*Stoppedat2,stopreason=stepover
->2localdst=KEYS[2]
#
luadebugger>n
*Stoppedat3,stopreason=stepover
->3localcount=tonumber(ARGV[1])
#打印KEYS全部都是keys
luadebugger>printKEYS
<value>{"list_a";"list_b";"10"}
#打印ARGV
luadebugger>printARGV
<value>{}
#结束
luadebugger>n

(integer)1

(Luadebuggingsessionended--datasetchangesrolledback)

127.0.0.1:6379>

**参数 中 KEYS & ARGV 区分: 逗号隔开** 1. 参数`list_b,`, 逗号与key之间没有空格,则认为依旧是KEYS;

#注意
➜7Debuggit:(main)✗redis-cli--ldb--eval/Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lualist_alist_b,10
Luadebuggingsessionstarted,pleaseuse:
quit--Endthesession.
restart--Restartthescriptindebugmodeagain.
help--ShowLuascriptdebuggingcommands.

*Stoppedat1,stopreason=stepover
->1localsrc=KEYS[1]
#KEYS
luadebugger>printKEYS
<value>{"list_a";"list_b"}
#ARGV
luadebugger>printARGV
<value>{"10"}
luadebugger>
luadebugger>n
*Stoppedat2,stopreason=stepover
->2localdst=KEYS[2]
luadebugger>n
*Stoppedat3,stopreason=stepover
->3localcount=tonumber(ARGV[1])
luadebugger>n
*Stoppedat4,stopreason=stepover
->4returntrue
#
luadebugger>print
<value>src="list_a"
<value>dst="list_b"
<value>count=10
luadebugger>n

(integer)1

(Luadebuggingsessionended--datasetchangesrolledback)

127.0.0.1:6379>

队列操作

将list_a中的一个元素push到list_b

➜7Debuggit:(main)✗redis-cli--ldb--eval/Users/mw/Desktop/Github/Integration/Redis/7Debug/script1.lualist_alist_b,10
Luadebuggingsessionstarted,pleaseuse:
quit--Endthesession.
restart--Restartthescriptindebugmodeagain.
help--ShowLuascriptdebuggingcommands.

*Stoppedat1,stopreason=stepover
->1localsrc=KEYS[1]
#打印keys
luadebugger>printKEYS
<value>{"list_a";"list_b"}
#打印参数
luadebugger>printARGV
<value>{"10"}
#初始化队列list_a、list_b
luadebugger>rlpushlist_a1234
<redis>lpushlist_a1234
<reply>4
luadebugger>rlpushlist_babcd
<redis>lpushlist_babcd
<reply>4
luadebugger>n
*Stoppedat2,stopreason=stepover
->2localdst=KEYS[2]
luadebugger>n
*Stoppedat3,stopreason=stepover
->3localcount=tonumber(ARGV[1])
luadebugger>n
*Stoppedat5,stopreason=stepover
->5localitem=redis.call('rpop',src)
#next执行pop
luadebugger>n
<redis>rpoplist_a
<reply>"1"
*Stoppedat6,stopreason=stepover
->6redis.call('lpush',dst,item)
#next执行push
luadebugger>n
<redis>lpushlist_b1
<reply>5
*Stoppedat7,stopreason=stepover
->7returntrue
#r:redis;获取list元素
luadebugger>rlrangelist_a0-1
<redis>lrangelist_a0-1
<reply>["4","3","2"]
#r:redis;获取list元素
luadebugger>rlrangelist_b0-1
<redis>lrangelist_b0-1
#执行成功
<reply>["1","d","c","b","a"]
#重启,将所有执行回滚
luadebugger>restart

Luadebuggingsessionstarted,pleaseuse:
quit--Endthesession.
restart--Restartthescriptindebugmodeagain.
help--ShowLuascriptdebuggingcommands.

*Stoppedat1,stopreason=stepover
->1localsrc=KEYS[1]
#回滚至无数据
luadebugger>rlrangelist_b0-1
<redis>lrangelist_b0-1
<reply>[]
luadebugger>

断点调试

断点操作

#写入redis准备数据
➜7Debuggit:(main)✗redis-cli
127.0.0.1:6379>lpushlist_a1234
(integer)4
127.0.0.1:6379>lpushlist_babcd
(integer)4
127.0.0.1:6379>llenlist_a
(integer)4
127.0.0.1:6379>llenlist_b
(integer)4
127.0.0.1:6379>

#获取全部文件
luadebugger>w
1localsrc=KEYS[1]
2localdst=KEYS[2]
3localcount=tonumber(ARGV[1])
4
->5whilecount<10do
6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
8redis.call('lpush',dst,item)
9count=count-1
10end
11
12returnredis.call('llen',dst);
#显示第7行【l:list】
luadebugger>l7
2localdst=KEYS[2]
3localcount=tonumber(ARGV[1])
4
->5whilecount<10do
6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
8redis.call('lpush',dst,item)
9count=count-1
10end
11
12returnredis.call('llen',dst);
#显示第7行前后一行【l:list】
luadebugger>l71
6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
8redis.call('lpush',dst,item)
luadebugger>b6
->5whilecount<10do
#6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
#continue继续,直到断点执行
luadebugger>c

(integer)4

(Luadebuggingsessionended--datasetchangesrolledback)

127.0.0.1:6379>

逐步断点

luadebugger>w
->1localsrc=KEYS[1]
2localdst=KEYS[2]
3localcount=tonumber(ARGV[1])
4
5whilecount>0do
6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
8redis.call('lpush',dst,item)
9count=count-1
10end
11
12returnredis.call('llen',dst);
luadebugger>b8
7ifitem==falsethenbreakend
#8redis.call('lpush',dst,item)
9count=count-1
#验证断点位置
luadebugger>w
->1localsrc=KEYS[1]
2localdst=KEYS[2]
3localcount=tonumber(ARGV[1])
4
5whilecount>0do
6localitem=redis.call('rpop',src)
7ifitem==falsethenbreakend
#8redis.call('lpush',dst,item)
9count=count-1
10end
11
12returnredis.call('llen',dst);
#等同与next
luadebugger>c
*Stoppedat8,stopreason=breakpoint
->#8redis.call('lpush',dst,item)

luadebugger>print
<value>src="list_a"
<value>dst="list_b"
<value>count=10
<value>item="1"
luadebugger>c
*Stoppedat8,stopreason=breakpoint
->#8redis.call('lpush',dst,item)
luadebugger>c
*Stoppedat8,stopreason=breakpoint
->#8redis.call('lpush',dst,item)
luadebugger>c
*Stoppedat8,stopreason=breakpoint
->#8redis.call('lpush',dst,item)
#打印日志
luadebugger>print
<value>src="list_a"
<value>dst="list_b"
<value>count=7
<value>item="4"

redis.debug 一步到位

此处有加新代码: redis.debug('dubug value:', item)

luadebugger>w
->1localsrc=KEYS[1]
2localdst=KEYS[2]
3localcount=tonumber(ARGV[1])
4
5whilecount>0do
6localitem=redis.call('rpop',src)
#采用redis.debug
7redis.debug('dubugvalue:',item)
8ifitem==falsethenbreakend
9redis.call('lpush',dst,item)
10count=count-1
11end
12
13returnredis.call('llen',dst);
#继续,打印redis.debug处的日志
luadebugger>c
<debug>line7:"dubugvalue:","1"
<debug>line7:"dubugvalue:","2"
<debug>line7:"dubugvalue:","3"
<debug>line7:"dubugvalue:","4"
<debug>line7:"dubugvalue:",false

(integer)8

(Luadebuggingsessionended--datasetchangesrolledback)

127.0.0.1:6379>