TaobaoShortLinkCacheCore.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using dodohold.core;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace molilian.core
  5. {
  6. public static class TaobaoShortLinkCacheCore
  7. {
  8. // 短链每次走 HTTP 解析都计数(保留 1 天),超过 CacheAfterCount 次才缓存长链,避免一次性短链占用内存
  9. public const int CacheExpireSeconds = 86400;
  10. public const int CounterExpireSeconds = 86400;
  11. public const int CacheAfterCount = 3;
  12. public static async Task<string?> GetAsync(string shortUrl)
  13. {
  14. try
  15. {
  16. string longUrl = await CenterHub.Redis.GetAsync<string>(GetCacheKey(Hash(shortUrl)));
  17. return IsLongUrl(longUrl) ? longUrl : null;
  18. }
  19. catch (Exception ex)
  20. {
  21. LogFailure("read", ex);
  22. return null;
  23. }
  24. }
  25. public static string? Get(string shortUrl)
  26. {
  27. try
  28. {
  29. string longUrl = CenterHub.Redis.Get<string>(GetCacheKey(Hash(shortUrl)));
  30. return IsLongUrl(longUrl) ? longUrl : null;
  31. }
  32. catch (Exception ex)
  33. {
  34. LogFailure("read", ex);
  35. return null;
  36. }
  37. }
  38. public static async Task SaveAsync(string shortUrl, string longUrl)
  39. {
  40. if (!IsLongUrl(longUrl)) return;
  41. try
  42. {
  43. string hash = Hash(shortUrl);
  44. string counterKey = GetCounterKey(hash);
  45. long count = await CenterHub.Redis.IncrByAsync(counterKey);
  46. if (count == 1) await CenterHub.Redis.ExpireAsync(counterKey, CounterExpireSeconds);
  47. if (count <= CacheAfterCount) return;
  48. await CenterHub.Redis.SetAsync(GetCacheKey(hash), longUrl, CacheExpireSeconds);
  49. }
  50. catch (Exception ex)
  51. {
  52. LogFailure("write", ex);
  53. }
  54. }
  55. public static void Save(string shortUrl, string longUrl)
  56. {
  57. if (!IsLongUrl(longUrl)) return;
  58. try
  59. {
  60. string hash = Hash(shortUrl);
  61. string counterKey = GetCounterKey(hash);
  62. long count = CenterHub.Redis.IncrBy(counterKey);
  63. if (count == 1) CenterHub.Redis.Expire(counterKey, CounterExpireSeconds);
  64. if (count <= CacheAfterCount) return;
  65. CenterHub.Redis.Set(GetCacheKey(hash), longUrl, CacheExpireSeconds);
  66. }
  67. catch (Exception ex)
  68. {
  69. LogFailure("write", ex);
  70. }
  71. }
  72. private static bool IsLongUrl(string? url)
  73. {
  74. return Uri.TryCreate(url, UriKind.Absolute, out var uri) &&
  75. (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) &&
  76. !AlimamaPlus.IsShortLink(url!);
  77. }
  78. // 取 SHA256 前 8 字节(16 位十六进制),压缩 key 占用,碰撞概率可忽略
  79. private static string Hash(string shortUrl)
  80. {
  81. return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(shortUrl.Trim())), 0, 8);
  82. }
  83. // v2:改为计数达标后才缓存,不与 v1 旧数据混用
  84. private static string GetCacheKey(string hash) => $"cache:taobao:short_link_http:v2:{hash}";
  85. private static string GetCounterKey(string hash) => $"cache:taobao:short_link_http:cnt:{hash}";
  86. private static void LogFailure(string operation, Exception ex)
  87. {
  88. _ = new LoggerLibrary("taobao_short_link_cache", operation)
  89. .Info(ex.Message, ex.StackTrace)
  90. .SaveAsync();
  91. }
  92. }
  93. }