赞
踩
go安装go get github.com/garyburd/redigo/redis
该仓库换了新的仓库,建议都使用新仓库。如下:
go get github.com/gomodule/redigo/redis
该框架以近似原生的方式使用redis,命令及参数都作为函数参数传递,功能强大。
redigo 库中的 Conn 接口是操作 Redis 的主要接口:
在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
// 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) }
客户端实例都是
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() }
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)
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")
使用起来就和原生redis一样将命令及参数依次传递即可。这也是官方推荐的第三方框架。
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")
需要那种数据类型就调用那个方法即可。
两个框架各有优势,喜欢那个就选择那个喏。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。