当前位置:网站首页>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")
}
边栏推荐
- sql concat()函数
- MySQL题外篇【ORM思想解析】
- C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
- Ali two sides: Sentinel vs Hystrix comparison, how to choose?
- PXE efficient mass network capacity
- 《心智社会》—马文·明斯基
- Graphical relational database design ideas, this is too vivid
- 2020年度总结——品曾经,明得失,展未来
- No, the Log4j vulnerability hasn't been fully fixed yet?
- C# 使用RestSharp 实现Get,Post 请求(2)
猜你喜欢

2020年度总结——品曾经,明得失,展未来

redis实现分布式锁的原理

Ali Ermian: How many cluster solutions does Redis have?I answered 4

相机坐标系,世界坐标系,像素坐标系三者转换,以及OPENGLDEFocal Length和Opengl 的 Fov转换

What happens when @Bean and @Component are used on the same class?

STL源码剖析:临时对象的代码测试和理解

The CTO said I was not advised to use SELECT *, why is that?

AI元学习引入神经科学,医疗效果有望精准提升

mysql高阶语句(一)

你被MySQL 中的反斜杠 \\坑过吗?
随机推荐
阿里二面:Redis有几种集群方案?我答了4种
assert
【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现
AI元学习引入神经科学,医疗效果有望精准提升
Detailed explanation of numpy multidimensional array ndarray
roslyn folder under bin folder
go : create database records using gorm
Redis download and installation
Electron之初出茅庐——搭建环境并运行第一个程序
云服务器零基础部署网站(保姆级教程)
How to use Swagger, say goodbye to postman
Go 使用mencached缓存
redis实现分布式锁的原理
sql concat()函数
深度学习:线性回归模型
interface
STL源码剖析:class template explicit specialization代码测试和理解
go : go-redis 基础操作
Local Implicit Grid Representations for 3D Scenes详解
taro 打包编译报错