diff --git a/lib/ttlmap/ttlmap.go b/lib/ttlmap/ttlmap.go index 3405e742..19bf7094 100644 --- a/lib/ttlmap/ttlmap.go +++ b/lib/ttlmap/ttlmap.go @@ -20,7 +20,7 @@ const ( type ItemWrapper struct { Value interface{} `yaml:"value"` Expiration int64 `yaml:"expiration"` - DoNotFlushToDisk bool + DoNotFlushToDisk bool `yaml:"-"` } type TTLMap struct { @@ -115,6 +115,9 @@ func (t *TTLMap) cleanup() { } func (t *TTLMap) flush() error { + t.mu.Lock() + defer t.mu.Unlock() + if !t.shouldSave { return nil } @@ -165,6 +168,12 @@ func (t *TTLMap) loadFromFile() error { return fmt.Errorf("failed to unmarshal data, err: %v", err) } + if data == nil { + data = make(map[string]*ItemWrapper) + } + + t.mu.Lock() t.data = data + t.mu.Unlock() return nil }