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

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

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