diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index d3d793d..5b8087e 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -274,7 +274,7 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool, defer func() { if !isOk { _ = fp.Close() - _ = os.Remove(path) + _ = this.removeCacheFile(path) } }() @@ -535,13 +535,8 @@ func (this *FileStorage) Delete(key string) error { if err != nil { return err } - err = os.Remove(path) + err = this.removeCacheFile(path) if err == nil || os.IsNotExist(err) { - // 删除Partial相关 - if strings.HasSuffix(key, SuffixPartial) { - _ = os.Remove(partialRangesFilePath(path)) - } - return nil } @@ -652,16 +647,11 @@ func (this *FileStorage) Purge(keys []string, urlType string) error { // 文件 for _, key := range keys { hash, path := this.keyPath(key) - err := os.Remove(path) + err := this.removeCacheFile(path) if err != nil && !os.IsNotExist(err) { return err } - // 删除Partial相关 - if strings.HasSuffix(key, SuffixPartial) { - _ = os.Remove(partialRangesFilePath(path)) - } - err = this.list.Remove(hash) if err != nil { return err @@ -836,10 +826,11 @@ func (this *FileStorage) purgeLoop() { for i := 0; i < times; i++ { countFound, err := this.list.Purge(purgeCount, func(hash string) error { path := this.hashPath(hash) - err := os.Remove(path) + err := this.removeCacheFile(path) if err != nil && !os.IsNotExist(err) { remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error()) } + return nil }) if err != nil { @@ -869,10 +860,11 @@ func (this *FileStorage) purgeLoop() { remotelogs.Println("CACHE", "LFU purge policy '"+this.policy.Name+"' id: "+types.String(this.policy.Id)+", count: "+types.String(count)) err := this.list.PurgeLFU(count, func(hash string) error { path := this.hashPath(hash) - err := os.Remove(path) + err := this.removeCacheFile(path) if err != nil && !os.IsNotExist(err) { remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error()) } + return nil }) if err != nil { @@ -1062,3 +1054,15 @@ func (this *FileStorage) increaseHit(key string, hash string, reader Reader) { } } } + +// 删除缓存文件 +func (this *FileStorage) removeCacheFile(path string) error { + var err = os.Remove(path) + if err == nil || os.IsNotExist(err) { + err = nil + + // 删除Partial相关 + _ = os.Remove(partialRangesFilePath(path)) + } + return err +} diff --git a/internal/caches/storage_file_test.go b/internal/caches/storage_file_test.go index d8e2749..7cd24d3 100644 --- a/internal/caches/storage_file_test.go +++ b/internal/caches/storage_file_test.go @@ -511,7 +511,7 @@ func TestFileStorage_Stop(t *testing.T) { } func TestFileStorage_DecodeFile(t *testing.T) { - storage := NewFileStorage(&serverconfigs.HTTPCachePolicy{ + var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{ Id: 1, IsOn: true, Options: map[string]interface{}{ @@ -526,6 +526,11 @@ func TestFileStorage_DecodeFile(t *testing.T) { t.Log(path) } +func TestFileStorage_RemoveCacheFile(t *testing.T) { + var storage = NewFileStorage(nil) + t.Log(storage.removeCacheFile("/Users/WorkSpace/EdgeProject/EdgeCache/p43/15/7e/157eba0dfc6dfb6fbbf20b1f9e584674.cache")) +} + func BenchmarkFileStorage_Read(b *testing.B) { runtime.GOMAXPROCS(1)