From 7ba6ccd2fbf40db1e5430c586b3e55aa053bc63b Mon Sep 17 00:00:00 2001 From: John Date: Tue, 5 Jun 2018 21:18:41 +0800 Subject: [PATCH] =?UTF-8?q?gredis=E5=A2=9E=E5=8A=A0=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/database/gredis/gredis.go | 33 ++++++++++++++++++++------------- g/g.go | 14 ++++++++++---- geg/database/redis/config.yml | 4 ++++ geg/database/redis/gredis.go | 5 ++++- geg/database/redis/gredis2.go | 19 +++++++++++++++++++ geg/other/test.go | 9 +++++++-- 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 geg/database/redis/config.yml create mode 100644 geg/database/redis/gredis2.go diff --git a/g/database/gredis/gredis.go b/g/database/gredis/gredis.go index b6eebeb8e..e8b333504 100644 --- a/g/database/gredis/gredis.go +++ b/g/database/gredis/gredis.go @@ -12,7 +12,7 @@ import ( "time" "github.com/gomodule/redigo/redis" "gitee.com/johng/gf/g/container/gmap" - "gitee.com/johng/gf/g/util/gconv" + "fmt" ) const ( @@ -28,6 +28,14 @@ type Redis struct { pool *redis.Pool } +// Redis服务端但节点连接配置信息 +type Config struct { + Host string // IP/域名 + Port int // 端口 + Db int // db + Pass string // 密码 +} + // Redis链接池统计信息 type PoolStats struct { redis.PoolStats @@ -38,13 +46,9 @@ var pools = gmap.NewStringInterfaceMap() // 创建redis操作对象 // address参数格式 host:port -func New(address string, db ... interface{}) *Redis { - r := &Redis{} - dialDb := 0 - if len(db) > 0 { - dialDb = gconv.Int(db[0]) - } - poolKey := address + "," + gconv.String(dialDb) +func New(config Config) *Redis { + r := &Redis{} + poolKey := fmt.Sprintf("%s:%d,%d", config.Host, config.Port, config.Db) if v := pools.Get(poolKey); v == nil { pool := &redis.Pool { MaxIdle : gDEFAULT_POOL_MAX_IDLE, @@ -52,11 +56,14 @@ func New(address string, db ... interface{}) *Redis { IdleTimeout : gDEFAULT_POOL_IDLE_TIMEOUT, MaxConnLifetime : gDEFAULT_POOL_MAX_LIFE_TIME, Dial : func() (redis.Conn, error) { - c, err := redis.Dial("tcp", address) + c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", config.Host, config.Port)) if err != nil { return nil, err } - c.Do("SELECT", dialDb) + c.Do("SELECT", config.Db) + if len(config.Pass) > 0 { + c.Do("AUTH", config.Pass) + } return c, nil }, } @@ -69,7 +76,7 @@ func New(address string, db ... interface{}) *Redis { return r } -// 关闭链接 +// 关闭链接,将底层的redis对象放回池中 func (r *Redis) Close() error { return r.conn.Close() } @@ -99,12 +106,12 @@ func (r *Redis) Stats() *PoolStats { return &PoolStats{r.pool.Stats()} } -// 执行命令 - Do +// 执行同步命令 - Do func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) { return r.conn.Do(command, args...) } -// 执行命令 - Send +// 执行异步命令 - Send func (r *Redis) Send(command string, args ...interface{}) error { return r.conn.Send(command, args...) } diff --git a/g/g.go b/g/g.go index 430ab82be..b2cd29f55 100644 --- a/g/g.go +++ b/g/g.go @@ -8,7 +8,6 @@ package g import ( - "strings" "gitee.com/johng/gf/g/os/gcfg" "gitee.com/johng/gf/g/os/gview" "gitee.com/johng/gf/g/util/gconv" @@ -20,6 +19,7 @@ import ( "gitee.com/johng/gf/g/net/ghttp" "gitee.com/johng/gf/g/net/gtcp" "gitee.com/johng/gf/g/net/gudp" + "gitee.com/johng/gf/g/util/gregx" ) const ( @@ -138,10 +138,16 @@ func Redis(name...string) *gredis.Redis { return nil } if m := config.GetMap("redis"); m != nil { + // host:port[,db[,pass]] if v, ok := m[group]; ok { - array := strings.Split(gconv.String(v), ",") - if len(array) > 1 { - return gredis.New(array[0], array[1]) + array, err := gregx.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v)) + if err == nil { + return gredis.New(gredis.Config{ + Host : array[1], + Port : gconv.Int(array[2]), + Db : gconv.Int(array[3]), + Pass : array[4], + }) } } } diff --git a/geg/database/redis/config.yml b/geg/database/redis/config.yml new file mode 100644 index 000000000..eb332f9a1 --- /dev/null +++ b/geg/database/redis/config.yml @@ -0,0 +1,4 @@ +# Redis数据库配置 +redis: + default: 127.0.0.1:6379,0,111111 + cache : 127.0.0.1:6379,1 diff --git a/geg/database/redis/gredis.go b/geg/database/redis/gredis.go index d08c0119f..3bcaae8c7 100644 --- a/geg/database/redis/gredis.go +++ b/geg/database/redis/gredis.go @@ -7,7 +7,10 @@ import ( ) func main() { - redis := gredis.New("127.0.0.1:6379", 1) + redis := gredis.New(gredis.Config{ + Host : "127.0.0.1", + Port : 6379, + }) defer redis.Close() redis.Do("SET", "k1", "v1") redis.Do("SET", "k2", "v2") diff --git a/geg/database/redis/gredis2.go b/geg/database/redis/gredis2.go new file mode 100644 index 000000000..fdcd1ffa6 --- /dev/null +++ b/geg/database/redis/gredis2.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/util/gconv" +) + +func main() { + redis := g.Redis() + defer redis.Close() + redis.Do("SET", "k1", "v1") + redis.Do("SET", "k2", "v2") + v1, _ := redis.Do("GET", "k1") + v2, _ := redis.Do("GET", "k1") + fmt.Println(gconv.String(v1)) + fmt.Println(gconv.String(v2)) +} + diff --git a/geg/other/test.go b/geg/other/test.go index 19cbc6b79..a6757e723 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,9 +1,14 @@ package main import ( - "gitee.com/johng/gf/geg/other/sleep" + "fmt" + "gitee.com/johng/gf/g/util/gregx" ) func main() { - sleep.Test() + a , e := gregx.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, "127.0.0.1:12333") + fmt.Println(e) + for k, v := range a { + fmt.Printf("%d:%v\n", k, v) + } } \ No newline at end of file