当前位置:   article > 正文

安利两个超好用的的go redis框架_redis golang好用的库

redis golang好用的库

redigo

github地址

go安装go get github.com/garyburd/redigo/redis

在这里插入图片描述

该仓库换了新的仓库,建议都使用新仓库。如下:

go get github.com/gomodule/redigo/redis

该框架以近似原生的方式使用redis,命令及参数都作为函数参数传递,功能强大。

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

  1. 创建会话

在github仓库的redis目录下conn.go是客户端连接服务的方法:

在这里插入图片描述

// 常用的ip与端口连接
Dial(network, address string, options ...DialOption)
// 地址url连接
DialURL(rawurl string, options ...DialOption) (Conn, error)
// 携带上下文连接的方法
DialURLContext(ctx context.Context, rawurl string, options ...DialOption) (Conn, error)

// 新建连接
NewConn(netConn net.Conn, readTimeout, writeTimeout time.Duration) Conn
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. 客户端实例
// Conn represents a connection to a Redis server.
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.
	// This function will use the timeout which was set when the connection is created
	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)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

客户端实例都是Conn类型。

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

type redisClient struct {
	redis.Conn
}

var RedisConn redisClient

func init() {
	c, err := redis.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		fmt.Println("conn redis failed,", err)
		return
	}
	RedisConn = redisClient{c}
	fmt.Println("redis conn success")
	RedisConn.Do("auth", "123456")
	fmt.Println("redis auth success")
}

func (c *redisClient) Close() {
	c.Close()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

redis的基本操作都是通过Do方法完成。

redis-cli的基本命令如下是COMMAND KEY_NAME args...,如下:

在这里插入图片描述
在这里插入图片描述

更多命令参考

对于字符串最主要的命令就是set,del,get,expire

在这里插入图片描述
hash表最主要的命令是HMSET ket value或者hset都可,HDEL key field1,HGET key field,HGETALL key

另外还有列表,set,有序数组。

Do方法以近乎命令行的方式操作redis,如下:

RedisConn.Do("set", "test", "hello", "ex", 10)
reply, err := RedisConn.Do("get", "test")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(reply)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
c.Do("set", "key", "value")
c.Do("get", "key")
c.Do("del", "key")
c.Do("hset", "hash", "key", "value")
c.Do("hget", "hash", "key")
c.Do("hdel", "hash", "key")
c.Do("lpush", "list", "value")
c.Do("lrange", "list", 0, 10)
c.Do("lpoop", "list")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

使用起来就和原生redis一样将命令及参数依次传递即可。这也是官方推荐的第三方框架。

go-redis

redis-go官方第三方方框架

go get github.com/redis/go-redis/v9

go-redis框架对redis命令封装了方法,可以使用方法来调用redis命令,例如:

rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "123456", // no password set
		DB:       0,        // use default DB
})


rdb.Set(ctx, "key2", "value2", 0)
rdb.Get(ctx, "key2")
rdb.Del(ctx, "key2")

//
rdb.HMSet(ctx, "hash", map[string]interface{}{
	"field1": "value1",
	"field2": "value2",
})
rdb.HGet(ctx, "hash", "field1")
rdb.HGetAll(ctx, "hash")
rdb.HDel(ctx, "hash", "field1")

//
rdb.SAdd(ctx, "set", "value1", "value2")
rdb.SPop(ctx, "set")
rdb.SRem(ctx, "set", "value1")

//
rdb.LPush(ctx, "list", "value1", "value2")
rdb.LPop(ctx, "list")
rdb.LRem(ctx, "list", 0, "value1")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

需要那种数据类型就调用那个方法即可。

两个框架各有优势,喜欢那个就选择那个喏。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号