list 类型
list 类型数据基本操作
添加/修改数据
# 从左加入lpush key value1 [value2] ……# 从右加入rpush key value1 [value2] ……
获取数据
# 从左查询 start :开始位置,stop:结束位置lrange key start stoplindex key indexllen key
获取并移除数据
# 从最左侧取出(首位处)lpop key# 从最右侧取出(末尾处)rpop key
例子:
c_redis:0>lpush foodList guazi latiao mianbao"3"c_redis:0>lrange foodList 0 21) "mianbao"2) "latiao"3) "guazi"c_redis:0>lrange foodList 0 -11) "mianbao"2) "latiao"3) "guazi"c_redis:0>rpush foodList shui"4"c_redis:0>lrange foodList 0 -11) "mianbao"2) "latiao"3) "guazi"4) "shui"c_redis:0>lpop foodList"mianbao"c_redis:0>lrange foodList 0 -11) "latiao"2) "guazi"3) "shui"c_redis:0>llen foodList"3"c_redis:0>lindex foodList 0"latiao"c_redis:0>lindex foodList 4null
list 类型数据扩展操作
移除指定数据
# count : 删除的个数(可去重)lrem key count value
规定时间内获取并移除数据
# 从左侧获取第一个值,在规定时间内失效(在规定时间内,立即获取到数据,如果没有在规定时间内,获取到数据,就会等待大概规定时间,返回null值。)# 可以在等待的时间,进行加入,一旦有值,立刻获取到值blpop key1 [key2] timeout# 从右侧获取第一个值,在规定时间内失效brpop key1 [key2] timeout# 从右侧的第一个值传到左侧第一个值,在规定时间内失效brpoplpush source destination timeout
例子:
c_redis:0>lpush paramList a b c a b c a b c b"10"c_redis:0>lrange paramList 0 -11) "b"2) "c"3) "b"4) "a"5) "c"6) "b"7) "a"8) "c"9) "b"10) "a"c_redis:0>lrem paramList 3 b"3"c_redis:0>lrange paramList 0 -11) "c"2) "a"3) "c"4) "a"5) "c"6) "b"7) "a"
blpop key1 [key2] timeout执行
c_redis:0>lpush a a1"1"c_redis:0>blpop a 301) "a"2) "a1"c_redis:0>lrange a 0 -1c_redis:0>
此时key为a还存在。
c_redis:0>blpop a 30
执行完这条命令,此时数据还是有值,需要等待30秒钟后,键值对就会失效。
c_redis:0>blpop a 30c_redis:0>
此时,彻底执行完毕,数据已经失效。
应用场景
数据顺序的查看
多台服务器打印输出的log,方便人员查看。
举例:
服务器A 输出 2021-03-05 00:30:25 error
服务器B 输出 2021-03-05 00:30:24 error
服务器C 输出 2021-03-05 00:31:24 error
c_redis:0>rpush logs a:2021-03-05 00:30:25 error"3"c_redis:0>rpush logs a:2021-03-05 00:30:24 error"6"c_redis:0>rpush logs c:2021-03-05 00:31:24 error"9"c_redis:0>lrange logs 0 -11) "a:2021-03-05"2) "00:30:25"3) "error"4) "a:2021-03-05"5) "00:30:24"6) "error"7) "c:2021-03-05"8) "00:31:24"9) "error"
