Golang 官方推荐使用的 Redis 客户端 redigo

简介: Golang 官方推荐使用的 Redis 客户端 redigo

介绍

在之前的文章介绍过 Golang 操作 Redis 的三方库 go-redis,本文主要介绍另外一个 Golang 操作 Redis 的三方库 redigo,它是 Golang 官方推荐使用的 Redis 客户端。

go-redis 三方库为我们封装了很多函数来执行 Redis 命令,而 redigo 三方库只有一个 Do 函数执行 Redis 命令,更接近使用 redis-cli 操作 Redis,这一点是我个人比较喜欢的,只要熟悉 Redis,就可以轻松使用 redigo 作为客户端操作 Redis,而不需要再去记三方库封装的函数。

使用 go get 命令安装 redigo:

go get github.com/gomodule/redigo/redis

redigo 库中的 Conn 接口是操作 Redis 的主要接口。

type Conn interface {
 // Close closes the connection.
 Close() error
 // Err returns a non-nil value when the connection is not usable.
 Err() error
 // Do sends a command to the server and returns the received reply.
 Do(commandName string, args ...interface{}) (reply interface{}, err error)
 // Send writes the command to the client's output buffer.
 Send(commandName string, args ...interface{}) error
 // Flush flushes the output buffer to the Redis server.
 Flush() error
 // Receive receives a single reply from the Redis server
 Receive() (reply interface{}, err error)
}

阅读 redigo 库的源码,我们可以发现 Conn 接口包含 6 个方法,本文我们主要使用 Do 和 Close。

02

创建连接

redigo 库提供了多个函数创建连接,本文我们使用 Dial 函数创建连接,此外,还可以使用 DialURL 函数和 NewConn 函数创建连接,限于篇幅,本文不准备逐一介绍。

示例代码:

// 连接 redis-server
// 创建连接
c, err := redis.Dial("tcp", ":6379")
if err != nil {
  fmt.Printf("redis.Dial() error:%v", err)
  return
}
// 关闭连接
defer c.Close()

阅读上面这段代码,我们使用 Dial 函数创建连接,需要注意的是,我们不要忘记关闭连接。

使用该方式主要是为了读者朋友们容易理解,建议在生产环境中使用连接池,避免每次执行命令都需要先创建连接,影响性能。

03

string 操作

redigo 执行 Redis 命令的通用方法是使用 Conn 接口的 Do 函数,Do 函数可以发送命令给 Redis 服务器,并返回 Redis 服务器的回复。

Do(commandName string, args ...interface{}) (reply interface{}, err error)

示例代码:

func stringSet(conn redis.Conn) {
 replySet, err := conn.Do("SET", "key1", "value1")
 if err != nil {
  fmt.Println("SET error: ", err)
 }
 fmt.Println(replySet)
}

阅读上面这段代码,我们使用 Do 函数执行 Redis 的 set 命令,限于篇幅,更多关于 string 类型的 redis 命令示例代码,请阅读推送到 GitHub 的本文相关代码。

04

复合类型操作

除了操作字符串,我们还会操作复合类型,redigo 库中的 Args 类型提供了操作复合类型的方法 AddFlat。

func (args Args) AddFlat(v interface{}) Args

写操作:

type User struct {
 ID   int64  `redis:"id"`
 Name string `redis:"name"`
}
// struct
func structAdd(conn redis.Conn) {
 u1 := User{
  ID:   1,
  Name: "name1",
 }
 replyStruct, err := conn.Do("HMSET", redis.Args{}.Add("hkey1").AddFlat(&u1)...)
 if err != nil {
  fmt.Println("struct err: ", err)
 }
 fmt.Println(replyStruct)
}

阅读上面这段代码,我们使用 AddFlat 方法将结构体写入 Redis,需要注意的是结构体字段是可导出的字段名称,并且使用了字段标签 redis。

读操作:

func structValues(conn redis.Conn) {
 v, err := redis.Values(conn.Do("HGETALL", "hkey1"))
 if err != nil {
  fmt.Println("redis.Values() err: ", err)
 }
 // redis.ScanStruct()
 u2 := new(User)
 if err := redis.ScanStruct(v, u2); err != nil {
  fmt.Println("redis.ScanStruct() err: ", err)
 }
 fmt.Printf("%+v\n", u2)
}

阅读上面这段代码,我们使用两个助手函数 Values 和 ScanStruct,将 Redis 服务器的回复解析到 struct,redigo 还提供了助手函数 ScanSlice 将 Redis 服务器的回复解析到 slice。

限于篇幅,关于 map 和 slice 的示例代码,请阅读推送到 GitHub 上的本文相关代码。

05

总结

本文我们介绍了 Golang 官方推荐的操作 Redis 的三方库 redigo,它仅需使用 Do 函数执行 Redis 所有命令,它还提供了很多助手函数帮助我们更加方便操作 Redis。如果读者朋友们对 redigo 感兴趣,建议花时间阅读一遍 redigo 的文档。关于 redigo 更多示例代码,请阅读文章相关代码。

文章相关代码,请点击「阅读原文」

推荐阅读:

Golang 语言中的非类型安全指针

参考资料:

https://github.com/gomodule/redigo 

https://pkg.go.dev/github.com/gomodule/redigo/redis 

https://redis.io/commands 


目录
相关文章
|
NoSQL Redis 数据安全/隐私保护
Redis 最流行的图形化界面下载及使用超详细教程(带安装包)! redis windows客户端下载
文章提供了Redis最流行的图形化界面工具Another Redis Desktop Manager的下载及使用教程,包括如何下载、解压、连接Redis服务器以及使用控制台和查看数据类型详细信息。
6939 6
Redis 最流行的图形化界面下载及使用超详细教程(带安装包)! redis windows客户端下载
|
NoSQL Redis 数据库
Redis 图形化界面下载及使用超详细教程(带安装包)! redis windows下客户端下载
文章提供了Redis图形化界面工具的下载及使用教程,包括如何连接本地Redis服务器、操作键值对、查看日志和使用命令行等功能。
4848 0
Redis 图形化界面下载及使用超详细教程(带安装包)! redis windows下客户端下载
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
1547 3
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
存储 消息中间件 NoSQL
Redis 入门 - C#.NET Core客户端库六种选择
Redis 入门 - C#.NET Core客户端库六种选择
1405 8
|
NoSQL 网络协议 算法
Redis 客户端连接
10月更文挑战第21天
328 1
|
NoSQL 网络协议 Linux
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
270 1
|
NoSQL 网络协议 Linux
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
475 0
|
NoSQL 网络协议 Linux
【Azure Redis】Redis客户端出现15分钟的超时异常
【Azure Redis】Redis客户端出现15分钟的超时异常
401 0
|
缓存 监控 NoSQL
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
419 0

推荐镜像

更多