Merge branch 'Pomyk-fix_filecache_locks'

pull/137/head
Miguel Mota 3 years ago
commit b8b7a87f1b
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -20,6 +20,7 @@ var DefaultCacheDir = "/tmp"
// FileCache ...
type FileCache struct {
mapLock sync.Mutex
muts map[string]*sync.Mutex
prefix string
cacheDir string
@ -56,12 +57,17 @@ func NewFileCache(config *Config) (*FileCache, error) {
// Set writes item to cache
func (f *FileCache) Set(key string, data interface{}, expire time.Duration) error {
if _, ok := f.muts[key]; !ok {
var mu *sync.Mutex
var ok bool
f.mapLock.Lock()
if mu, ok = f.muts[key]; !ok {
f.muts[key] = new(sync.Mutex)
mu = f.muts[key]
}
f.mapLock.Unlock()
f.muts[key].Lock()
defer f.muts[key].Unlock()
mu.Lock()
defer mu.Unlock()
key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
if f.prefix != "" {
@ -78,9 +84,6 @@ func (f *FileCache) Set(key string, data interface{}, expire time.Duration) erro
return err
}
var fmutex sync.RWMutex
fmutex.Lock()
defer fmutex.Unlock()
fp, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err

Loading…
Cancel
Save