mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
gredis增加密码支持
This commit is contained in:
parent
219717eab1
commit
7ba6ccd2fb
@ -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...)
|
||||
}
|
||||
|
14
g/g.go
14
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],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
geg/database/redis/config.yml
Normal file
4
geg/database/redis/config.yml
Normal file
@ -0,0 +1,4 @@
|
||||
# Redis数据库配置
|
||||
redis:
|
||||
default: 127.0.0.1:6379,0,111111
|
||||
cache : 127.0.0.1:6379,1
|
@ -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")
|
||||
|
19
geg/database/redis/gredis2.go
Normal file
19
geg/database/redis/gredis2.go
Normal file
@ -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))
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user