diff --git a/pkg/fsutils/filecache.go b/pkg/fsutils/filecache.go index 6c97bdda..8e4d8c32 100644 --- a/pkg/fsutils/filecache.go +++ b/pkg/fsutils/filecache.go @@ -3,6 +3,7 @@ package fsutils import ( "fmt" "io/ioutil" + "sync" "github.com/golangci/golangci-lint/pkg/logutils" @@ -10,19 +11,17 @@ import ( ) type FileCache struct { - files map[string][]byte + files sync.Map } func NewFileCache() *FileCache { - return &FileCache{ - files: map[string][]byte{}, - } + return &FileCache{} } func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) { - cachedBytes := fc.files[filePath] - if cachedBytes != nil { - return cachedBytes, nil + cachedBytes, ok := fc.files.Load(filePath) + if ok { + return cachedBytes.([]byte), nil } fileBytes, err := ioutil.ReadFile(filePath) @@ -30,7 +29,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) { return nil, errors.Wrapf(err, "can't read file %s", filePath) } - fc.files[filePath] = fileBytes + fc.files.Store(filePath, fileBytes) return fileBytes, nil } @@ -56,9 +55,13 @@ func prettifyBytesCount(n int) string { func (fc *FileCache) PrintStats(log logutils.Log) { var size int - for _, fileBytes := range fc.files { - size += len(fileBytes) - } + var mapLen int + fc.files.Range(func(_, fileBytes interface{}) bool { + mapLen++ + size += len(fileBytes.([]byte)) - log.Infof("File cache stats: %d entries of total size %s", len(fc.files), prettifyBytesCount(size)) + return true + }) + + log.Infof("File cache stats: %d entries of total size %s", mapLen, prettifyBytesCount(size)) }