当前位置:网站首页>go : go-redis list操作
go : go-redis list操作
2022-07-30 05:51:00 【行人已】
春江潮水连海平,海上明月共潮生!!!
具体代码在: 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 是把链接的单独剔除去当做一个方法引用,这里说一下坑:
// RedisDb 大写的字母表示公用方法,小写字母表示私有方法
//LPush : 此时列表不存在,依然可以插入
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:仅当Key存在的时候才插入数据,此时列表不存在,无法插入
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 (替换list中指定坐标的值)
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 (截取 /返回 ist中指定坐标的值 )
// 截取(可以理解为删除)名称为testList 的 key,并把截取后的值赋值给testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList list中第三个值
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
2.5 LPop & LRem (删除 值 )
//从列表左边删除第一个数据,并返回删除的数据 可以理解为删除最后一个数据
config.RedisDb.LPop("testList")
//删除列表中的数据。删除10个值为bee的数据重复元素
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 : 此时列表不存在,依然可以插入
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:仅当K存在的时候才插入数据,此时列表不存在,无法插入
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,并把截取后的值赋值给testList
val := config.RedisDb.LTrim("testList", 0, 3)
fmt.Println(val)
//返回 testList list中第三个值
index, err := config.RedisDb.LIndex("testList", 2).Result()
fmt.Println(index)
//LSet & LInsert
//替换 testList 中第三条数据
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示例
//从列表左边删除第一个数据,并返回删除的数据 可以理解为删除最后一个数据
config.RedisDb.LPop("testList")
//删除列表中的数据。删除10个值为bee的数据重复元素
config.RedisDb.LRem("testList", 10, "bee")
}
边栏推荐
猜你喜欢

Let the "label" content in Baidu map generator expand--solution

Electron之初出茅庐——搭建环境并运行第一个程序

空间顶点到直线的距离计算及其源码

MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释

PXE efficient mass network capacity

华为发布“十大发明”,包含计算、智能驾驶等新领域

How to understand plucker coordinates (geometric understanding)

LVM和磁盘配额

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

限塑令下的新材料——聚乳酸(PLA)
随机推荐
The CTO said I was not advised to use SELECT *, why is that?
PXE efficient mass network capacity
Distance calculation from space vertex to straight line and its source code
Huawei released "ten inventions", including computing, intelligent driving and other new fields
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
New material under the plastic restriction order - polylactic acid (PLA)
CTO说不建议我使用SELECT * ,这是为什么?
C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
Oracle查看表空间使用率及爆满解决方案
阿里一面:多线程顺序运行有多少种方法?
mysql高阶语句(一)
Test the basics 02
Mobile phone side scroll to page to specify location
Playing script killing with AI: actually more involved than me
proftpd 配置文件说明
进程和计划任务管理
debian problem
空间直线到平面上的交点的计算证明及其源码
你被MySQL 中的反斜杠 \\坑过吗?
Selenium01