Skip to content

Commit 3709ae8

Browse files
authored
Merge pull request #125 from lhcn/concurrent_fix
fix concurrent map write
2 parents 43388e8 + 45953fc commit 3709ae8

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

internal/db/cache.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package db
22

3-
import "github.com/zu1k/nali/pkg/dbif"
3+
import (
4+
"sync"
5+
6+
"github.com/zu1k/nali/pkg/dbif"
7+
)
48

59
var (
610
dbNameCache = make(map[string]dbif.DB)
711
dbTypeCache = make(map[dbif.QueryType]dbif.DB)
8-
queryCache = make(map[string]string)
12+
queryCache = sync.Map{}
913
)
1014

1115
var (

internal/db/db.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) {
7070
}
7171

7272
func Find(typ dbif.QueryType, query string) string {
73-
if result, found := queryCache[query]; found {
74-
return result
73+
if result, found := queryCache.Load(query); found {
74+
return result.(string)
7575
}
7676
result, err := GetDB(typ).Find(query)
7777
if err != nil {
7878
return ""
7979
}
8080
r := strings.Trim(result.String(), " ")
81-
queryCache[query] = r
81+
queryCache.Store(query, r)
8282
return r
8383
}

0 commit comments

Comments
 (0)