新增Windows代理程序支持
This commit is contained in:
@@ -511,6 +511,18 @@ func HandleMetricsPost(c *gin.Context) {
|
||||
var globalStorage *storage.Storage
|
||||
var deviceStorage device.Storage
|
||||
|
||||
// 硬件信息缓存结构
|
||||
type hardwareCacheItem struct {
|
||||
Data map[string]interface{}
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// 硬件信息缓存
|
||||
var (
|
||||
hardwareCache sync.Map
|
||||
cacheDuration = 5 * time.Minute // 缓存过期时间5分钟
|
||||
)
|
||||
|
||||
// SetStorage 设置全局存储实例
|
||||
func SetStorage(s *storage.Storage) {
|
||||
globalStorage = s
|
||||
@@ -1275,12 +1287,35 @@ func GetHardwareMetrics(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 从存储中查询硬件信息
|
||||
hardwareInfo, err := globalStorage.QueryHardwareMetrics(context.Background(), deviceID)
|
||||
// 尝试从缓存中获取硬件信息
|
||||
var hardwareInfo map[string]interface{}
|
||||
var err error
|
||||
|
||||
if cached, ok := hardwareCache.Load(deviceID); ok {
|
||||
cacheItem := cached.(hardwareCacheItem)
|
||||
if time.Now().Before(cacheItem.ExpiresAt) {
|
||||
// 缓存有效
|
||||
hardwareInfo = cacheItem.Data
|
||||
} else {
|
||||
// 缓存过期,删除并重新查询
|
||||
hardwareCache.Delete(deviceID)
|
||||
hardwareInfo, err = globalStorage.QueryHardwareMetrics(context.Background(), deviceID)
|
||||
}
|
||||
} else {
|
||||
// 缓存未命中,查询数据库
|
||||
hardwareInfo, err = globalStorage.QueryHardwareMetrics(context.Background(), deviceID)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to query hardware metrics: %v", err)
|
||||
// 查询失败时,返回一个空的硬件信息对象
|
||||
hardwareInfo = make(map[string]interface{})
|
||||
} else if len(hardwareInfo) > 0 {
|
||||
// 查询成功且有数据,更新缓存
|
||||
hardwareCache.Store(deviceID, hardwareCacheItem{
|
||||
Data: hardwareInfo,
|
||||
ExpiresAt: time.Now().Add(cacheDuration),
|
||||
})
|
||||
}
|
||||
|
||||
// 为了确保返回的结构与前端期望的一致,我们需要将查询结果转换为期望的格式
|
||||
|
||||
Reference in New Issue
Block a user