diff --git a/internal/caches/manager.go b/internal/caches/manager.go index b6e39ed..fc424fa 100644 --- a/internal/caches/manager.go +++ b/internal/caches/manager.go @@ -138,3 +138,27 @@ func (this *Manager) NewStorageWithPolicy(policy *serverconfigs.HTTPCachePolicy) } return nil } + +// TotalDiskSize 消耗的磁盘尺寸 +func (this *Manager) TotalDiskSize() int64 { + this.locker.RLock() + defer this.locker.RUnlock() + + total := int64(0) + for _, storage := range this.storageMap { + total += storage.TotalDiskSize() + } + return total +} + +// TotalMemorySize 消耗的内存尺寸 +func (this *Manager) TotalMemorySize() int64 { + this.locker.RLock() + defer this.locker.RUnlock() + + total := int64(0) + for _, storage := range this.storageMap { + total += storage.TotalMemorySize() + } + return total +} diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index 057c485..8b3a224 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -496,6 +496,19 @@ func (this *FileStorage) Stop() { } } +// TotalDiskSize 消耗的磁盘尺寸 +func (this *FileStorage) TotalDiskSize() int64 { + return atomic.LoadInt64(&this.totalSize) +} + +// TotalMemorySize 内存尺寸 +func (this *FileStorage) TotalMemorySize() int64 { + if this.memoryStorage == nil { + return 0 + } + return this.memoryStorage.TotalMemorySize() +} + // 绝对路径 func (this *FileStorage) dir() string { return this.cacheConfig.Dir + "/p" + strconv.FormatInt(this.policy.Id, 10) + "/" diff --git a/internal/caches/storage_interface.go b/internal/caches/storage_interface.go index 23b5167..3d59f18 100644 --- a/internal/caches/storage_interface.go +++ b/internal/caches/storage_interface.go @@ -4,35 +4,41 @@ import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" ) -// 缓存存储接口 +// StorageInterface 缓存存储接口 type StorageInterface interface { - // 初始化 + // Init 初始化 Init() error - // 读取缓存 + // OpenReader 读取缓存 OpenReader(key string) (Reader, error) - // 打开缓存写入器等待写入 + // OpenWriter 打开缓存写入器等待写入 OpenWriter(key string, expiredAt int64, status int) (Writer, error) - // 删除某个键值对应的缓存 + // Delete 删除某个键值对应的缓存 Delete(key string) error - // 统计缓存 + // Stat 统计缓存 Stat() (*Stat, error) - // 清除所有缓存 + // TotalDiskSize 消耗的磁盘尺寸 + TotalDiskSize() int64 + + // TotalMemorySize 内存尺寸 + TotalMemorySize() int64 + + // CleanAll 清除所有缓存 CleanAll() error - // 批量删除缓存 + // Purge 批量删除缓存 Purge(keys []string, urlType string) error - // 停止缓存策略 + // Stop 停止缓存策略 Stop() - // 获取当前存储的Policy + // Policy 获取当前存储的Policy Policy() *serverconfigs.HTTPCachePolicy - // 将缓存添加到列表 + // AddToList 将缓存添加到列表 AddToList(item *Item) } diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index c40e68a..ef398be 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -183,6 +183,16 @@ func (this *MemoryStorage) AddToList(item *Item) { this.list.Add(hash, item) } +// TotalDiskSize 消耗的磁盘尺寸 +func (this *MemoryStorage) TotalDiskSize() int64 { + return 0 +} + +// TotalMemorySize 内存尺寸 +func (this *MemoryStorage) TotalMemorySize() int64 { + return atomic.LoadInt64(&this.totalSize) +} + // 计算Key Hash func (this *MemoryStorage) hash(key string) uint64 { return xxhash.Sum64String(key) diff --git a/internal/nodes/node_status_executor.go b/internal/nodes/node_status_executor.go index b5cd59c..db67dd3 100644 --- a/internal/nodes/node_status_executor.go +++ b/internal/nodes/node_status_executor.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeNode/internal/caches" teaconst "github.com/TeaOSLab/EdgeNode/internal/const" "github.com/TeaOSLab/EdgeNode/internal/events" "github.com/TeaOSLab/EdgeNode/internal/monitor" @@ -64,6 +65,8 @@ func (this *NodeStatusExecutor) update() { status.ConfigVersion = sharedNodeConfig.Version status.IsActive = true status.ConnectionCount = sharedListenerManager.TotalActiveConnections() + status.CacheTotalDiskSize = caches.SharedManager.TotalDiskSize() + status.CacheTotalMemorySize = caches.SharedManager.TotalMemorySize() // 记录监控数据 monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemConnections, maps.Map{