-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
50 lines (43 loc) · 1.01 KB
/
Copy pathcache.go
File metadata and controls
50 lines (43 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"crypto/sha256"
"sync"
)
// Cache stores scan results keyed by content hash
type Cache struct {
mu sync.RWMutex
entries map[[32]byte][]Finding
}
// NewCache creates a new result cache
func NewCache() *Cache {
return &Cache{
entries: make(map[[32]byte][]Finding),
}
}
// Get retrieves cached findings for content
func (c *Cache) Get(content string) ([]Finding, bool) {
hash := sha256.Sum256([]byte(content))
c.mu.RLock()
defer c.mu.RUnlock()
findings, ok := c.entries[hash]
return findings, ok
}
// Put stores findings for content
func (c *Cache) Put(content string, findings []Finding) {
hash := sha256.Sum256([]byte(content))
c.mu.Lock()
defer c.mu.Unlock()
c.entries[hash] = findings
}
// Clear empties the cache (e.g., on config reload)
func (c *Cache) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
c.entries = make(map[[32]byte][]Finding)
}
// Size returns the number of entries in the cache
func (c *Cache) Size() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.entries)
}