优化Partial Content缓存

This commit is contained in:
刘祥超
2022-03-05 16:47:17 +08:00
parent f34ad57e12
commit 10443dfb15
2 changed files with 25 additions and 16 deletions

View File

@@ -274,7 +274,7 @@ func (this *FileStorage) openReader(key string, allowMemory bool, useStale bool,
defer func() { defer func() {
if !isOk { if !isOk {
_ = fp.Close() _ = fp.Close()
_ = os.Remove(path) _ = this.removeCacheFile(path)
} }
}() }()
@@ -535,13 +535,8 @@ func (this *FileStorage) Delete(key string) error {
if err != nil { if err != nil {
return err return err
} }
err = os.Remove(path) err = this.removeCacheFile(path)
if err == nil || os.IsNotExist(err) { if err == nil || os.IsNotExist(err) {
// 删除Partial相关
if strings.HasSuffix(key, SuffixPartial) {
_ = os.Remove(partialRangesFilePath(path))
}
return nil return nil
} }
@@ -652,16 +647,11 @@ func (this *FileStorage) Purge(keys []string, urlType string) error {
// 文件 // 文件
for _, key := range keys { for _, key := range keys {
hash, path := this.keyPath(key) hash, path := this.keyPath(key)
err := os.Remove(path) err := this.removeCacheFile(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return err return err
} }
// 删除Partial相关
if strings.HasSuffix(key, SuffixPartial) {
_ = os.Remove(partialRangesFilePath(path))
}
err = this.list.Remove(hash) err = this.list.Remove(hash)
if err != nil { if err != nil {
return err return err
@@ -836,10 +826,11 @@ func (this *FileStorage) purgeLoop() {
for i := 0; i < times; i++ { for i := 0; i < times; i++ {
countFound, err := this.list.Purge(purgeCount, func(hash string) error { countFound, err := this.list.Purge(purgeCount, func(hash string) error {
path := this.hashPath(hash) path := this.hashPath(hash)
err := os.Remove(path) err := this.removeCacheFile(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error()) remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
} }
return nil return nil
}) })
if err != 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)) 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 { err := this.list.PurgeLFU(count, func(hash string) error {
path := this.hashPath(hash) path := this.hashPath(hash)
err := os.Remove(path) err := this.removeCacheFile(path)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error()) remotelogs.Error("CACHE", "purge '"+path+"' error: "+err.Error())
} }
return nil return nil
}) })
if err != 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
}

View File

@@ -511,7 +511,7 @@ func TestFileStorage_Stop(t *testing.T) {
} }
func TestFileStorage_DecodeFile(t *testing.T) { func TestFileStorage_DecodeFile(t *testing.T) {
storage := NewFileStorage(&serverconfigs.HTTPCachePolicy{ var storage = NewFileStorage(&serverconfigs.HTTPCachePolicy{
Id: 1, Id: 1,
IsOn: true, IsOn: true,
Options: map[string]interface{}{ Options: map[string]interface{}{
@@ -526,6 +526,11 @@ func TestFileStorage_DecodeFile(t *testing.T) {
t.Log(path) 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) { func BenchmarkFileStorage_Read(b *testing.B) {
runtime.GOMAXPROCS(1) runtime.GOMAXPROCS(1)