mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	
		
			
	
	
		
			25 lines
		
	
	
		
			633 B
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			25 lines
		
	
	
		
			633 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								package cache
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"errors"
							 | 
						||
| 
								 | 
							
									"fmt"
							 | 
						||
| 
								 | 
							
									"mayfly-go/internal/machine/mcm"
							 | 
						||
| 
								 | 
							
									global_cache "mayfly-go/pkg/cache"
							 | 
						||
| 
								 | 
							
									"mayfly-go/pkg/utils/jsonx"
							 | 
						||
| 
								 | 
							
									"time"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const MachineStatCackeKey = "mayfly:machine:%d:stat"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func SaveMachineStats(machineId uint64, stat *mcm.Stats) error {
							 | 
						||
| 
								 | 
							
									return global_cache.SetStr(fmt.Sprintf(MachineStatCackeKey, machineId), jsonx.ToStr(stat), 10*time.Minute)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func GetMachineStats(machineId uint64) (*mcm.Stats, error) {
							 | 
						||
| 
								 | 
							
									cacheStr := global_cache.GetStr(fmt.Sprintf(MachineStatCackeKey, machineId))
							 | 
						||
| 
								 | 
							
									if cacheStr == "" {
							 | 
						||
| 
								 | 
							
										return nil, errors.New("不存在该值")
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
									return jsonx.To(cacheStr, new(mcm.Stats))
							 | 
						||
| 
								 | 
							
								}
							 |