萌新,寫的差輕噴!!!
package mainimport ("fmt""os""os/signal""syscall""time""github.com/gomodule/redigo/redis")var (Pool *redis.Pool)func init() {redisHost := "106.14.39.56:6379"redispass := "foobared"db := 6Pool = newPool(redisHost, redispass, db)close()}func newPool(server string, password string, db int) *redis.Pool {return &redis.Pool{// 最大的空閑串連數,表示即使沒有redis串連時依然可以保持N個閒置串連,而不被清除,隨時處於待命狀態MaxIdle: 3,//最大的空閑串連等待時間,超過此時間後,空閑串連將被關閉IdleTimeout: 240 * time.Second,// 最大的啟用串連數,表示同時最多有N個串連 ,為0事表示沒有限制// MaxActive int// 當連結數達到最大後是否阻塞,如果不的話,達到最大後返回錯誤// Wait boolDial: func() (redis.Conn, error) {c, err := redis.Dial("tcp", server)if err != nil {return nil, err}if _, err := c.Do("AUTH", password); err != nil {c.Close()return nil, err}if _, err := c.Do("SELECT", db); err != nil {c.Close()return nil, err}return c, err},//TestOnBorrow 是一個測試連結可用性的方法TestOnBorrow: func(c redis.Conn, t time.Time) error {_, err := c.Do("PING")return err},}}func close() {c := make(chan os.Signal, 1)signal.Notify(c, os.Interrupt)signal.Notify(c, syscall.SIGTERM)signal.Notify(c, syscall.SIGKILL)go func() {<-cPool.Close()os.Exit(0)}()}func Get(key string) ([]byte, error) {conn := Pool.Get()defer conn.Close()var data []bytedata, err := redis.Bytes(conn.Do("GET", key))if err != nil {return data, fmt.Errorf("error get key %s: %v", key, err)}return data, err}func MGet(key ...interface{}) ([]string, error) {conn := Pool.Get()defer conn.Close()var data []stringdata, err := redis.Strings(conn.Do("MGET", key...))if err != nil {return data, fmt.Errorf("error get key %s: %v", key, err)}return data, err}func Set(key string, value interface{}, args ...interface{}) ([]byte, error) {args_len := len(args)new_args := make([]interface{}, args_len+2)copy(new_args[2:], args)new_args[0] = keynew_args[1] = valueconn := Pool.Get()defer conn.Close()var data []bytedata, err := redis.Bytes(conn.Do("SET", new_args...))if err != nil {return data, fmt.Errorf("error get key %s: %v", key, err)}return data, err}func Mset(args ...interface{}) ([]byte, error) {conn := Pool.Get()defer conn.Close()var data []bytedata, err := redis.Bytes(conn.Do("MSET", args...))if err != nil {return data, fmt.Errorf("error get key: %v", err)}return data, err}// DEL func Del(key string) (int64, error) {conn := Pool.Get()defer conn.Close()var data int64data, err := redis.Int64(conn.Do("DEL", key))if err != nil {return data, fmt.Errorf("error get key %s: %v", key, err)}return data, nil}//設定到期時間(毫秒級)func Pexp(key string, timeint int64) (int64, error) {conn := Pool.Get()defer conn.Close()data, err := redis.Int64(conn.Do("PEXPIRE", key, timeint))if err != nil {return data, fmt.Errorf("error get key %s: %v", key, err)}return data, nil}//同一個類型的操作只需要一個conn.Receive()接受//不同的操作需要多個conn.Receive()//只支援SET功能func Pipe(mapdata map[string][]map[string]interface{}) (interface{}, error) {conn := Pool.Get()defer conn.Close()senddata := []interface{}{"test", 1}for _, v := range mapdata {for _, sdata := range v {senddata = []interface{}{}for sk, sv := range sdata {senddata = append(senddata, sk)senddata = append(senddata, sv)}fmt.Println(senddata)conn.Send("SET", senddata...)}}conn.Flush()data, err := conn.Receive()if err != nil {return data, fmt.Errorf("error get key : %v", err)}return data, nil}func main() {// set 資料// v, err := Set("test", 3, "EX", "180")// if err != nil {// fmt.Println(err)// return// }// data := string(v[:])// fmt.Println(data)// mset 資料// v, err := Mset("test", 2, "test1", "2")// if err != nil {// fmt.Println(err)// return// }// data := string(v[:])// fmt.Println(data)// get 資料// v, err := Get("pool")// data := string(v[:])// fmt.Println(data, err)//MGET 資料// v, err := MGet("test", "test1")// fmt.Println(v, err)//批量擷取資料// key_data := []string{}// fmt.Println(key_data)// v, err := MGet()// data := string(v[:])// fmt.Println(data, err)//刪除資料// v, err := Del("test1")// fmt.Println(v, err)//修改到期時間(毫秒級)// v, err := Pexp("test1", 100000)// if v == 1 {// fmt.Println("修改成功")// }// fmt.Println(v, err)mapa := map[string][]map[string]interface{}{}mapb := map[string]interface{}{"test": 1, "EX": "1000"}mapc := map[string]interface{}{"test1": 2, "EX": "1000"}mapa["SET"] = append(mapa["SET"], mapb)mapa["SET"] = append(mapa["SET"], mapc)v, err := Pipe(mapa)fmt.Println(v, err)}
198 次點擊