MCPcopy Create free account
hub / github.com/tbphp/gpt-load / addKeyToStore

Method addKeyToStore

internal/keypool/provider.go:554–573  ·  view source on GitHub ↗

addKeyToStore is a helper to add a single key to the cache.

(key *models.APIKey)

Source from the content-addressed store, hash-verified

552
553// addKeyToStore is a helper to add a single key to the cache.
554func (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 批量添加密钥到缓存(用于批量导入场景)
576func (p *KeyProvider) addKeysToCacheBatch(groupID uint, keys []models.APIKey) error {

Callers 2

RestoreKeysMethod · 0.95
RestoreMultipleKeysMethod · 0.95

Calls 4

apiKeyToMapMethod · 0.95
HSetMethod · 0.65
LRemMethod · 0.65
LPushMethod · 0.65

Tested by

no test coverage detected