当前位置:网站首页>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")
}
边栏推荐
- AI可通过X光片识别种族,但没人知道为什么
- The calculation proof of the intersection of the space line and the plane and its source code
- PXE高效批量网络装机
- 深度学习:线性回归模型
- 识别“数据陷阱”,发现数据的可疑之处
- What new materials are used in the large aircraft C919?
- AI can identify race from X-rays, but no one knows why
- New material under the plastic restriction order - polylactic acid (PLA)
- The calculation of the determinant of the matrix and its source code
- 上传文件--文件类型大全,图片类型,文档类型,视频类型,压缩包类型
猜你喜欢

DNS domain name resolution services

Proof of distance calculation from space vertex to plane and its source code

STL源码剖析:迭代器的概念理解,以及代码测试。

“AI教练”请进家,家庭智能健身蓬勃发展

MySQL主从复制配置搭建,一步到位

什么是微服务?

The first artificial intelligence safety competition officially launched

让百度地图生成器里的“标注”内容展开--解决方案

Data types of Redis6

LVM and disk quotas
随机推荐
B站崩了,如果是你是那晚负责的开发人员你会怎么做?
Test the basics 02
矩阵的行列式的计算及其源码
Redis download and installation
Required request body is missing 问题解决
Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
The first artificial intelligence safety competition officially launched
Bull: remove common characters
分布式系统中的开创者—莱斯利·兰伯特
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
手机端滚动至页面指定位置
How to use Swagger, say goodbye to postman
向量三重积的等式推导证明
STL源码剖析:迭代器的概念理解,以及代码测试。
Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
export , export default,import完整用法
STL源码剖析:临时对象的代码测试和理解
深度学习:线性回归模型
The terminal connection tools, rolling Xshell
window.open()的用法,js打开新窗体