通过内存缓存提升文件缓存效率大约20%

This commit is contained in:
GoEdgeLab
2021-08-21 21:06:48 +08:00
parent 53f3a56972
commit 7a452c5a6f
6 changed files with 56 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ package caches
import (
"database/sql"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/lists"
_ "github.com/mattn/go-sqlite3"
@@ -35,10 +36,15 @@ type FileList struct {
itemsTableName string
isClosed bool
memoryCache *ttlcache.Cache
}
func NewFileList(dir string) ListInterface {
return &FileList{dir: dir}
return &FileList{
dir: dir,
memoryCache: ttlcache.NewCache(),
}
}
func (this *FileList) Init() error {
@@ -100,7 +106,7 @@ func (this *FileList) Init() error {
this.total = total
// 常用语句
this.existsByHashStmt, err = this.db.Prepare(`SELECT "bodySize" FROM "` + this.itemsTableName + `" WHERE "hash"=? AND expiredAt>? LIMIT 1`)
this.existsByHashStmt, err = this.db.Prepare(`SELECT "expiredAt" FROM "` + this.itemsTableName + `" WHERE "hash"=? AND expiredAt>? LIMIT 1`)
if err != nil {
return err
}
@@ -166,6 +172,11 @@ func (this *FileList) Exist(hash string) (bool, error) {
return false, nil
}
item := this.memoryCache.Read(hash)
if item != nil {
return true, nil
}
rows, err := this.existsByHashStmt.Query(hash, time.Now().Unix())
if err != nil {
return false, err
@@ -174,6 +185,12 @@ func (this *FileList) Exist(hash string) (bool, error) {
_ = rows.Close()
}()
if rows.Next() {
var expiredAt int64
err = rows.Scan(&expiredAt)
if err != nil {
return true, nil
}
this.memoryCache.Write(hash, 1, expiredAt)
return true, nil
}
return false, nil
@@ -210,6 +227,9 @@ func (this *FileList) Remove(hash string) error {
return nil
}
// 从缓存中删除
this.memoryCache.Delete(hash)
row := this.selectByHashStmt.QueryRow(hash)
if row.Err() != nil {
return row.Err()
@@ -288,6 +308,8 @@ func (this *FileList) CleanAll() error {
return nil
}
this.memoryCache.Clean()
_, err := this.deleteAllStmt.Exec()
if err != nil {
return err