SaveBatch 保存批次信息
(batch *BatchUpload)
| 166 | |
| 167 | // SaveBatch 保存批次信息 |
| 168 | func (s *ProgressStorage) SaveBatch(batch *BatchUpload) error { |
| 169 | // 保存到内存缓存 |
| 170 | s.memoryCache.Store(fmt.Sprintf("batch:%s", batch.ID), batch) |
| 171 | |
| 172 | // 尝试保存到Redis |
| 173 | if redisClient := s.ensureRedisConnection(); redisClient != nil { |
| 174 | batchJSON, err := json.Marshal(batch) |
| 175 | if err != nil { |
| 176 | return fmt.Errorf("序列化批次信息失败: %v", err) |
| 177 | } |
| 178 | |
| 179 | key := fmt.Sprintf("batch_upload:batch:%s", batch.ID) |
| 180 | err = redisClient.Set(context.Background(), key, batchJSON, s.batchTimeout).Err() |
| 181 | if err != nil { |
| 182 | // Redis错误不应该阻止操作,只记录警告 |
| 183 | fmt.Printf("警告: 保存批次到Redis失败: %v\n", err) |
| 184 | } |
| 185 | |
| 186 | // 更新最后更新时间 |
| 187 | lastUpdateKey := fmt.Sprintf("batch_upload:last_update:%s", batch.ID) |
| 188 | redisClient.Set(context.Background(), lastUpdateKey, time.Now().UnixMilli(), s.batchTimeout) |
| 189 | } |
| 190 | |
| 191 | return nil |
| 192 | } |
| 193 | |
| 194 | // LoadBatch 加载批次信息 |
| 195 | func (s *ProgressStorage) LoadBatch(batchID string) (*BatchUpload, error) { |
no test coverage detected