feat(cache): only write when dirty

This commit is contained in:
Jan De Dobbeleer 2022-02-06 13:56:23 +01:00 committed by Jan De Dobbeleer
parent 7a60418297
commit 8ce47273df

View file

@ -19,6 +19,7 @@ type cacheObject struct {
type fileCache struct { type fileCache struct {
cache *concurrentMap cache *concurrentMap
cachePath string cachePath string
dirty bool
} }
func (fc *fileCache) Init(cachePath string) { func (fc *fileCache) Init(cachePath string) {
@ -40,10 +41,10 @@ func (fc *fileCache) Init(cachePath string) {
} }
func (fc *fileCache) Close() { func (fc *fileCache) Close() {
cache := fc.cache.list() if !fc.dirty {
if len(cache) == 0 {
return return
} }
cache := fc.cache.list()
if dump, err := json.MarshalIndent(cache, "", " "); err == nil { if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
_ = ioutil.WriteFile(fc.cachePath+fileName, dump, 0644) _ = ioutil.WriteFile(fc.cachePath+fileName, dump, 0644)
} }
@ -78,4 +79,5 @@ func (fc *fileCache) Set(key, value string, ttl int) {
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
TTL: ttl, TTL: ttl,
}) })
fc.dirty = true
} }