addKeyToStore is a helper to add a single key to the cache.
(key *models.APIKey)
| 552 | |
| 553 | // addKeyToStore is a helper to add a single key to the cache. |
| 554 | func (p *KeyProvider) addKeyToStore(key *models.APIKey) error { |
| 555 | // 1. Store key details in HASH |
| 556 | keyHashKey := fmt.Sprintf("key:%d", key.ID) |
| 557 | keyDetails := p.apiKeyToMap(key) |
| 558 | if err := p.store.HSet(keyHashKey, keyDetails); err != nil { |
| 559 | return fmt.Errorf("failed to HSet key details for key %d: %w", key.ID, err) |
| 560 | } |
| 561 | |
| 562 | // 2. If active, add to the active LIST |
| 563 | if key.Status == models.KeyStatusActive { |
| 564 | activeKeysListKey := fmt.Sprintf("group:%d:active_keys", key.GroupID) |
| 565 | if err := p.store.LRem(activeKeysListKey, 0, key.ID); err != nil { |
| 566 | return fmt.Errorf("failed to LRem key %d before LPush for group %d: %w", key.ID, key.GroupID, err) |
| 567 | } |
| 568 | if err := p.store.LPush(activeKeysListKey, key.ID); err != nil { |
| 569 | return fmt.Errorf("failed to LPush key %d to group %d: %w", key.ID, key.GroupID, err) |
| 570 | } |
| 571 | } |
| 572 | return nil |
| 573 | } |
| 574 | |
| 575 | // addKeysToCacheBatch 批量添加密钥到缓存(用于批量导入场景) |
| 576 | func (p *KeyProvider) addKeysToCacheBatch(groupID uint, keys []models.APIKey) error { |
no test coverage detected