TaobaoGeneralLinkParseCacheCore.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using dodohold.core;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace molilian.core
  5. {
  6. public static class TaobaoGeneralLinkParseCacheCore
  7. {
  8. // 成功结果缓存 1 天;确定无法解析的链接缓存 1 小时;网络异常等临时错误不缓存
  9. public const int SuccessExpireSeconds = 86400;
  10. public const int FailureExpireSeconds = 3600;
  11. // 统计跨天保留,每次通知领取上次通知后的全部次数;同一天只允许领取一次。
  12. private const string ClaimNotificationScript = @"
  13. local count = redis.call('HINCRBY', KEYS[1], 'pending', 1)
  14. local lastDate = redis.call('HGET', KEYS[1], 'last_notify_date')
  15. if lastDate and lastDate >= ARGV[1] then
  16. return 0
  17. end
  18. redis.call('HSET', KEYS[1], 'last_notify_date', ARGV[1])
  19. redis.call('HSET', KEYS[1], 'pending', 0)
  20. return count";
  21. public static async Task<TaobaoGeneralLinkParseCacheDTO?> GetAsync(string content)
  22. {
  23. try
  24. {
  25. return await CenterHub.Redis.GetAsync<TaobaoGeneralLinkParseCacheDTO>(GetCacheKey(content));
  26. }
  27. catch (Exception ex)
  28. {
  29. LogFailure("cache_read_error", ex);
  30. return null;
  31. }
  32. }
  33. public static async Task SaveAsync(string content, TaobaoGeneralLinkParseCacheDTO record, int expireSeconds)
  34. {
  35. if (expireSeconds <= 0) return;
  36. try
  37. {
  38. await CenterHub.Redis.SetAsync(GetCacheKey(content), record, expireSeconds);
  39. }
  40. catch (Exception ex)
  41. {
  42. LogFailure("cache_write_error", ex);
  43. }
  44. }
  45. public static async Task NotifyFailureOnceAsync(string reason, int parseAccountId, int consumerAccountId)
  46. {
  47. reason = string.IsNullOrWhiteSpace(reason) ? "未知错误" : reason.Trim();
  48. string key = $"notify:taobao:general_link_parse:v1:{Hash(reason)}";
  49. long count = 0;
  50. try
  51. {
  52. count = Convert.ToInt64(await CenterHub.Redis.EvalAsync(ClaimNotificationScript, key, DateTime.Now.ToString("yyyyMMdd")));
  53. if (count <= 0) return;
  54. await NotifyCore.NotifyAsync(
  55. $"【淘宝万能解析失败】\n错误信息:{reason}\n" +
  56. $"自上次相同错误通知以来发生:{count} 次(含本次)\n" +
  57. $"使用账号:{consumerAccountId},解析账号:{parseAccountId}\n已回退原短链解析逻辑。");
  58. }
  59. catch (Exception ex)
  60. {
  61. if (count > 0)
  62. {
  63. try
  64. {
  65. // 发送失败的次数留给下一次通知,同一天不重复尝试发送。
  66. await CenterHub.Redis.HIncrByAsync(key, "pending", count);
  67. }
  68. catch (Exception restoreException)
  69. {
  70. LogFailure("notify_count_restore_error", restoreException);
  71. }
  72. }
  73. LogFailure("notify_error", ex);
  74. }
  75. }
  76. private static string GetCacheKey(string content)
  77. {
  78. // v2:精简缓存结构,不与 v1 旧数据混用
  79. // 取 SHA256 前 8 字节(16 位十六进制),压缩 key 占用
  80. string hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(content.Trim())), 0, 8);
  81. return $"cache:taobao:general_link_parse:v2:{hash}";
  82. }
  83. private static string Hash(string value)
  84. {
  85. return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(value)));
  86. }
  87. private static void LogFailure(string operation, Exception ex)
  88. {
  89. _ = new LoggerLibrary("tbk_dg_general_link_parse", operation)
  90. .Info(ex.Message, ex.StackTrace)
  91. .SaveAsync();
  92. }
  93. }
  94. }