diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index c434c83..622aebe 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -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 } diff --git a/internal/caches/writer_memory.go b/internal/caches/writer_memory.go index 3a3b173..5f81cf1 100644 --- a/internal/caches/writer_memory.go +++ b/internal/caches/writer_memory.go @@ -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)