2023-11-07 21:05:21 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"mayfly-go/internal/machine/mcm"
|
|
|
|
|
global_cache "mayfly-go/pkg/cache"
|
|
|
|
|
"mayfly-go/pkg/utils/jsonx"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-05 05:31:32 +00:00
|
|
|
const MachineStatCacheKey = "mayfly:machine:%d:stat"
|
2023-11-07 21:05:21 +08:00
|
|
|
|
|
|
|
|
func SaveMachineStats(machineId uint64, stat *mcm.Stats) error {
|
2025-04-23 20:36:32 +08:00
|
|
|
return global_cache.Set(fmt.Sprintf(MachineStatCacheKey, machineId), stat, 10*time.Minute)
|
2023-11-07 21:05:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetMachineStats(machineId uint64) (*mcm.Stats, error) {
|
2024-01-05 05:31:32 +00:00
|
|
|
cacheStr := global_cache.GetStr(fmt.Sprintf(MachineStatCacheKey, machineId))
|
2023-11-07 21:05:21 +08:00
|
|
|
if cacheStr == "" {
|
|
|
|
|
return nil, errors.New("不存在该值")
|
|
|
|
|
}
|
2025-05-24 16:22:54 +08:00
|
|
|
return jsonx.To[*mcm.Stats](cacheStr)
|
2023-11-07 21:05:21 +08:00
|
|
|
}
|