修复并发下,写缓存文件可能冲突的问题

This commit is contained in:
刘祥超
2021-12-21 08:03:09 +08:00
parent fd0bc37ec7
commit 5cb5ddf2c1
2 changed files with 21 additions and 15 deletions

View File

@@ -47,8 +47,8 @@ const (
HotItemSize = 1024
)
var sharedWritingKeyMap = map[string]zero.Zero{} // key => bool
var sharedWritingKeyLocker = sync.Mutex{}
var sharedWritingFileKeyMap = map[string]zero.Zero{} // key => bool
var sharedWritingFileKeyLocker = sync.Mutex{}
// FileStorage 文件缓存
// 文件结构:
@@ -316,19 +316,19 @@ func (this *FileStorage) OpenWriter(key string, expiredAt int64, status int) (Wr
// 是否正在写入
var isOk = false
sharedWritingKeyLocker.Lock()
_, ok := sharedWritingKeyMap[key]
sharedWritingFileKeyLocker.Lock()
_, ok := sharedWritingFileKeyMap[key]
if ok {
sharedWritingKeyLocker.Unlock()
sharedWritingFileKeyLocker.Unlock()
return nil, ErrFileIsWriting
}
sharedWritingKeyMap[key] = zero.New()
sharedWritingKeyLocker.Unlock()
sharedWritingFileKeyMap[key] = zero.New()
sharedWritingFileKeyLocker.Unlock()
defer func() {
if !isOk {
sharedWritingKeyLocker.Lock()
delete(sharedWritingKeyMap, key)
sharedWritingKeyLocker.Unlock()
sharedWritingFileKeyLocker.Lock()
delete(sharedWritingFileKeyMap, key)
sharedWritingFileKeyLocker.Unlock()
}
}()
@@ -460,9 +460,9 @@ func (this *FileStorage) OpenWriter(key string, expiredAt int64, status int) (Wr
isOk = true
return NewFileWriter(writer, key, expiredAt, func() {
sharedWritingKeyLocker.Lock()
delete(sharedWritingKeyMap, key)
sharedWritingKeyLocker.Unlock()
sharedWritingFileKeyLocker.Lock()
delete(sharedWritingFileKeyMap, key)
sharedWritingFileKeyLocker.Unlock()
}), nil
}

View File

@@ -2,6 +2,7 @@ package caches
import (
"github.com/cespare/xxhash"
"sync"
"time"
)
@@ -18,6 +19,7 @@ type MemoryWriter struct {
hash uint64
item *MemoryItem
endFunc func()
once sync.Once
}
func NewMemoryWriter(memoryStorage *MemoryStorage, key string, expiredAt int64, status int, isDirty bool, endFunc func()) *MemoryWriter {
@@ -66,7 +68,9 @@ func (this *MemoryWriter) BodySize() int64 {
// Close 关闭
func (this *MemoryWriter) Close() error {
// 需要在Locker之外
defer this.endFunc()
defer this.once.Do(func() {
this.endFunc()
})
if this.item == nil {
return nil
@@ -92,7 +96,9 @@ func (this *MemoryWriter) Close() error {
// Discard 丢弃
func (this *MemoryWriter) Discard() error {
// 需要在Locker之外
defer this.endFunc()
defer this.once.Do(func() {
this.endFunc()
})
this.storage.locker.Lock()
delete(this.storage.valuesMap, this.hash)