Skip to content

Commit fd783b0

Browse files
Fix cache corruption in large sets
1 parent 0015e58 commit fd783b0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/cache/cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
log "github.com/sirupsen/logrus"
99
"os"
1010
"path/filepath"
11+
"sync"
1112
)
1213

1314
type data struct {
@@ -16,6 +17,8 @@ type data struct {
1617
Blocks []detect.TextBlock
1718
}
1819

20+
var mu sync.Mutex
21+
1922
// read creates a cache if one doesn't already exist, reads the data from the cache,
2023
// and returns it as a slice of cache Data.
2124
func read() []data {
@@ -51,6 +54,9 @@ func read() []data {
5154

5255
// Check returns the text blocks of the given image hash and its translation service if it is in cache, otherwise returns nil and the given service.
5356
func Check(h string, service string) (blocks []detect.TextBlock, translateOnly bool) {
57+
mu.Lock()
58+
defer mu.Unlock()
59+
5460
cacheData := read()
5561

5662
var existingBlocks []detect.TextBlock
@@ -75,6 +81,9 @@ func Check(h string, service string) (blocks []detect.TextBlock, translateOnly b
7581

7682
// Add adds a new entry to the cache.
7783
func Add(h string, service string, blocks []detect.TextBlock) {
84+
mu.Lock()
85+
defer mu.Unlock()
86+
7887
log.Debugf("Adding new image to cache. sha256:%v", h)
7988
cacheData := read()
8089

0 commit comments

Comments
 (0)