golang go-redisとredigoの異なる使い方
9743 ワード
https://github.com/garyburd/redigo現在の移行先:https://github.com/gomodule/redigo
現在スターが一番多いのはredigoの下にあるのが基本的な使い方です
hashは、オブジェクトを格納するために特に適したキー値対のセットです.
hset key k-v hget key k//原生コマンド
集合類似秩序集合もこのように格納される要素はstringです
まとめ:doで原生のコマンドを呼び出す
go-redisもいろいろなコマンドを呼び出してドキュメントを見ましょうredisのコマンドはよく見ます
現在スターが一番多いのは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
", 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
", 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で原生のコマンドを呼び出す
go-redisもいろいろなコマンドを呼び出してドキュメントを見ましょうredisのコマンドはよく見ます
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
", err)
return
}
val, err := redisdb.Get("score").Result()
if err != nil {
fmt.Printf("get score failed, err:%v
", 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
", 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("
%s:%s", inx, item)
}
fmt.Print("
")
redisdb.HSet(articleKey,"content"," ")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("
%s:%s", inx, item)
}
fmt.Print("
")
view, err := redisdb.HIncrBy(articleKey, "Views", 1).Result()
if err != nil {
fmt.Printf("
HIncrBy error=%s ", err)
} else {
fmt.Printf("
HIncrBy Views=%d ", view)
}
fmt.Print("
")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("
%s:%s", inx, item)
}
fmt.Print("
")
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("
%d:%s
", 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
", err)
return
}
fmt.Printf("zadd %d succ.
", num)
// Golang 10
newScore, err := redisdb.ZIncrBy(zsetKey, 10.0, "Golang").Result()
if err != nil {
fmt.Printf("zincrby failed, err:%v
", err)
return
}
fmt.Printf("Golang's score is %f now.
", newScore)
// 3
ret, err := redisdb.ZRevRangeWithScores(zsetKey, 0, 2).Result()
if err != nil {
fmt.Printf("zrevrange failed, err:%v
", 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
", 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
", 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
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
", err)
return
}
val, err := redisdb.Get("score").Result()
if err != nil {
fmt.Printf("get score failed, err:%v
", 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
", 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("
%s:%s", inx, item)
}
fmt.Print("
")
redisdb.HSet(articleKey,"content"," ")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("
%s:%s", inx, item)
}
fmt.Print("
")
view, err := redisdb.HIncrBy(articleKey, "Views", 1).Result()
if err != nil {
fmt.Printf("
HIncrBy error=%s ", err)
} else {
fmt.Printf("
HIncrBy Views=%d ", view)
}
fmt.Print("
")
mapOut = redisdb.HGetAll(articleKey).Val()
for inx, item := range mapOut {
fmt.Printf("
%s:%s", inx, item)
}
fmt.Print("
")
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("
%d:%s
", 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
", err)
return
}
fmt.Printf("zadd %d succ.
", num)
// Golang 10
newScore, err := redisdb.ZIncrBy(zsetKey, 10.0, "Golang").Result()
if err != nil {
fmt.Printf("zincrby failed, err:%v
", err)
return
}
fmt.Printf("Golang's score is %f now.
", newScore)
// 3
ret, err := redisdb.ZRevRangeWithScores(zsetKey, 0, 2).Result()
if err != nil {
fmt.Printf("zrevrange failed, err:%v
", 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
", 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
", 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