add LevelPrint configuration for glog.Logger; add package internal/instance for grouped instance management feature; add default logger for panic message printing if no logger set in gcron.Cron (#2388)

* improve logging feature, add LevelPrint configuration for glog.Logger; add package internal/instance

* improve command build

* add default logger for panic message printing if no logger set

* up

* fix scheduler when timer triggers in less than one second for package gcron

* up
This commit is contained in:
John Guo
2023-01-06 14:15:30 +08:00
committed by GitHub
parent 5a8b33fa09
commit ae4f14c2e2
22 changed files with 254 additions and 216 deletions

View File

@ -0,0 +1,77 @@
// Copyright GoFrame Author(https://goframe.org). 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://github.com/gogf/gf.
// Package instance provides instances management.
package instance
import (
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/encoding/ghash"
)
const (
groupNumber = 64
)
var (
groups = make([]*gmap.StrAnyMap, groupNumber)
)
func init() {
for i := 0; i < groupNumber; i++ {
groups[i] = gmap.NewStrAnyMap(true)
}
}
func getGroup(key string) *gmap.StrAnyMap {
return groups[int(ghash.DJB([]byte(key))%groupNumber)]
}
// Get returns the instance by given name.
func Get(name string) interface{} {
return getGroup(name).Get(name)
}
// Set sets an instance to the instance manager with given name.
func Set(name string, instance interface{}) {
getGroup(name).Set(name, instance)
}
// GetOrSet returns the instance by name,
// or set instance to the instance manager if it does not exist and returns this instance.
func GetOrSet(name string, instance interface{}) interface{} {
return getGroup(name).GetOrSet(name, instance)
}
// GetOrSetFunc returns the instance by name,
// or sets instance with returned value of callback function `f` if it does not exist
// and then returns this instance.
func GetOrSetFunc(name string, f func() interface{}) interface{} {
return getGroup(name).GetOrSetFunc(name, f)
}
// GetOrSetFuncLock returns the instance by name,
// or sets instance with returned value of callback function `f` if it does not exist
// and then returns this instance.
//
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
// with mutex.Lock of the hash map.
func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
return getGroup(name).GetOrSetFuncLock(name, f)
}
// SetIfNotExist sets `instance` to the map if the `name` does not exist, then returns true.
// It returns false if `name` exists, and `instance` would be ignored.
func SetIfNotExist(name string, instance interface{}) bool {
return getGroup(name).SetIfNotExist(name, instance)
}
// Clear deletes all instances stored.
func Clear() {
for i := 0; i < groupNumber; i++ {
groups[i].Clear()
}
}

View File

@ -0,0 +1,44 @@
// Copyright GoFrame Author(https://goframe.org). 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://github.com/gogf/gf.
package instance_test
import (
"testing"
"github.com/gogf/gf/v2/internal/instance"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_SetGet(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
instance.Set("test-user", 1)
t.Assert(instance.Get("test-user"), 1)
t.Assert(instance.Get("none-exists"), nil)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(instance.GetOrSet("test-1", 1), 1)
t.Assert(instance.Get("test-1"), 1)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(instance.GetOrSetFunc("test-2", func() interface{} {
return 2
}), 2)
t.Assert(instance.Get("test-2"), 2)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(instance.GetOrSetFuncLock("test-3", func() interface{} {
return 3
}), 3)
t.Assert(instance.Get("test-3"), 3)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(instance.SetIfNotExist("test-4", 4), true)
t.Assert(instance.Get("test-4"), 4)
t.Assert(instance.SetIfNotExist("test-4", 5), false)
t.Assert(instance.Get("test-4"), 4)
})
}