From 382911de3fcf06a20177394eb4368ca51e4a7614 Mon Sep 17 00:00:00 2001 From: dragon Date: Wed, 16 Apr 2025 12:07:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE=E6=8C=81?= =?UTF-8?q?=E4=B9=85=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/config.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/utils/config.go b/pkg/utils/config.go index 5734d86..c7de0f1 100644 --- a/pkg/utils/config.go +++ b/pkg/utils/config.go @@ -31,13 +31,14 @@ func NewConfigMemory(store string) (Config, error) { data: sync.Map{}, } if store != "" { - item := make(map[string]configContent) + item := make(map[string]ConfigContent) data, err := os.ReadFile(store) - if err == nil && os.IsNotExist(err) { - err := json.Unmarshal(data, &item) - if err != nil { - return nil, err - } + if err != nil && !os.IsNotExist(err) { + return nil, err + } + err = json.Unmarshal(data, &item) + if err != nil { + return nil, err } for key, content := range item { if content.Ttl == nil || time.Now().Before(*content.Ttl) { @@ -49,7 +50,7 @@ func NewConfigMemory(store string) (Config, error) { return ret, nil } -type configContent struct { +type ConfigContent struct { Data string `json:"data"` Ttl *time.Time `json:"ttl,omitempty"` } @@ -60,7 +61,7 @@ func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error { if ttl == -1 { td = nil } - m.data.Store(key, configContent{ + m.data.Store(key, ConfigContent{ Data: value, Ttl: td, }) @@ -69,7 +70,7 @@ func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error { func (m *ConfigMemory) Get(key string) (string, error) { if value, ok := m.data.Load(key); ok { - content := value.(configContent) + content := value.(ConfigContent) if content.Ttl != nil && time.Now().After(*content.Ttl) { return "", os.ErrNotExist } @@ -86,11 +87,11 @@ func (m *ConfigMemory) Delete(key string) error { func (m *ConfigMemory) Close() error { defer m.data.Clear() if m.store != "" { - item := make(map[string]configContent) + item := make(map[string]ConfigContent) now := time.Now() m.data.Range( func(key, value interface{}) bool { - content := value.(configContent) + content := value.(ConfigContent) if content.Ttl == nil || now.Before(*content.Ttl) { item[key.(string)] = content }