From 79ba5a4c47797a7c7a904bc49f1ca2b82437de0e Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 17 Jul 2023 09:29:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B8=85=E7=90=86=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=BC=93=E5=AD=98=E5=86=85=E5=AE=B9=E5=90=8E=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=86=99=E5=85=A5=E6=96=B0=E7=BC=93=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=88=E4=B8=80=E7=9B=B4=E6=8F=90=E7=A4=BA?= =?UTF-8?q?the=20file=20is=20writing=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/storage_memory.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index 6bbe960..47dbc7a 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -111,8 +111,15 @@ func (this *MemoryStorage) Init() error { // OpenReader 读取缓存 func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool) (Reader, error) { - hash := this.hash(key) + var hash = this.hash(key) + // check if exists in list + exists, _ := this.list.Exist(types.String(hash)) + if !exists { + return nil, ErrNotFound + } + + // read from valuesMap this.locker.RLock() item := this.valuesMap[hash] if item == nil || !item.IsDone { @@ -121,7 +128,7 @@ func (this *MemoryStorage) OpenReader(key string, useStale bool, isPartial bool) } if useStale || (item.ExpiresAt > fasttime.Now().Unix()) { - reader := NewMemoryReader(item) + var reader = NewMemoryReader(item) err := reader.Init() if err != nil { this.locker.RUnlock() @@ -193,10 +200,19 @@ func (this *MemoryStorage) openWriter(key string, expiresAt int64, status int, h }() // 检查是否过期 - hash := this.hash(key) + var hash = this.hash(key) item, ok := this.valuesMap[hash] if ok && !item.IsExpired() { - return nil, ErrFileIsWriting + var hashString = types.String(hash) + exists, _ := this.list.Exist(hashString) + if !exists { + // remove from values map + delete(this.valuesMap, hash) + _ = this.list.Remove(hashString) + item = nil + } else { + return nil, ErrFileIsWriting + } } // 检查是否超出最大值