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 {
|
2024-01-05 05:31:32 +00:00
|
|
|
return global_cache.SetStr(fmt.Sprintf(MachineStatCacheKey, machineId), jsonx.ToStr(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("不存在该值")
|
|
|
|
|
}
|
|
|
|
|
return jsonx.To(cacheStr, new(mcm.Stats))
|
|
|
|
|
}
|