perf(go): decouple realtime mysql projection

This commit is contained in:
lingniu
2026-07-03 11:49:13 +08:00
parent 6fb6262d0d
commit 76f5a20b79
3 changed files with 136 additions and 17 deletions

View File

@@ -185,26 +185,34 @@ func (r *Repository) setJSON(ctx context.Context, key string, value any, ttl tim
func (r *Repository) setKV(ctx context.Context, vin string, env envelope.FrameEnvelope, parsed map[string]any) error {
rows := realtimeKVFields(env, parsed)
if len(rows) == 0 {
return nil
}
grouped := make(map[string]map[string]any)
for _, row := range rows {
key := realtimeKVKey(row.Protocol, vin, row.Domain)
values := map[string]any{
row.Field: row.Value,
"_event_time_ms": strconv.FormatInt(row.EventTimeMS, 10),
"_received_at_ms": strconv.FormatInt(row.ReceivedAtMS, 10),
"_event_id": row.EventID,
"_protocol": string(row.Protocol),
"_vin": vin,
"_domain": row.Domain,
"_value_type:" + row.Field: row.ValueType,
}
if err := r.client.HSet(ctx, key, values).Err(); err != nil {
return err
}
if err := r.client.Expire(ctx, key, r.cfg.ttl()).Err(); err != nil {
return err
values := grouped[key]
if values == nil {
values = map[string]any{
"_event_time_ms": strconv.FormatInt(row.EventTimeMS, 10),
"_received_at_ms": strconv.FormatInt(row.ReceivedAtMS, 10),
"_event_id": row.EventID,
"_protocol": string(row.Protocol),
"_vin": vin,
"_domain": row.Domain,
}
grouped[key] = values
}
values[row.Field] = row.Value
values["_value_type:"+row.Field] = row.ValueType
}
return nil
pipe := r.client.Pipeline()
for key, values := range grouped {
pipe.HSet(ctx, key, values)
pipe.Expire(ctx, key, r.cfg.ttl())
}
_, err := pipe.Exec(ctx)
return err
}
func (r *Repository) addProtocol(ctx context.Context, vehicleKey string, protocol envelope.Protocol) ([]envelope.Protocol, error) {