优化缓存相关代码

This commit is contained in:
GoEdgeLab
2023-10-03 11:39:28 +08:00
parent 414f0b873d
commit e5b9e27347
2 changed files with 42 additions and 31 deletions

View File

@@ -112,6 +112,7 @@ func (this *MemoryWriter) Close() error {
// 需要在Locker之外
defer this.once.Do(func() {
this.endFunc(this.item)
this.item = nil // free memory
})
if this.item == nil {
@@ -123,11 +124,14 @@ func (this *MemoryWriter) Close() error {
var err error
if this.isDirty {
if this.storage.parentStorage != nil {
this.storage.valuesMap[this.hash] = this.item
select {
case this.storage.dirtyChan <- this.key:
this.storage.valuesMap[this.hash] = this.item
default:
// do not add value map
// remove from values map
delete(this.storage.valuesMap, this.hash)
err = ErrWritingQueueFull
}
} else {
@@ -147,6 +151,7 @@ func (this *MemoryWriter) Discard() error {
// 需要在Locker之外
defer this.once.Do(func() {
this.endFunc(this.item)
this.item = nil // free memory
})
this.storage.locker.Lock()