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

This commit is contained in:
GoEdgeLab
2021-12-21 08:03:09 +08:00
parent 627444ef2b
commit fb06b22d60
2 changed files with 21 additions and 15 deletions

View File

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

View File

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