improve default instance name handling

This commit is contained in:
John
2019-07-28 17:37:13 +08:00
parent a36e00efeb
commit 165602ae0a
12 changed files with 32 additions and 22 deletions

View File

@ -50,6 +50,11 @@
1. grpool增加支持阻塞添加任务接口
1. gdb.Model在链式安全的对象创建中增加sync.Pool的使用
1. 增加g.Table快捷方法以方便操作数据表但是得考虑后续模型操作设计特别是脚手架的模型管理
1. 改进ghttp分组路由中对hook的支持方式以便格式与BindHookHandler统一
# DONE
1. gconv完善针对不同类型的判断例如尽量减少sprintf("%v", xxx)来执行string类型的转换

View File

@ -170,7 +170,7 @@ var (
// which is DEFAULT_GROUP_NAME in default.
func New(name ...string) (db DB, err error) {
group := configs.defaultGroup
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
group = name[0]
}
configs.RLock()

View File

@ -110,7 +110,7 @@ func New(config Config) *Redis {
// it returns a redis instance with default group.
func Instance(name ...string) *Redis {
group := DEFAULT_GROUP_NAME
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
group = name[0]
}
v := instances.GetOrSetFuncLock(group, func() interface{} {

View File

@ -77,7 +77,7 @@ func Config(name ...string) *gcfg.Config {
func Database(name ...string) gdb.DB {
config := Config()
group := gdb.DEFAULT_GROUP_NAME
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
group = name[0]
}
key := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
@ -214,7 +214,7 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
func Redis(name ...string) *gredis.Redis {
config := Config()
group := "default"
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
group = name[0]
}
key := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)

View File

@ -192,7 +192,7 @@ func serverProcessInit() {
// 单例模式请保证name的唯一性
func GetServer(name ...interface{}) *Server {
sname := gDEFAULT_SERVER
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
sname = gconv.String(name[0])
}
if s := serverMapping.Get(sname); s != nil {

View File

@ -38,13 +38,13 @@ func (s *Server) Group(prefix ...string) *RouterGroup {
// 获取分组路由对象
func (d *Domain) Group(prefix ...string) *RouterGroup {
if len(prefix) > 0 {
return &RouterGroup{
domain: d,
prefix: prefix[0],
}
group := &RouterGroup{
domain: d,
}
return &RouterGroup{}
if len(prefix) > 0 {
group.prefix = prefix[0]
}
return group
}
// 执行分组路由批量绑定

View File

@ -43,14 +43,22 @@ func GetSession(r *Request) *Session {
}
}
// UpdateSession updates the session with custom map.
func (s *Server) UpdateSession(id string, data map[string]interface{}) {
v := s.sessions.GetOrSetFuncLock(id, func() interface{} {
return gmap.NewStrAnyMap()
}, s.GetSessionMaxAge()*1000)
v.(*gmap.StrAnyMap).Sets(data)
}
// 延迟初始化
func (s *Session) init() {
if len(s.id) == 0 {
s.server = s.request.Server
if id := s.request.Cookie.GetSessionId(); id != "" {
if data := s.server.sessions.Get(id); data != nil {
if v := s.server.sessions.Get(id); v != nil {
s.id = id
s.data = data.(*gmap.StrAnyMap)
s.data = v.(*gmap.StrAnyMap)
return
}
}

View File

@ -35,7 +35,7 @@ var serverMapping = gmap.NewStrAnyMap()
// The parameter <name> is used to specify the TCP server
func GetServer(name ...interface{}) *Server {
serverName := gDEFAULT_SERVER
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
serverName = gconv.String(name[0])
}
return serverMapping.GetOrSetFuncLock(serverName, func() interface{} {

View File

@ -32,7 +32,7 @@ var serverMapping = gmap.NewStrAnyMap()
// 单例模式请保证name的唯一性
func GetServer(name ...interface{}) *Server {
serverName := gDEFAULT_SERVER
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
serverName = gconv.String(name[0])
}
if s := serverMapping.Get(serverName); s != nil {

View File

@ -24,7 +24,7 @@ var (
// The parameter <name> is the name for the instance.
func Instance(name ...string) *Config {
key := DEFAULT_GROUP_NAME
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
key = name[0]
}
return instances.GetOrSetFuncLock(key, func() interface{} {

View File

@ -22,7 +22,7 @@ var (
// The parameter <name> is the name for the instance.
func Instance(name ...string) *View {
key := DEFAULT_INSTANCE_NAME
if len(name) > 0 {
if len(name) > 0 && name[0] != "" {
key = name[0]
}
return instances.GetOrSetFuncLock(key, func() interface{} {

View File

@ -24,11 +24,8 @@ func RedisHandlerGet(r *ghttp.Request) {
if id == "" {
return
}
// 当内存中的SESSION存在时不需要读取Redis
if r.Session.Size() > 0 {
return
}
// SESSION不存在时例如服务重启自动从Redis读取并恢复数据
// 应用服务器一般是多个节点构成的集群,
// 当请求中带有SESSION ID时自动从Redis读取并恢复数据。
value, err := g.Redis().DoVar("GET", id)
if err != nil {
panic(err)