Skip to content

Commit

Permalink
fix concurrent map write
Browse files Browse the repository at this point in the history
  • Loading branch information
lh1xc committed Jul 28, 2022
1 parent 43388e8 commit 45953fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions internal/db/cache.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package db

import "github.com/zu1k/nali/pkg/dbif"
import (
"sync"

"github.com/zu1k/nali/pkg/dbif"
)

var (
dbNameCache = make(map[string]dbif.DB)
dbTypeCache = make(map[dbif.QueryType]dbif.DB)
queryCache = make(map[string]string)
queryCache = sync.Map{}
)

var (
Expand Down
6 changes: 3 additions & 3 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) {
}

func Find(typ dbif.QueryType, query string) string {
if result, found := queryCache[query]; found {
return result
if result, found := queryCache.Load(query); found {
return result.(string)
}
result, err := GetDB(typ).Find(query)
if err != nil {
return ""
}
r := strings.Trim(result.String(), " ")
queryCache[query] = r
queryCache.Store(query, r)
return r
}

0 comments on commit 45953fc

Please sign in to comment.