【Go】go-redis使用

点击阅读更多查看文章内容

go-redis

redis/go-redis: Redis Go client

文档:Golang Redis客户端

安装:go get github.com/redis/go-redis/v9

建立连接:

1
2
3
4
5
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // 没有密码,默认值
DB: 0, // 默认DB 0
})

Set/Get:

1
2
3
4
5
6
7
8
9
ctx := context.Background()
err := rdb.Set(ctx, "key", "value", 10*time.Second).Err()
if err != nil {
panic(err)
}
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
作者

ShiHaonan

发布于

2025-03-03

更新于

2025-03-20

许可协议

评论