当前位置:网站首页>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")
}
边栏推荐
- The first artificial intelligence safety competition officially launched
- 阿里二面:列出 Api 接口优化的几个技巧
- “AI教练”请进家,家庭智能健身蓬勃发展
- STL源码剖析:class template explicit specialization代码测试和理解
- sizeof
- 预测人们对你的第一印象,“AI颜狗”的诞生
- 什么是微服务?
- Graphical relational database design ideas, this is too vivid
- 阿里二面:Redis有几种集群方案?我答了4种
- 限塑令下的新材料——聚乳酸(PLA)
猜你喜欢

深度学习:线性回归模型

Playing script killing with AI: actually more involved than me

万能js时间日期格式转换

What new materials are used in the large aircraft C919?

什么是微服务?

The first artificial intelligence safety competition officially launched

便携小风扇PD取电芯片

《心智社会》—马文·明斯基

STL源码剖析:class template explicit specialization代码测试和理解

LVM and disk quotas
随机推荐
什么是微服务?
Electron中设置菜单(Menu),主进程向渲染进程共享数据
Ali two sides: Sentinel vs Hystrix comparison, how to choose?
roslyn folder under bin folder
debian problem
How to understand plucker coordinates (geometric understanding)
万能js时间日期格式转换
首届人工智能安全大赛正式启动
golang: Gorm配置Mysql多数据源
Redis 如何实现防止超卖和库存扣减操作?
Keil软件中map文件解析
便携小风扇PD取电芯片
阿里一面:多线程顺序运行有多少种方法?
【MySQL】MySQL中如何实现分页操作
适合程序员的输入法
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
sizeof
Equation Derivation Proof of Vector Triple Product
C# 使用RestSharp 实现Get,Post 请求(2)
go : go-redis set operations