Skip to content

Commit

Permalink
Use mutex locks for filecache writes; fixes #32"
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jun 1, 2019
1 parent 50f8d6c commit ca3921e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cointop/common/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ import (

const cachedir = "/tmp"

var muts = make(map[string]*sync.Mutex)

// Set writes item to cache
func Set(key string, data interface{}, expire time.Duration) error {
if _, ok := muts[key]; !ok {
muts[key] = new(sync.Mutex)
}

muts[key].Lock()
defer muts[key].Unlock()

key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
file := fmt.Sprintf("fcache.%s.%v", key, strconv.FormatInt(time.Now().Add(expire).Unix(), 10))
fpath := filepath.Join(cachedir, file)
Expand Down

0 comments on commit ca3921e

Please sign in to comment.