Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 7b80745

Browse files
c4s4Michel Casabianca
andauthored
Fixed synchronization bug on file cache (#9)
Co-authored-by: Michel Casabianca <michel.casabianca@intercloud.com>
1 parent aead8d5 commit 7b80745

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

gobinsec/cache-file.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55
"path/filepath"
66
"strings"
7+
"sync"
78
"time"
89

910
"gopkg.in/yaml.v3"
@@ -49,6 +50,9 @@ type FileCache struct {
4950
Cache map[string]DependencyCache
5051
}
5152

53+
// lock for memory cache
54+
var lock sync.RWMutex
55+
5256
// NewFileCache builds a cache using file
5357
func NewFileCache(config *FileConfig) Cache {
5458
cache := make(map[string]DependencyCache)
@@ -69,7 +73,10 @@ func NewFileCache(config *FileConfig) Cache {
6973

7074
// Get returns NVD response for given dependency
7175
func (fc *FileCache) Get(d *Dependency) []byte {
72-
dependency, ok := fc.Cache[d.Name]
76+
key := d.Key()
77+
lock.RLock()
78+
defer lock.RUnlock()
79+
dependency, ok := fc.Cache[key]
7380
if ok {
7481
return []byte(dependency.Vulnerabilities)
7582
}
@@ -78,12 +85,15 @@ func (fc *FileCache) Get(d *Dependency) []byte {
7885

7986
// Set put NVD response for given dependency in cache
8087
func (fc *FileCache) Set(d *Dependency, v []byte) {
88+
key := d.Key()
89+
lock.Lock()
90+
defer lock.Unlock()
8191
now := time.Now().UTC().Format("2006-01-02T15:04:05")
8292
cache := DependencyCache{
8393
Date: now,
8494
Vulnerabilities: string(v),
8595
}
86-
fc.Cache[d.Name] = cache
96+
fc.Cache[key] = cache
8797
}
8898

8999
// Ping does nothing

0 commit comments

Comments
 (0)