list 类型

list 类型数据基本操作

添加/修改数据

  1. # 从左加入
  2. lpush key value1 [value2] ……
  3. # 从右加入
  4. rpush key value1 [value2] ……

获取数据

  1. # 从左查询 start :开始位置,stop:结束位置
  2. lrange key start stop
  3. lindex key index
  4. llen key

获取并移除数据

  1. # 从最左侧取出(首位处)
  2. lpop key
  3. # 从最右侧取出(末尾处)
  4. rpop key

例子:

  1. c_redis:0>lpush foodList guazi latiao mianbao
  2. "3"
  3. c_redis:0>lrange foodList 0 2
  4. 1) "mianbao"
  5. 2) "latiao"
  6. 3) "guazi"
  7. c_redis:0>lrange foodList 0 -1
  8. 1) "mianbao"
  9. 2) "latiao"
  10. 3) "guazi"
  11. c_redis:0>rpush foodList shui
  12. "4"
  13. c_redis:0>lrange foodList 0 -1
  14. 1) "mianbao"
  15. 2) "latiao"
  16. 3) "guazi"
  17. 4) "shui"
  18. c_redis:0>lpop foodList
  19. "mianbao"
  20. c_redis:0>lrange foodList 0 -1
  21. 1) "latiao"
  22. 2) "guazi"
  23. 3) "shui"
  24. c_redis:0>llen foodList
  25. "3"
  26. c_redis:0>lindex foodList 0
  27. "latiao"
  28. c_redis:0>lindex foodList 4
  29. null

list 类型数据扩展操作

移除指定数据

  1. # count : 删除的个数(可去重)
  2. lrem key count value

规定时间内获取并移除数据

  1. # 从左侧获取第一个值,在规定时间内失效(在规定时间内,立即获取到数据,如果没有在规定时间内,获取到数据,就会等待大概规定时间,返回null值。)
  2. # 可以在等待的时间,进行加入,一旦有值,立刻获取到值
  3. blpop key1 [key2] timeout
  4. # 从右侧获取第一个值,在规定时间内失效
  5. brpop key1 [key2] timeout
  6. # 从右侧的第一个值传到左侧第一个值,在规定时间内失效
  7. brpoplpush source destination timeout

例子:

  1. c_redis:0>lpush paramList a b c a b c a b c b
  2. "10"
  3. c_redis:0>lrange paramList 0 -1
  4. 1) "b"
  5. 2) "c"
  6. 3) "b"
  7. 4) "a"
  8. 5) "c"
  9. 6) "b"
  10. 7) "a"
  11. 8) "c"
  12. 9) "b"
  13. 10) "a"
  14. c_redis:0>lrem paramList 3 b
  15. "3"
  16. c_redis:0>lrange paramList 0 -1
  17. 1) "c"
  18. 2) "a"
  19. 3) "c"
  20. 4) "a"
  21. 5) "c"
  22. 6) "b"
  23. 7) "a"

blpop key1 [key2] timeout执行

  1. c_redis:0>lpush a a1
  2. "1"
  3. c_redis:0>blpop a 30
  4. 1) "a"
  5. 2) "a1"
  6. c_redis:0>lrange a 0 -1
  7. c_redis:0>

此时key为a还存在。
image.png

  1. c_redis:0>blpop a 30

执行完这条命令,此时数据还是有值,需要等待30秒钟后,键值对就会失效。image.png

  1. c_redis:0>blpop a 30
  2. c_redis:0>

此时,彻底执行完毕,数据已经失效。
image.png
应用场景
数据顺序的查看
多台服务器打印输出的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

  1. c_redis:0>rpush logs a:2021-03-05 00:30:25 error
  2. "3"
  3. c_redis:0>rpush logs a:2021-03-05 00:30:24 error
  4. "6"
  5. c_redis:0>rpush logs c:2021-03-05 00:31:24 error
  6. "9"
  7. c_redis:0>lrange logs 0 -1
  8. 1) "a:2021-03-05"
  9. 2) "00:30:25"
  10. 3) "error"
  11. 4) "a:2021-03-05"
  12. 5) "00:30:24"
  13. 6) "error"
  14. 7) "c:2021-03-05"
  15. 8) "00:31:24"
  16. 9) "error"