https://github.com/garyburd/redigo 目前已经迁移到:https://github.com/gomodule/redigo
目前Star最多的莫属于redigo 下面是基本用法
"github.com/garyburd/redigo/redis"
func main() {
conn,err := redis.Dial("tcp","10.1.210.69:6379")
if err != nil {
fmt.Println("connect redis error :",err)
return
}
defer conn.Close()
}
使用conn接口中的do方法执行redis命令 redis命令可参考文档
这里写入的值永远不会过期
字符串操作
_, err = c.Do("SET", "mykey", "superWang")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "mykey"))
如何设置过期呢
_, err = c.Do("SET", "mykey", "superWang", "EX", "5")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "mykey")) // 转化格式
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
is_key_exit, err := redis.Bool(c.Do("EXISTS", "mykey1")) // 转化bool格式
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("exists or not: %v \n", is_key_exit)
}
valueGet, err := redis.Bytes(c.Do("GET", key))
列表操作
_, err = c.Do("lpush", "runoobkey", "redis")
values, _ := redis.Values(c.Do("lrange", "runoobkey", "0", "100"))
for _, v := range values {
fmt.Println(string(v.([]byte)))
}
hash操作
redis.Int(c.Do("hget","user","name")) // 转化int类型
redis.Strings(c.Do('HMGet',"user",'name','age')) // 取两个元素 string加了s
const (
RedisURL = "redis://*****:6379"
redisMaxIdle = 3 //最大空闲连接数
redisIdleTimeoutSec = 240 //最大空闲连接时间
RedisPassword = "*****"
)
// NewRedisPool 返回redis连接池
func NewRedisPool(redisURL string) *redis.Pool {
return &redis.Pool{
MaxIdle: redisMaxIdle,
IdleTimeout: redisIdleTimeoutSec * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL(redisURL)
if err != nil {
return nil, fmt.Errorf("redis connection error: %s", err)
}
//验证redis密码
if _, authErr := c.Do("AUTH", RedisPassword); authErr != nil {
return nil, fmt.Errorf("redis auth password error: %s", authErr)
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
if err != nil {
return fmt.Errorf("ping redis error: %s", err)
}
return nil
},
}
}
hash是一个键值对集合 特别适合用于存储对象
hset key k-v hget key k //原生命令
集合类似 有序集合也是这样存储的元素是string
总结:就是用do来调用原生的命令
package main
import (
"github.com/go-redis/redis"
"fmt"
"sync"
)
var redisdb *redis.Client
// 初始化连接
func initClient() (err error) {
redisdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
// defer redisdb.Close()
_, err = redisdb.Ping().Result()
if err != nil {
return err
}
return nil
}
// 字符串操作
func redisstring() {
// 存/取 字符串key
err := redisdb.Set("score", 100, 0).Err()
if err != nil {
fmt.Printf("set score failed, err:%v\n", err)
return
}
val, err := redisdb.Get("score").Result()
if err != nil {
fmt.Printf("get score failed, err:%v\n", err)
return
}
fmt.Println("score", val)
// 不存在的key
val2, err := redisdb.Get("name").Result()
if err == redis.Nil {
fmt.Println("name does not exist")
} else if err != nil {
fmt.Printf("get name failed, err:%v\n", err)
return
} else {
fmt.Println("name", val2)
}
}
// 哈希操作
func redishash() {
fmt.Println("hash 操作==========================")
article := Article{"222", "3333333", 10, 0}
articleKey := "article16"
redisdb.HMSet(articleKey,ToStringDictionary(&article))
mapOut := redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("\n %s:%s", inx, item)
}
fmt.Print("\n")
redisdb.HSet(articleKey,"content","测试内容")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("\n %s:%s", inx, item)
}
fmt.Print("\n")
view, err := redisdb.HIncrBy(articleKey, "Views", 1).Result()
if err != nil {
fmt.Printf("\n HIncrBy error=%s ", err)
} else {
fmt.Printf("\n HIncrBy Views=%d ", view)
}
fmt.Print("\n")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("\n %s:%s", inx, item)
}
fmt.Print("\n")
redisdb.HMSet("hash_test","name","nieweibo","age","28","height","dsaf")
mapOuts := redisdb.HGetAll("hash_test").Val()
for inx, item := range mapOuts {
fmt.Printf("%s:%s", inx, item)
}
// 不知道为啥 添加hash 第一个总是 空值
// 1) ""
// 2) ""
// 3) "name"
// 4) "nieweibo"
// 5) "age"
// 6) "28"
// 7) "height"
// 8) "dsaf"
}
type Article struct {
Title string
Content string
Views int
Favourites int
}
func ToStringDictionary(m *Article) map[string]interface{} {
ArtMap := make(map[string]interface{}, 0)
ArtMap["Title"] = m.Title
ArtMap["Content"] = m.Content
ArtMap["Views"] = m.Views
ArtMap["Favourites"] = m.Favourites
return ArtMap
}
// 列表操作
func redislist() {
fmt.Println("-----------------------welcome to ListDemo-----------------------")
articleKey := "article"
result,err:=redisdb.RPush(articleKey, "a","b","c").Result() //在名称为 key 的list尾添加一个值为value的元素
if err!=nil {
fmt.Println(err)
return
}
fmt.Println("result:",result)
result,err = redisdb.LPush(articleKey, "d").Result() //在名称为 key 的list头添加一个值为value的元素
if err!=nil {
fmt.Println(err)
return
}
fmt.Println("result:",result)
length, err := redisdb.LLen(articleKey).Result()
if err != nil {
fmt.Println("ListDemo LLen is nil")
}
fmt.Println("length: ", length) // 长度
mapOut,err1:=redisdb.LRange(articleKey,0,100).Result()
if err1!=nil {
fmt.Println(err1)
return
}
for inx, item := range mapOut {
fmt.Printf("\n %d:%s \n", inx, item)
}
}
// 有序集合
func redisExample2() {
zsetKey := "language_rank"
languages := []*redis.Z{
&redis.Z{Score: 90.0, Member: "Golang"},
&redis.Z{Score: 98.0, Member: "Java"},
&redis.Z{Score: 95.0, Member: "Python"},
&redis.Z{Score: 97.0, Member: "JavaScript"},
&redis.Z{Score: 99.0, Member: "C/C++"},
}
// ZADD
num, err := redisdb.ZAdd(zsetKey, languages...).Result()
if err != nil {
fmt.Printf("zadd failed, err:%v\n", err)
return
}
fmt.Printf("zadd %d succ.\n", num)
// 把Golang的分数加10
newScore, err := redisdb.ZIncrBy(zsetKey, 10.0, "Golang").Result()
if err != nil {
fmt.Printf("zincrby failed, err:%v\n", err)
return
}
fmt.Printf("Golang's score is %f now.\n", newScore)
// 取分数最高的3个
ret, err := redisdb.ZRevRangeWithScores(zsetKey, 0, 2).Result()
if err != nil {
fmt.Printf("zrevrange failed, err:%v\n", err)
return
}
for _, z := range ret {
fmt.Println(z.Member, z.Score)
}
// 取95~100分的
op := &redis.ZRangeBy{
Min: "95",
Max: "100",
}
ret, err = redisdb.ZRangeByScoreWithScores(zsetKey, op).Result()
if err != nil {
fmt.Printf("zrangebyscore failed, err:%v\n", err)
return
}
for _, z := range ret {
fmt.Println(z.Member, z.Score)
}
}
func GetRedisClientPool() *redis.Client{
redisdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
PoolSize: 5,})
pong, err := redisdb.Ping().Result()
if err != nil {
fmt.Println(pong, err)
}
return redisdb
}
// 连接池测试
func connectPoolTest() {
fmt.Println("-----------------------welcome to connect Pool Test-----------------------")
client :=GetRedisClientPool()
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
client.Set(fmt.Sprintf("name%d", j), fmt.Sprintf("xys%d", j), 0).Err()
client.Get(fmt.Sprintf("name%d", j)).Result()
}
fmt.Printf("PoolStats, TotalConns: %d, IdleConns: %d\n", client.PoolStats().TotalConns, client.PoolStats().IdleConns);
}()
}
wg.Wait()
}
func main() {
// initClient()
// redisstring()
// redislist()
// redishash()
// redisExample2()
connectPoolTest()
}
score 100
name does not exist
-----------------------welcome to ListDemo-----------------------
result: 33
result: 4
length: 4
// 0:d
// 1:a
// 2:b
// 3:c
//hash 操作==========================
// :
// Title:222
// Content:3333333
// Views:10
// Favourites:0
// content:测试内容
// :
// Title:222
// Content:3333333
// Views:10
// Favourites:0
//content:测试内容
//HIncrBy Views=11
//:
//Title:222
//Content:3333333
// Views:11
//Favourites:0
//content:测试内容
//:name:nieweiboage:28height:dsaf
//zadd 0 succ.
// score 100
// name does not exist
// zadd 5 succ.
// Golang's score is 100.000000 now.
// Golang 100
// C/C++ 99
// Java 98
// Python 95
// JavaScript 97
// Java 98
// C/C++ 99
// Golang 100