当前位置:网站首页>go : go-redis list operation
go : go-redis list operation
2022-07-30 08:03:00 【pedestrians have】
春江潮水连海平,海上明月共潮生!!!
具体代码在: https://gitee.com/hjx_RuGuoYunZhiDao/strom-huang-go/blob/master/go_redis/go_redis_list.go
1、初始化redis
参考上一篇:https://blog.csdn.net/bei_FengBoby/article/details/124756296
2、list操作
2.1、LPushX & LPush (插入数据)
//config.RedisDb is to separate out the link as a method reference,Let's talk about the pit here:
// RedisDb Capital letters indicate public methods,Lowercase letters indicate private methods
//LPush : The list does not exist at this time,依然可以插入
n, err := config.RedisDb.LPush("testList", "tonoy1").Result()
fmt.Println("number:", n)
err = config.RedisDb.LPush("testList", "tonoy1", "tonoy3", "tonoy2").Err()
if err != nil {
//redis连接错误
panic(err)
}
//LPushX:仅当KeyInsert data when it exists,The list does not exist at this time,无法插入
t, err := config.RedisDb.LPushX("testList1", "aaa").Result()
fmt.Println("number:", t)
2.2、LRange & LLen (读取list指定坐标的值)
// 返回从0开始到-1位置之间的数据,意思就是返回全部数据
vals, err := config.RedisDb.LRange("testList", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(vals)
// 返回从0开始到2 位置之间的数据,意思就是返回全部数据
vals2, err := config.RedisDb.LRange("testList", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(vals2)
//返回list集合中的长度
testList, err := config.RedisDb.LLen("testList").Result()
if err != nil {
panic(err)
}
2.3、LSet & LInsert (替换listThe value of the specified coordinate in )
bee, err := config.RedisDb.LSet("testList", 2, "bee").Result()
fmt.Print(bee)
//在list列表testList 中值为bee 前面添加元素hello : 第二个参数有:after ,前面/后面
config.RedisDb.LInsert("testList", "before", "bee", "hello")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
config.RedisDb.LInsert("testList", "after", "bee", "a")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
2.4 LTrim & LIndex (截取 /返回 istThe value of the specified coordinate in )
// 截取(可以理解为删除)名称为testList 的 key,And assign the truncated value to testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList listthe third value in
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
2.5 LPop & LRem (删除 值 )
//从列表左边删除第一个数据,并返回删除的数据 It can be understood as deleting the last data
config.RedisDb.LPop("testList")
//删除列表中的数据.删除10个值为beedata repeating elements
config.RedisDb.LRem("testList", 10, "bee")
3、所有代码
package main
import (
"fmt"
config "strom-huang-go/go_redis/config"
)
func main() {
err := config.InitRedisClient()
if err != nil {
//redis连接错误
panic(err)
}
fmt.Println("Redis连接成功")
// -------------------------list-操作 -----------------------------------
//LPushX & LPush
//LPush : The list does not exist at this time,依然可以插入
n, err := config.RedisDb.LPush("testList", "tonoy1").Result()
fmt.Println("number:", n)
err = config.RedisDb.LPush("testList", "tonoy1", "tonoy3", "tonoy2").Err()
if err != nil {
//redis连接错误
panic(err)
}
//LPushX:仅当KInsert data when it exists,The list does not exist at this time,无法插入
t, err := config.RedisDb.LPushX("testList1", "aaa").Result()
fmt.Println("number:", t)
// LRange & LLen
// 返回从0开始到-1位置之间的数据,意思就是返回全部数据
vals, err := config.RedisDb.LRange("testList", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(vals)
// 返回从0开始到2 位置之间的数据,意思就是返回全部数据
vals2, err := config.RedisDb.LRange("testList", 0, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(vals2)
//返回list集合中的长度
testList, err := config.RedisDb.LLen("testList").Result()
if err != nil {
panic(err)
}
fmt.Println("testList集合的长度为:", testList)
// LTrim & LIndex
// 截取(可以理解为删除)名称为testList 的 key,And assign the truncated value to testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList listthe third value in
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
//LSet & LInsert
//替换 testList the third data
bee, err := config.RedisDb.LSet("testList", 2, "bee").Result()
fmt.Print(bee)
//在list列表testList 中值为bee 前面添加元素hello : 第二个参数有:after ,前面/后面
config.RedisDb.LInsert("testList", "before", "bee", "hello")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
config.RedisDb.LInsert("testList", "after", "bee", "a")
//config.RedisDb.LInsertBefore("studentList","lilei","hello") 执行效果一样
//LPop & LRem示例
//从列表左边删除第一个数据,并返回删除的数据 It can be understood as deleting the last data
config.RedisDb.LPop("testList")
//删除列表中的数据.删除10个值为beedata repeating elements
config.RedisDb.LRem("testList", 10, "bee")
}
边栏推荐
- 2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
- 进制转换。。。
- MySQL什么时候用表锁,什么时候用行锁?
- Distance calculation from space vertex to straight line and its source code
- The Society of Mind - Marvin Minsky
- 预测人们对你的第一印象,“AI颜狗”的诞生
- From catching up to surpassing, domestic software shows its talents
- 不会吧,Log4j 漏洞还没有完全修复?
- 包含min函数的栈(js)
- “AI教练”请进家,家庭智能健身蓬勃发展
猜你喜欢

图解关系数据库设计思想,这也太形象了

这个终端连接工具,碾压Xshell

Rodrigues: vector representation of rotation matrices

MySQL master-slave replication configuration construction, one step in place

你被MySQL 中的反斜杠 \\坑过吗?

【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现

便携小风扇PD取电芯片

MYSQL下载及安装完整教程

阿里二面:列出 Api 接口优化的几个技巧

What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles
随机推荐
MYSQL下载及安装完整教程
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
New material under the plastic restriction order - polylactic acid (PLA)
When does MySQL use table locks and when does it use row locks?
How to use Swagger, say goodbye to postman
MySQL题外篇【ORM思想解析】
Upload file -- file type, picture type, document type, video type, compressed package type
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
Keil软件中map文件解析
华为发布“十大发明”,包含计算、智能驾驶等新领域
Go 使用 freecache 缓存
go : 使用 grom 删除数据库数据
Headline 2: there are several kinds of common SQL errors in MySQL usage?
uniapp中canvas与v-if更“配”
Go 使用mencached缓存
Station B collapsed, what would you do if you were the developer in charge that night?
Electron使用romote报错 : Uncaught TypeError: Cannot read property ‘BrowserWindow‘ of undefined
Electron之初出茅庐——搭建环境并运行第一个程序
Develop common tool software
STL源码剖析:迭代器的概念理解,以及代码测试。