From 6ca8b6837cd7dfd0d29767e62525c218d10b3c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Tue, 10 Oct 2023 22:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E4=BD=BF=E7=94=A8=E6=89=B9=E9=87=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/caches/list_file.go b/internal/caches/list_file.go index 6fab1d2..ce9c666 100644 --- a/internal/caches/list_file.go +++ b/internal/caches/list_file.go @@ -14,6 +14,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/zero" "github.com/iwind/TeaGo/types" "os" + "strings" "sync" "time" ) @@ -236,7 +237,7 @@ func (this *FileList) CleanMatchPrefix(prefix string) error { } func (this *FileList) Remove(hash string) error { - _, err := this.remove(hash) + _, err := this.remove(hash, false) return err } @@ -255,11 +256,21 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e if err != nil { return 0, nil } + + if len(hashStrings) == 0 { + continue + } + + _, err = db.writeDB.Exec(`DELETE FROM "cacheItems" WHERE "hash" IN ('` + strings.Join(hashStrings, "', '") + `')`) + if err != nil { + return 0, err + } + countFound += len(hashStrings) // 不在 rows.Next() 循环中操作是为了避免死锁 for _, hash := range hashStrings { - err = this.Remove(hash) + _, err = this.remove(hash, true) if err != nil { return 0, err } @@ -286,9 +297,18 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro return err } + if len(hashStrings) == 0 { + continue + } + + _, err = db.writeDB.Exec(`DELETE FROM "cacheItems" WHERE "hash" IN ('` + strings.Join(hashStrings, "', '") + `')`) + if err != nil { + return err + } + // 不在 rows.Next() 循环中操作是为了避免死锁 for _, hash := range hashStrings { - _, err = this.remove(hash) + _, err = this.remove(hash, true) if err != nil { return err } @@ -407,7 +427,7 @@ func (this *FileList) HashMapIsLoaded() bool { return true } -func (this *FileList) remove(hash string) (notFound bool, err error) { +func (this *FileList) remove(hash string, isDeleted bool) (notFound bool, err error) { var db = this.GetDB(hash) if !db.IsReady() { @@ -423,9 +443,11 @@ func (this *FileList) remove(hash string) (notFound bool, err error) { // 从缓存中删除 this.memoryCache.Delete(hash) - err = db.DeleteSync(hash) - if err != nil { - return false, db.WrapError(err) + if !isDeleted { + err = db.DeleteSync(hash) + if err != nil { + return false, db.WrapError(err) + } } if this.onRemove != nil {