mirror of
https://gitee.com/johng/gf
synced 2026-06-24 00:40:38 +08:00
gredis改进为单例操作方式,每次操作redis服务器时开发者无需显示调用Close方法
This commit is contained in:
@ -24,7 +24,6 @@ const (
|
||||
|
||||
// Redis客户端
|
||||
type Redis struct {
|
||||
conn redis.Conn
|
||||
pool *redis.Pool
|
||||
}
|
||||
|
||||
@ -80,13 +79,12 @@ func New(config Config) *Redis {
|
||||
} else {
|
||||
r.pool = v.(*redis.Pool)
|
||||
}
|
||||
r.conn = r.pool.Get()
|
||||
return r
|
||||
}
|
||||
|
||||
// 关闭链接,将底层的redis对象放回池中
|
||||
// 关闭redis管理对象,将会关闭底层的
|
||||
func (r *Redis) Close() error {
|
||||
return r.conn.Close()
|
||||
return r.pool.Close()
|
||||
}
|
||||
|
||||
// 设置属性 - MaxIdle
|
||||
@ -116,11 +114,15 @@ func (r *Redis) Stats() *PoolStats {
|
||||
|
||||
// 执行同步命令 - Do
|
||||
func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) {
|
||||
return r.conn.Do(command, args...)
|
||||
conn := r.pool.Get()
|
||||
defer conn.Close()
|
||||
return conn.Do(command, args...)
|
||||
}
|
||||
|
||||
// 执行异步命令 - Send
|
||||
func (r *Redis) Send(command string, args ...interface{}) error {
|
||||
return r.conn.Send(command, args...)
|
||||
conn := r.pool.Get()
|
||||
defer conn.Close()
|
||||
return conn.Send(command, args...)
|
||||
}
|
||||
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://gitee.com/johng/gf.
|
||||
|
||||
// go test *.go -bench=".*"
|
||||
|
||||
package gredis_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 这里需要修改为本地配置文件的目录地址
|
||||
g.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame")
|
||||
}
|
||||
|
||||
func Benchmark_New(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
g.Redis()
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_NewAndClose(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
g.Redis().Close()
|
||||
}
|
||||
}
|
||||
@ -19,11 +19,14 @@ import (
|
||||
"gitee.com/johng/gf/g/database/gdb"
|
||||
"gitee.com/johng/gf/g/os/gfsnotify"
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/database/gredis"
|
||||
"gitee.com/johng/gf/g/util/gregex"
|
||||
)
|
||||
|
||||
const (
|
||||
gFRAME_CORE_COMPONENT_NAME_VIEW = "gf.core.component.view"
|
||||
gFRAME_CORE_COMPONENT_NAME_CONFIG = "gf.core.component.config"
|
||||
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
|
||||
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
|
||||
)
|
||||
|
||||
@ -183,6 +186,44 @@ func Database(name...string) *gdb.Db {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Redis操作对象,使用了连接池
|
||||
func Redis(name...string) *gredis.Redis {
|
||||
config := Config()
|
||||
group := "default"
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
key := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)
|
||||
result := instances.GetOrSetFuncLock(key, func() interface{} {
|
||||
if m := config.GetMap("redis"); m != nil {
|
||||
// host:port[,db[,pass]]
|
||||
if v, ok := m[group]; ok {
|
||||
line := gconv.String(v)
|
||||
array, _ := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, line)
|
||||
if len(array) > 4 {
|
||||
return gredis.New(gredis.Config{
|
||||
Host : array[1],
|
||||
Port : gconv.Int(array[2]),
|
||||
Db : gconv.Int(array[3]),
|
||||
Pass : array[4],
|
||||
})
|
||||
} else {
|
||||
panic(fmt.Sprintf(`invalid redis node configuration: "%s"`, line))
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group))
|
||||
}
|
||||
} else {
|
||||
panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.GetFilePath()))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if result != nil {
|
||||
return result.(*gredis.Redis)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 模板内置方法:config
|
||||
func funcConfig(pattern string, file...string) gview.HTML {
|
||||
return gview.HTML(Config().GetString(pattern, file...))
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
package g
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/util/gconv"
|
||||
"gitee.com/johng/gf/g/database/gdb"
|
||||
"gitee.com/johng/gf/g/database/gredis"
|
||||
"gitee.com/johng/gf/g/frame/gins"
|
||||
@ -16,7 +15,6 @@ import (
|
||||
"gitee.com/johng/gf/g/net/gudp"
|
||||
"gitee.com/johng/gf/g/os/gview"
|
||||
"gitee.com/johng/gf/g/os/gcfg"
|
||||
"gitee.com/johng/gf/g/util/gregex"
|
||||
)
|
||||
|
||||
// HTTPServer单例对象
|
||||
@ -57,27 +55,5 @@ func DB(name...string) *gdb.Db {
|
||||
|
||||
// Redis操作对象,使用了连接池
|
||||
func Redis(name...string) *gredis.Redis {
|
||||
group := "default"
|
||||
if len(name) > 0 {
|
||||
group = name[0]
|
||||
}
|
||||
config := gins.Config()
|
||||
if config == nil {
|
||||
return nil
|
||||
}
|
||||
if m := config.GetMap("redis"); m != nil {
|
||||
// host:port[,db[,pass]]
|
||||
if v, ok := m[group]; ok {
|
||||
array, err := gregex.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],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return gins.Redis(name...)
|
||||
}
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"gitee.com/johng/gf/g/database/gredis"
|
||||
)
|
||||
|
||||
// 使用原生gredis.New操作redis,但是注意需要自己调用Close方法关闭redis链接池
|
||||
func main() {
|
||||
redis := gredis.New(gredis.Config{
|
||||
Host : "127.0.0.1",
|
||||
|
||||
@ -6,11 +6,10 @@ import (
|
||||
"gitee.com/johng/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// 使用框架封装的g.Redis()方法获得redis操作对象单例,不需要开发者显示调用Close方法
|
||||
func main() {
|
||||
redis := g.Redis()
|
||||
defer redis.Close()
|
||||
redis.Do("SET", "k", "v")
|
||||
v, _ := redis.Do("GET", "k")
|
||||
g.Redis().Do("SET", "k", "v")
|
||||
v, _ := g.Redis().Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user