From 6f52df63a5adeea085259a91b1d0c1e91018f38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 20 Aug 2022 12:09:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E6=9C=89=E7=B3=BB=E7=BB=9F=E5=86=85?= =?UTF-8?q?=E5=AD=98=E8=B6=85=E8=BF=873GB=E7=9A=84=E6=89=8D=E7=BC=93?= =?UTF-8?q?=E5=AD=98Hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file_hash_map.go | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/internal/caches/list_file_hash_map.go b/internal/caches/list_file_hash_map.go index 3016813..e46174f 100644 --- a/internal/caches/list_file_hash_map.go +++ b/internal/caches/list_file_hash_map.go @@ -3,25 +3,36 @@ package caches import ( + "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/zero" "sync" ) -// FileListHashMap +// FileListHashMap 文件Hash列表 type FileListHashMap struct { - m map[string]zero.Zero - locker sync.RWMutex - isReady bool + m map[string]zero.Zero + + locker sync.RWMutex + isAvailable bool + isReady bool } func NewFileListHashMap() *FileListHashMap { return &FileListHashMap{ - m: map[string]zero.Zero{}, - isReady: false, + m: map[string]zero.Zero{}, + isAvailable: false, + isReady: false, } } func (this *FileListHashMap) Load(db *FileListDB) error { + // 如果系统内存过小,我们不缓存 + if utils.SystemMemoryGB() < 3 { + return nil + } + + this.isAvailable = true + var lastId int64 for { hashList, maxId, err := db.ListHashes(lastId) @@ -42,18 +53,29 @@ func (this *FileListHashMap) Load(db *FileListDB) error { } func (this *FileListHashMap) Add(hash string) { + if !this.isAvailable { + return + } + this.locker.Lock() this.m[hash] = zero.New() this.locker.Unlock() } func (this *FileListHashMap) Delete(hash string) { + if !this.isAvailable { + return + } + this.locker.Lock() delete(this.m, hash) this.locker.Unlock() } func (this *FileListHashMap) Exist(hash string) bool { + if !this.isAvailable { + return true + } if !this.isReady { // 只有完全Ready时才能判断是否为false return true