当前位置: 首页 > article >正文

redis之list核心命令演示与细节探索

redis之list核心命令演示与细节探索

BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 6.2.0

BLPOP key [key …] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0

BRPOP

BRPOP key [key …] timeout
移除并且得到集合中的最后一个元素,或者阻塞直到有一个元素(或者超时报错)

summary: Remove and get the last element in a list, or block until one is available

since: 2.0.0

> lrange test:list1 0 -1

> lrange test:list2 0 -1
list2_0
> lrange test:list3 0 -1
list3_0
> brpop test:list1 test:list2 test:list3 0
test:list2
list2_0

说明:

  1. test:list1中没有元素,test:list2中有元素,test:list3中有元素,brpop test:list1 test:list2 test:list3 0会依次检查test:list1,test:list2,test:list3三个list,如果有则从右边弹出元素,并返回弹出元素的key和弹出的元素
  2. 如果test:list1 test:list2 test:list3中都没有元素,会一直阻塞,直到其中有一个有元素,返回弹出元素的key和弹出的元素
  3. 超时时间单位为秒,0表示无限期阻塞

BRPOPLPUSH source destination timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 2.2.0

LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0

LINSERT key BEFORE|AFTER pivot element
summary: Insert an element before or after another element in a list
since: 2.2.0

LLEN key
summary: Get the length of a list
since: 1.0.0

LMOVE source destination LEFT|RIGHT LEFT|RIGHT
summary: Pop an element from a list, push it to another list and return it
since: 6.2.0

LPOP key [count]
summary: Remove and get the first elements in a list
since: 1.0.0

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
summary: Return the index of matching elements on a list
since: 6.0.6

LPUSH key element [element …]
summary: Prepend one or multiple elements to a list
since: 1.0.0

LPUSHX

LPUSHX key element [element …]
summary: Prepend an element to a list, only if the list exists

如果list存在,则从左边往里面添加元素

since: 2.2.0

# key test:list:lpushx 不存在
> exists test:list:lpushx
0
# 如果key不存在lpushx失败
> lpushx test:list:lpushx 0
0
# key test:list:lpushx 依然不存在
> exists test:list:lpushx
0
# lpush 一个元素使key test:list:lpushx 存在
> lpush test:list:lpushx 0
1
# key  test:list:lpushx 存在
> exists test:list:lpushx
1
# 如果key test:list:lpushx 存在则lpushx成功
> lpush test:list:lpushx 1
2
> LRANGE  test:list:lpushx 0 -1
1
0

LRANGE

LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0

说明:

  1. 计算offset有两种方式
    • 从头到尾,第一个元素的offset是0,第二个是1,以此类推
    • 从尾到头计算,倒数第一个元素的offset是-1,倒数第二个元素是-2,以此类推

LREM

LREM key count element
summary: Remove elements from a list
从list中删除传入的元素
count=0时移除所有的
count>0时从左往右移除 count个
count<0时从右往左移除 -count个
since: 1.0.0

@Test
void listlrem() {
    String key = "test:list";
    redisTemplate.delete(key);
    ListOperations<String, String> ops = redisTemplate.opsForList();
    ops.leftPushAll(key,"v1","v2","v1","v3","v1","v1");
    System.out.println(ops.range(key,0,-1));
    int count = 0;
    // int count = 2;
    // int count = -2;
    ops.remove(key,count,"v1");
    System.out.println(ops.range(key,0,-1));
}

count = 0时删除全部的v1

[v1, v1, v3, v1, v2, v1]
[v3, v2]

count = 2时从左往右删除2个v1

[v1, v1, v3, v1, v2, v1]
[v3, v1, v2, v1]

count = -2时从右往左删除2个v1

[v1, v1, v3, v1, v2, v1]
[v1, v1, v3, v2]

LSET key index element
summary: Set the value of an element in a list by its index
since: 1.0.0

LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0

RPOP key [count]
summary: Remove and get the last elements in a list
since: 1.0.0

RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
since: 1.2.0

RPUSH key element [element …]
summary: Append one or multiple elements to a list
since: 1.0.0

RPUSHX key element [element …]
summary: Append an element to a list, only if the list exists
since: 2.2.0


http://www.kler.cn/a/290777.html

相关文章:

  • LLM漫谈(八)| OpenAI 12天直播集锦
  • STM32-笔记17-PWM波型
  • Redis - Token JWT 概念解析及双token实现分布式session存储实战
  • 网络安全之接入控制
  • 常用的Django模板语言
  • .NET常用的ORM框架及性能优劣分析总结
  • 如何在算家云搭建OpenSora 1.2(文本生成视频)
  • SpringMvc项目异常处理方案
  • 计算机领域学术会议(ICCBD+AI 2024)
  • 缓存和数据库缓存有什么区别
  • VISION TRANSFORMER ADAPTER FORDENSE PREDICTIONS
  • 页面小组件-表格封装(基础版)
  • idea新建父工程和添加导入新模块的步骤
  • Gin 验证器详解与案例分析
  • 代码随想录第50天|图论
  • 【并行计算】CUDA基础
  • 行为型设计模式-命令(command)模式-python实现
  • C++判断语句(基础速通)ac-wing
  • OpenAI发布GPT-4o mini,3.5从此退出历史舞台?
  • 10.9 网络安全概述
  • watchdog: BUG: soft lockup - CPU#3 stuck for 23s! [swapper/0:1]
  • 微信小程序跳转到另一个微信小程序
  • 2025第十二届广州国际汽车零部件加工技术及汽车模具展览会
  • 替代 Django 默认 User 模型并使用 `django-mysql` 添加数据库备注20240904
  • 国内PFMEA的实施困境与价值探讨
  • 天气预报爬虫