This repository was archived by the owner on Nov 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
5357func 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
7175func (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
8087func (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
You can’t perform that action at this time.
0 commit comments