RtaOrderSyncService.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.adx.tencent.rta;
  2. import com.adx.tencent.rta.model.RtaTagOrderRecord;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.data.redis.connection.RedisConnection;
  6. import org.springframework.data.redis.connection.RedisStringCommands;
  7. import org.springframework.data.redis.core.StringRedisTemplate;
  8. import org.springframework.data.redis.core.types.Expiration;
  9. import java.nio.charset.StandardCharsets;
  10. import java.time.Duration;
  11. import java.util.LinkedHashMap;
  12. import java.util.LinkedHashSet;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Set;
  16. import java.util.stream.Collectors;
  17. /**
  18. * RTA order_id 同步服务。
  19. * 定时从 rta_tag_orders 表读取全部记录,同步到 Redis:
  20. * key = {prefix}{tag_id}
  21. * value = order_id 列表(逗号分隔)
  22. */
  23. public class RtaOrderSyncService {
  24. private static final Logger log = LoggerFactory.getLogger(RtaOrderSyncService.class);
  25. private final RtaOrderStore store;
  26. private final StringRedisTemplate redis;
  27. private final String keyPrefix;
  28. private final Duration ttl;
  29. public RtaOrderSyncService(RtaOrderStore store,
  30. StringRedisTemplate redis,
  31. String keyPrefix,
  32. Duration ttl) {
  33. this.store = store;
  34. this.redis = redis;
  35. this.keyPrefix = keyPrefix == null ? "adx:rta:order:" : keyPrefix;
  36. this.ttl = ttl;
  37. }
  38. public int syncOnce() {
  39. List<RtaTagOrderRecord> rows = store.listAll();
  40. if (rows == null || rows.isEmpty()) {
  41. log.info("[RtaOrderSync] 无数据可同步");
  42. return 0;
  43. }
  44. Map<String, Set<String>> grouped = new LinkedHashMap<>();
  45. for (RtaTagOrderRecord row : rows) {
  46. if (row.getTagId() == null || row.getTagId().isBlank()) {
  47. continue;
  48. }
  49. if (row.getOrderId() == null || row.getOrderId().isBlank()) {
  50. continue;
  51. }
  52. grouped.computeIfAbsent(row.getTagId().trim(), ignored -> new LinkedHashSet<>())
  53. .add(row.getOrderId().trim());
  54. }
  55. if (grouped.isEmpty()) {
  56. log.info("[RtaOrderSync] 无有效数据可同步");
  57. return 0;
  58. }
  59. long ttlMs = (ttl != null && !ttl.isZero() && !ttl.isNegative()) ? ttl.toMillis() : 0;
  60. redis.executePipelined((RedisConnection conn) -> {
  61. for (Map.Entry<String, Set<String>> entry : grouped.entrySet()) {
  62. byte[] key = redisKey(entry.getKey()).getBytes(StandardCharsets.UTF_8);
  63. String value = entry.getValue().stream().collect(Collectors.joining(","));
  64. byte[] val = value.getBytes(StandardCharsets.UTF_8);
  65. if (ttlMs > 0) {
  66. conn.stringCommands().set(key, val,
  67. Expiration.milliseconds(ttlMs),
  68. RedisStringCommands.SetOption.UPSERT);
  69. } else {
  70. conn.stringCommands().set(key, val);
  71. }
  72. }
  73. return null;
  74. });
  75. log.info("[RtaOrderSync] 同步完成,共 {} 条 RTA order_id 写入 Redis, 聚合后 {} 个广告位",
  76. rows.size(), grouped.size());
  77. return rows.size();
  78. }
  79. private String redisKey(String tagId) {
  80. return keyPrefix + tagId;
  81. }
  82. }