mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	当使用quit退出进程时,同时也禁用缓存策略
This commit is contained in:
		@@ -47,6 +47,7 @@ type FileList struct {
 | 
			
		||||
	hitsTableName  string
 | 
			
		||||
 | 
			
		||||
	isClosed bool
 | 
			
		||||
	isReady  bool
 | 
			
		||||
 | 
			
		||||
	memoryCache *ttlcache.Cache
 | 
			
		||||
}
 | 
			
		||||
@@ -77,11 +78,30 @@ func (this *FileList) Init() error {
 | 
			
		||||
		// 防止sqlite提示authority错误
 | 
			
		||||
		dir = ""
 | 
			
		||||
	}
 | 
			
		||||
	db, err := sql.Open("sqlite3", "file:"+dir+"/index.db?cache=shared&mode=rwc&_journal_mode=WAL")
 | 
			
		||||
	var dbPath = dir + "/index.db"
 | 
			
		||||
	remotelogs.Println("CACHE", "loading database '"+dbPath+"'")
 | 
			
		||||
	db, err := sql.Open("sqlite3", "file:"+dbPath+"?cache=shared&mode=rwc&_journal_mode=WAL")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查数据库
 | 
			
		||||
	_, err = db.Exec(`SELECT * FROM "` + this.itemsTableName + `" LIMIT 1`)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		// 删除重建
 | 
			
		||||
		remotelogs.Println("CACHE", "rebuilding database '"+dbPath+"'")
 | 
			
		||||
		_ = db.Close()
 | 
			
		||||
		this.isClosed = false
 | 
			
		||||
 | 
			
		||||
		_ = os.Remove(dbPath)
 | 
			
		||||
		db, err = sql.Open("sqlite3", "file:"+dbPath+"?cache=shared&mode=rwc&_journal_mode=WAL")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	db.SetMaxOpenConns(1)
 | 
			
		||||
 | 
			
		||||
	this.db = db
 | 
			
		||||
 | 
			
		||||
	// 清除旧表
 | 
			
		||||
@@ -176,6 +196,8 @@ func (this *FileList) Init() error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	this.isReady = true
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -185,7 +207,7 @@ func (this *FileList) Reset() error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) Add(hash string, item *Item) error {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -212,7 +234,7 @@ func (this *FileList) Add(hash string, item *Item) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) Exist(hash string) (bool, error) {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return false, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -242,7 +264,7 @@ func (this *FileList) Exist(hash string) (bool, error) {
 | 
			
		||||
 | 
			
		||||
// CleanPrefix 清理某个前缀的缓存数据
 | 
			
		||||
func (this *FileList) CleanPrefix(prefix string) error {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -272,7 +294,7 @@ func (this *FileList) CleanPrefix(prefix string) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) Remove(hash string) error {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -316,7 +338,7 @@ func (this *FileList) Remove(hash string) error {
 | 
			
		||||
// count 每次遍历的最大数量,控制此数字可以保证每次清理的时候不用花太多时间
 | 
			
		||||
// callback 每次发现过期key的调用
 | 
			
		||||
func (this *FileList) Purge(count int, callback func(hash string) error) (int, error) {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return 0, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -360,7 +382,7 @@ func (this *FileList) Purge(count int, callback func(hash string) error) (int, e
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) PurgeLFU(count int, callback func(hash string) error) error {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -403,7 +425,7 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) CleanAll() error {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -418,7 +440,7 @@ func (this *FileList) CleanAll() error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *FileList) Stat(check func(hash string) bool) (*Stat, error) {
 | 
			
		||||
	if this.isClosed {
 | 
			
		||||
	if !this.isReady {
 | 
			
		||||
		return &Stat{}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -461,6 +483,7 @@ func (this *FileList) OnRemove(f func(item *Item)) {
 | 
			
		||||
 | 
			
		||||
func (this *FileList) Close() error {
 | 
			
		||||
	this.isClosed = true
 | 
			
		||||
	this.isReady = false
 | 
			
		||||
 | 
			
		||||
	this.memoryCache.Destroy()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,8 +3,10 @@ package caches
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/events"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
 | 
			
		||||
	"github.com/iwind/TeaGo/lists"
 | 
			
		||||
	"github.com/iwind/TeaGo/logs"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -12,6 +14,13 @@ import (
 | 
			
		||||
 | 
			
		||||
var SharedManager = NewManager()
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	events.On(events.EventQuit, func() {
 | 
			
		||||
		logs.Println("CACHE", "quiting cache manager")
 | 
			
		||||
		SharedManager.UpdatePolicies([]*serverconfigs.HTTPCachePolicy{})
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Manager 缓存策略管理器
 | 
			
		||||
type Manager struct {
 | 
			
		||||
	// 全局配置
 | 
			
		||||
@@ -25,10 +34,12 @@ type Manager struct {
 | 
			
		||||
 | 
			
		||||
// NewManager 获取管理器对象
 | 
			
		||||
func NewManager() *Manager {
 | 
			
		||||
	return &Manager{
 | 
			
		||||
	var m = &Manager{
 | 
			
		||||
		policyMap:  map[int64]*serverconfigs.HTTPCachePolicy{},
 | 
			
		||||
		storageMap: map[int64]StorageInterface{},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return m
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdatePolicies 重新设置策略
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package nodes
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/assert"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -33,3 +34,17 @@ func TestHTTPRequest_RedirectToHTTPS(t *testing.T) {
 | 
			
		||||
		a.IsBool(req.web.RedirectToHttps.IsOn == true)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHTTPRequest_Memory(t *testing.T) {
 | 
			
		||||
	var stat1 = &runtime.MemStats{}
 | 
			
		||||
	runtime.ReadMemStats(stat1)
 | 
			
		||||
 | 
			
		||||
	var requests = []*HTTPRequest{}
 | 
			
		||||
	for i := 0; i < 1_000_000; i++ {
 | 
			
		||||
		requests = append(requests, &HTTPRequest{})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var stat2 = &runtime.MemStats{}
 | 
			
		||||
	runtime.ReadMemStats(stat2)
 | 
			
		||||
	t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB,")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user