2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-05 00:00:14 +00:00

Use mutex locks for filecache writes; fixes #32"

This commit is contained in:
Miguel Mota 2019-06-01 09:48:15 -07:00
parent 50f8d6cb17
commit ca3921efb0

View File

@ -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)