优化文件缓存

This commit is contained in:
刘祥超
2021-06-14 19:55:06 +08:00
parent 84c931b411
commit 24fc2249bb
3 changed files with 10 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ package caches
import ( import (
"database/sql" "database/sql"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os" "os"
@@ -76,8 +77,6 @@ func (this *FileList) Init() error {
}**/ }**/
// 创建 // 创建
// TODO accessesAt 用来存储访问时间,将来可以根据此访问时间删除不常访问的内容
// 且访问时间只需要每隔一个小时存储一个整数值即可,因为不需要那么精确
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemsTableName + `" ( _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemsTableName + `" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"hash" varchar(32), "hash" varchar(32),
@@ -86,14 +85,14 @@ func (this *FileList) Init() error {
"bodySize" integer DEFAULT 0, "bodySize" integer DEFAULT 0,
"metaSize" integer DEFAULT 0, "metaSize" integer DEFAULT 0,
"expiredAt" integer DEFAULT 0, "expiredAt" integer DEFAULT 0,
"accessedAt" integer DEFAULT 0, "createdAt" integer DEFAULT 0,
"host" varchar(128), "host" varchar(128),
"serverId" integer "serverId" integer
); );
CREATE INDEX IF NOT EXISTS "accessedAt" CREATE INDEX IF NOT EXISTS "createdAt"
ON "` + this.itemsTableName + `" ( ON "` + this.itemsTableName + `" (
"accessedAt" ASC "createdAt" ASC
); );
CREATE INDEX IF NOT EXISTS "expiredAt" CREATE INDEX IF NOT EXISTS "expiredAt"
@@ -133,7 +132,7 @@ ON "` + this.itemsTableName + `" (
return err return err
} }
this.insertStmt, err = this.db.Prepare(`INSERT INTO "` + this.itemsTableName + `" ("hash", "key", "headerSize", "bodySize", "metaSize", "expiredAt", "host", "serverId") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`) this.insertStmt, err = this.db.Prepare(`INSERT INTO "` + this.itemsTableName + `" ("hash", "key", "headerSize", "bodySize", "metaSize", "expiredAt", "host", "serverId", "createdAt") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil { if err != nil {
return err return err
} }
@@ -176,7 +175,7 @@ func (this *FileList) Add(hash string, item *Item) error {
return nil return nil
} }
_, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.Host, item.ServerId) _, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.Host, item.ServerId, utils.UnixTime())
if err != nil { if err != nil {
return err return err
} }
@@ -219,7 +218,7 @@ func (this *FileList) CleanPrefix(prefix string) error {
var count = int64(10000) var count = int64(10000)
for { for {
result, err := this.db.Exec(`UPDATE "`+this.itemsTableName+`" SET expiredAt=0 WHERE id IN (SELECT id FROM "`+this.itemsTableName+`" WHERE expiredAt>0 AND INSTR("key", ?)==1 LIMIT `+strconv.FormatInt(count, 10)+`)`, prefix) result, err := this.db.Exec(`UPDATE "`+this.itemsTableName+`" SET expiredAt=0 WHERE id IN (SELECT id FROM "`+this.itemsTableName+`" WHERE expiredAt>0 AND createdAt<=? AND INSTR("key", ?)==1 LIMIT `+strconv.FormatInt(count, 10)+`)`, utils.UnixTime(), prefix)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -6,7 +6,6 @@ import (
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/cespare/xxhash" "github.com/cespare/xxhash"
"runtime"
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
@@ -217,8 +216,6 @@ func (this *MemoryStorage) Stop() {
this.locker.Unlock() this.locker.Unlock()
runtime.GC()
remotelogs.Println("CACHE", "close memory storage '"+strconv.FormatInt(this.policy.Id, 10)+"'") remotelogs.Println("CACHE", "close memory storage '"+strconv.FormatInt(this.policy.Id, 10)+"'")
} }

View File

@@ -121,7 +121,6 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
} }
defer func() { defer func() {
_ = reader.Close() _ = reader.Close()
this.cacheRef = nil // 终止读取不再往下传递
}() }()
this.varMapping["cache.status"] = "HIT" this.varMapping["cache.status"] = "HIT"
@@ -186,6 +185,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
// 自定义Header // 自定义Header
this.processResponseHeaders(http.StatusNotModified) this.processResponseHeaders(http.StatusNotModified)
this.writer.WriteHeader(http.StatusNotModified) this.writer.WriteHeader(http.StatusNotModified)
this.cacheRef = nil
return true return true
} }
@@ -194,6 +194,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
// 自定义Header // 自定义Header
this.processResponseHeaders(http.StatusNotModified) this.processResponseHeaders(http.StatusNotModified)
this.writer.WriteHeader(http.StatusNotModified) this.writer.WriteHeader(http.StatusNotModified)
this.cacheRef = nil
return true return true
} }
@@ -355,5 +356,6 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
} }
this.isCached = true this.isCached = true
this.cacheRef = nil
return true return true
} }