You've already forked v2ray-core
refactor config cache
This commit is contained in:
@@ -11,36 +11,26 @@ var (
|
||||
|
||||
type ConfigCreator func() interface{}
|
||||
|
||||
type ConfigLoader interface {
|
||||
RegisterCreator(string, ConfigCreator) error
|
||||
CreateConfig(string) (interface{}, error)
|
||||
Load([]byte) (interface{}, string, error)
|
||||
LoadWithID([]byte, string) (interface{}, error)
|
||||
}
|
||||
type ConfigCreatorCache map[string]ConfigCreator
|
||||
|
||||
type BaseConfigLoader struct {
|
||||
creators map[string]ConfigCreator
|
||||
}
|
||||
|
||||
func NewBaseConfigLoader() *BaseConfigLoader {
|
||||
return &BaseConfigLoader{
|
||||
creators: make(map[string]ConfigCreator),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error {
|
||||
if _, found := this.creators[id]; found {
|
||||
func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
|
||||
if _, found := this[id]; found {
|
||||
return common.ErrDuplicatedName
|
||||
}
|
||||
|
||||
this.creators[id] = creator
|
||||
this[id] = creator
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *BaseConfigLoader) CreateConfig(id string) (interface{}, error) {
|
||||
creator, found := this.creators[id]
|
||||
func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
|
||||
creator, found := this[id]
|
||||
if !found {
|
||||
return nil, ErrUnknownConfigID
|
||||
}
|
||||
return creator(), nil
|
||||
}
|
||||
|
||||
type ConfigLoader interface {
|
||||
Load([]byte) (interface{}, string, error)
|
||||
LoadWithID([]byte, string) (interface{}, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user