Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix concurrent map write #125

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}