优化批量删除缓存Key代码,防止列表删除了文件还在

This commit is contained in:
刘祥超
2023-10-11 06:31:35 +08:00
parent 6ca8b6837c
commit 7f3e6ddc65

View File

@@ -261,11 +261,6 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e
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() 循环中操作是为了避免死锁
@@ -280,6 +275,11 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e
return 0, err
}
}
_, err = db.writeDB.Exec(`DELETE FROM "cacheItems" WHERE "hash" IN ('` + strings.Join(hashStrings, "', '") + `')`)
if err != nil {
return 0, err
}
}
return countFound, nil
@@ -301,11 +301,6 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro
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, true)
@@ -318,6 +313,11 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro
return err
}
}
_, err = db.writeDB.Exec(`DELETE FROM "cacheItems" WHERE "hash" IN ('` + strings.Join(hashStrings, "', '") + `')`)
if err != nil {
return err
}
}
return nil
}