Skip to content

Commit 24fc224

Browse files
committed
优化文件缓存
1 parent 84c931b commit 24fc224

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

internal/caches/list_file.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package caches
55
import (
66
"database/sql"
77
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
8+
"github.com/TeaOSLab/EdgeNode/internal/utils"
89
"github.com/iwind/TeaGo/lists"
910
_ "github.com/mattn/go-sqlite3"
1011
"os"
@@ -76,8 +77,6 @@ func (this *FileList) Init() error {
7677
}**/
7778

7879
// 创建
79-
// TODO accessesAt 用来存储访问时间,将来可以根据此访问时间删除不常访问的内容
80-
// 且访问时间只需要每隔一个小时存储一个整数值即可,因为不需要那么精确
8180
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemsTableName + `" (
8281
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
8382
"hash" varchar(32),
@@ -86,14 +85,14 @@ func (this *FileList) Init() error {
8685
"bodySize" integer DEFAULT 0,
8786
"metaSize" integer DEFAULT 0,
8887
"expiredAt" integer DEFAULT 0,
89-
"accessedAt" integer DEFAULT 0,
88+
"createdAt" integer DEFAULT 0,
9089
"host" varchar(128),
9190
"serverId" integer
9291
);
9392
94-
CREATE INDEX IF NOT EXISTS "accessedAt"
93+
CREATE INDEX IF NOT EXISTS "createdAt"
9594
ON "` + this.itemsTableName + `" (
96-
"accessedAt" ASC
95+
"createdAt" ASC
9796
);
9897
9998
CREATE INDEX IF NOT EXISTS "expiredAt"
@@ -133,7 +132,7 @@ ON "` + this.itemsTableName + `" (
133132
return err
134133
}
135134

136-
this.insertStmt, err = this.db.Prepare(`INSERT INTO "` + this.itemsTableName + `" ("hash", "key", "headerSize", "bodySize", "metaSize", "expiredAt", "host", "serverId") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
135+
this.insertStmt, err = this.db.Prepare(`INSERT INTO "` + this.itemsTableName + `" ("hash", "key", "headerSize", "bodySize", "metaSize", "expiredAt", "host", "serverId", "createdAt") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
137136
if err != nil {
138137
return err
139138
}
@@ -176,7 +175,7 @@ func (this *FileList) Add(hash string, item *Item) error {
176175
return nil
177176
}
178177

179-
_, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.Host, item.ServerId)
178+
_, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.Host, item.ServerId, utils.UnixTime())
180179
if err != nil {
181180
return err
182181
}
@@ -219,7 +218,7 @@ func (this *FileList) CleanPrefix(prefix string) error {
219218

220219
var count = int64(10000)
221220
for {
222-
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)
221+
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)
223222
if err != nil {
224223
return err
225224
}

internal/caches/storage_memory.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
77
"github.com/TeaOSLab/EdgeNode/internal/utils"
88
"github.com/cespare/xxhash"
9-
"runtime"
109
"strconv"
1110
"sync"
1211
"sync/atomic"
@@ -217,8 +216,6 @@ func (this *MemoryStorage) Stop() {
217216

218217
this.locker.Unlock()
219218

220-
runtime.GC()
221-
222219
remotelogs.Println("CACHE", "close memory storage '"+strconv.FormatInt(this.policy.Id, 10)+"'")
223220
}
224221

internal/nodes/http_request_cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
121121
}
122122
defer func() {
123123
_ = reader.Close()
124-
this.cacheRef = nil // 终止读取不再往下传递
125124
}()
126125

127126
this.varMapping["cache.status"] = "HIT"
@@ -186,6 +185,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
186185
// 自定义Header
187186
this.processResponseHeaders(http.StatusNotModified)
188187
this.writer.WriteHeader(http.StatusNotModified)
188+
this.cacheRef = nil
189189
return true
190190
}
191191

@@ -194,6 +194,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
194194
// 自定义Header
195195
this.processResponseHeaders(http.StatusNotModified)
196196
this.writer.WriteHeader(http.StatusNotModified)
197+
this.cacheRef = nil
197198
return true
198199
}
199200

@@ -355,5 +356,6 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) {
355356
}
356357

357358
this.isCached = true
359+
this.cacheRef = nil
358360
return true
359361
}

0 commit comments

Comments
 (0)