修复配置持久化问题

This commit is contained in:
dragon
2025-04-16 12:07:05 +08:00
parent 64182e1ac8
commit 382911de3f

View File

@@ -31,13 +31,14 @@ func NewConfigMemory(store string) (Config, error) {
data: sync.Map{}, data: sync.Map{},
} }
if store != "" { if store != "" {
item := make(map[string]configContent) item := make(map[string]ConfigContent)
data, err := os.ReadFile(store) data, err := os.ReadFile(store)
if err == nil && os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
err := json.Unmarshal(data, &item) return nil, err
if err != nil { }
return nil, err err = json.Unmarshal(data, &item)
} if err != nil {
return nil, err
} }
for key, content := range item { for key, content := range item {
if content.Ttl == nil || time.Now().Before(*content.Ttl) { if content.Ttl == nil || time.Now().Before(*content.Ttl) {
@@ -49,7 +50,7 @@ func NewConfigMemory(store string) (Config, error) {
return ret, nil return ret, nil
} }
type configContent struct { type ConfigContent struct {
Data string `json:"data"` Data string `json:"data"`
Ttl *time.Time `json:"ttl,omitempty"` 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 { if ttl == -1 {
td = nil td = nil
} }
m.data.Store(key, configContent{ m.data.Store(key, ConfigContent{
Data: value, Data: value,
Ttl: td, 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) { func (m *ConfigMemory) Get(key string) (string, error) {
if value, ok := m.data.Load(key); ok { if value, ok := m.data.Load(key); ok {
content := value.(configContent) content := value.(ConfigContent)
if content.Ttl != nil && time.Now().After(*content.Ttl) { if content.Ttl != nil && time.Now().After(*content.Ttl) {
return "", os.ErrNotExist return "", os.ErrNotExist
} }
@@ -86,11 +87,11 @@ func (m *ConfigMemory) Delete(key string) error {
func (m *ConfigMemory) Close() error { func (m *ConfigMemory) Close() error {
defer m.data.Clear() defer m.data.Clear()
if m.store != "" { if m.store != "" {
item := make(map[string]configContent) item := make(map[string]ConfigContent)
now := time.Now() now := time.Now()
m.data.Range( m.data.Range(
func(key, value interface{}) bool { func(key, value interface{}) bool {
content := value.(configContent) content := value.(ConfigContent)
if content.Ttl == nil || now.Before(*content.Ttl) { if content.Ttl == nil || now.Before(*content.Ttl) {
item[key.(string)] = content item[key.(string)] = content
} }