删除过期缓存时使用批量删除

This commit is contained in:
刘祥超
2023-10-10 22:08:42 +08:00
parent a9969430a3
commit 6ca8b6837c

View File

@@ -14,6 +14,7 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/zero" "github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
) )
@@ -236,7 +237,7 @@ func (this *FileList) CleanMatchPrefix(prefix string) error {
} }
func (this *FileList) Remove(hash string) error { func (this *FileList) Remove(hash string) error {
_, err := this.remove(hash) _, err := this.remove(hash, false)
return err return err
} }
@@ -255,11 +256,21 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e
if err != nil { if err != nil {
return 0, 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) countFound += len(hashStrings)
// 不在 rows.Next() 循环中操作是为了避免死锁 // 不在 rows.Next() 循环中操作是为了避免死锁
for _, hash := range hashStrings { for _, hash := range hashStrings {
err = this.Remove(hash) _, err = this.remove(hash, true)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -286,9 +297,18 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro
return err 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() 循环中操作是为了避免死锁 // 不在 rows.Next() 循环中操作是为了避免死锁
for _, hash := range hashStrings { for _, hash := range hashStrings {
_, err = this.remove(hash) _, err = this.remove(hash, true)
if err != nil { if err != nil {
return err return err
} }
@@ -407,7 +427,7 @@ func (this *FileList) HashMapIsLoaded() bool {
return true 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) var db = this.GetDB(hash)
if !db.IsReady() { if !db.IsReady() {
@@ -423,9 +443,11 @@ func (this *FileList) remove(hash string) (notFound bool, err error) {
// 从缓存中删除 // 从缓存中删除
this.memoryCache.Delete(hash) this.memoryCache.Delete(hash)
err = db.DeleteSync(hash) if !isDeleted {
if err != nil { err = db.DeleteSync(hash)
return false, db.WrapError(err) if err != nil {
return false, db.WrapError(err)
}
} }
if this.onRemove != nil { if this.onRemove != nil {