GolangのRedisクライアント、単機、哨兵、クラスタをサポート


gomodule/redigoの二次パッケージに基づいて、stand-alone sentinel clusterの3つの導入モードにおける統合インタフェースを提供し、redisの導入モードの交換がビジネスに透明になるようにする.

プロジェクトアドレス


Github:https://github.com/letsfire/r...

開発の進捗状況


Mode配備モード
コード完成度
テスト完了度
依存パッケージ
Alone単例,Twemproxy,Codis
100%
100%
sentinel哨兵モード
100%
100%
FZambia/sentinel
clusterクラスタモード
100%
100%
mna/redisc

メソッドの説明

  • サブスクリプションコマンドメソッド定義type SubFunc func(c redis.PubSubConn) (err error)
  • 一般コマンドメソッド定義type ExecFunc func(c redis.Conn) (res interface{}, err error)
  • サブスクリプションコールバックメソッドSub(fn SubFunc)(err error)
  • を実行する
  • コマンドコールバックメソッドExec(fn ExecFunc)(interface{},error)
  • を実行
  • 以下の方法はいずれもExecメソッドをカプセル化し、戻り値のタイプ
  • をフォーマットする.
  • Int(fn ExecFunc) (int, error)
  • Ints(fn ExecFunc) ([]int, error)
  • IntMap(fn ExecFunc) (map[string]int, error)
  • Int64(fn ExecFunc) (int64, error)
  • Int64s(fn ExecFunc) ([]int64, error)
  • Int64Map(fn ExecFunc) (map[string]int64, error)
  • Uint64(fn ExecFunc) (uint64, error)
  • Bool(fn ExecFunc) (bool, error)
  • String(fn ExecFunc) (string, error)
  • StringMap(fn ExecFunc) (map[string]string, error)
  • Strings(fn ExecFunc) ([]string, error)
  • Bytes(fn ExecFunc) ([]byte, error)
  • ByteSlices(fn ExecFunc) ([][]byte, error)
  • Positions(fn ExecFunc) ([]*[2]float64, error)
  • Float64(fn ExecFunc) (float64, error)
  • Float64s(fn ExecFunc) ([]float64, error)
  • Values(fn ExecFunc) ([]interface{}, error)

  • Aloneの例

    var echoStr = "hello world"
        
    var aloneMode = alone.New(
        alone.Addr("192.168.0.110:6379"),
        alone.PoolOpts(
            mode.MaxActive(0),       //      ,  0   
            mode.MaxIdle(0),         //          ,  2*runtime.GOMAXPROCS(0)
            mode.Wait(false),        //          ,  false
            mode.IdleTimeout(0),     //         ,  0   
            mode.MaxConnLifetime(0), //        ,  0   
            mode.TestOnBorrow(nil),  //              ,  nil
        ),
        alone.DialOpts(
            redis.DialReadTimeout(time.Second),    //     ,  time.Second
            redis.DialWriteTimeout(time.Second),   //     ,  time.Second
            redis.DialConnectTimeout(time.Second), //     ,  500*time.Millisecond
            redis.DialPassword(""),                //     ,   
            redis.DialDatabase(0),                 //     ,  0
            redis.DialKeepAlive(time.Minute*5),    //   5*time.Minute
            redis.DialNetDial(nil),                //    dial,  nil
            redis.DialUseTLS(false),               //    TLS,  false
            redis.DialTLSSkipVerify(false),        //        ,  false
            redis.DialTLSConfig(nil),              //   nil,  tls.Config
        ),
    )
    
    var instance = redigo.New(aloneMode)
    
    res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
        return c.Do("ECHO", echoStr)
    })
    
    if err != nil {
        log.Fatal(err)
    } else if res != echoStr {
        log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
    }

    Sentinalの例

    var echoStr = "hello world"
        
    var sentinelMode = sentinel.New(
        sentinel.Addrs([]string{"192.168.0.110:26379"}),
        //       Alone      
        // sentinel.PoolOpts(...),
        // sentinel.DialOpts(...),
        //       ,   sentinel.DialOpts()  
        //              sentinel.DialOpts()   
        // sentinel.SentinelDialOpts()
    )
    
    var instance = redigo.New(sentinelMode)
    
    res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
        return c.Do("ECHO", echoStr)
    })
    
    if err != nil {
        log.Fatal(err)
    } else if res != echoStr {
        log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
    }

    var echoStr = "hello world"
        
    var clusterMode = cluster.New(
        cluster.Nodes([]string{
            "192.168.0.110:30001", "192.168.0.110:30002", "192.168.0.110:30003",
            "192.168.0.110:30004", "192.168.0.110:30005", "192.168.0.110:30006",
        }),
        //       Alone      
        // cluster.PoolOpts(...),
        // cluster.DialOpts(...),
    )
    
    var instance = redigo.New(clusterMode)
    
    res, err := instance.String(func(c redis.Conn) (res interface{}, err error) {
        return c.Do("ECHO", echoStr)
    })
    
    if err != nil {
        log.Fatal(err)
    } else if res != echoStr {
        log.Fatalf("unexpected result, expect = %s, but = %s", echoStr, res)
    }