package service import ( "context" "crypto/sha256" "encoding/hex" "fmt" "github.com/ez-api/ez-api/internal/model" "github.com/redis/go-redis/v9" ) type SyncService struct { rdb *redis.Client } func NewSyncService(rdb *redis.Client) *SyncService { return &SyncService{rdb: rdb} } func (s *SyncService) SyncKey(key *model.Key) error { // Hash the token hasher := sha256.New() hasher.Write([]byte(key.KeySecret)) tokenHash := hex.EncodeToString(hasher.Sum(nil)) redisKey := fmt.Sprintf("auth:token:%s", tokenHash) // Store in Redis // Using HSet to store multiple fields fields := map[string]interface{}{ "status": key.Status, "balance": key.Balance, } if key.ProviderID != nil { fields["master_id"] = *key.ProviderID } else { fields["master_id"] = 0 // Default or handle as needed } err := s.rdb.HSet(context.Background(), redisKey, fields).Err() return err }