RiskControlCore.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using System.Threading.Channels;
  10. namespace molilian.core
  11. {
  12. public partial class RiskControlCore
  13. {
  14. private static Dictionary<string, int> _calls = new();
  15. private static Dictionary<string, decimal> _incomeAmt = new();
  16. public static void Refresh()
  17. {
  18. _incomeAmt = [];
  19. }
  20. public static void ResetCalls()
  21. {
  22. _calls = [];
  23. }
  24. internal static void CallsIncrBy(TkChannelEnum channel, int accountId)
  25. {
  26. CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMdd"));
  27. CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMddHH"));
  28. }
  29. internal static void CallsIncrBy(TkChannelEnum channel, int accountId, string flag)
  30. {
  31. string key = $"{channel}:{accountId}:{flag}";
  32. if (_calls.ContainsKey(key))
  33. {
  34. _calls[key] += 1;
  35. }
  36. else
  37. {
  38. _calls[key] = 1;
  39. }
  40. string cache_key = $"RiskControl:{key}:calls:{flag}";
  41. RedisHelper.IncrBy(cache_key);
  42. RedisHelper.Expire(cache_key, 3 * 86400);
  43. }
  44. internal static void SetCalls(TkChannelEnum channel, int accountId, string flag, int val)
  45. {
  46. string key = $"{channel}:{accountId}:{flag}";
  47. if (_calls.ContainsKey(key))
  48. {
  49. _calls[key] = val;
  50. }
  51. else
  52. {
  53. _calls[key] = val;
  54. }
  55. string cache_key = $"RiskControl:{key}:calls:{flag}";
  56. RedisHelper.IncrBy(cache_key);
  57. RedisHelper.Expire(cache_key, 3 * 86400);
  58. }
  59. public static int GetAllNodesCalls(TkChannelEnum channel, int accountId, string flag)
  60. {
  61. string key = $"{channel}:{accountId}:{flag}";
  62. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  63. {
  64. if (!node.is_public_api) return 0;
  65. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  66. #if DEBUG
  67. switch (node.name)
  68. {
  69. case "bj":
  70. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  71. break;
  72. case "gz":
  73. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  74. break;
  75. case "coupon1":
  76. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  77. break;
  78. default: return 0;
  79. }
  80. #endif
  81. string cache_key = $"RiskControl:{key}:calls:{flag}";
  82. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  83. return redis.Get<int>(cache_key);
  84. });
  85. int num = result.Sum();
  86. _calls.TryAdd(key, num);
  87. return num;
  88. }
  89. public static int GetCalls(TkChannelEnum channel, int accountId, string flag)
  90. {
  91. try
  92. {
  93. string key = $"{channel}:{accountId}:{flag}";
  94. if (_calls.ContainsKey(key)) return _calls[key];
  95. string cache_key = $"RiskControl:{key}:calls:{flag}";
  96. int num = RedisHelper.Get<int>(cache_key);
  97. _calls.TryAdd(key, num);
  98. return num;
  99. }
  100. catch (Exception ex) { return 0; }
  101. }
  102. internal static void SaveIncomeAmt(TkChannelEnum channel, string accountName, decimal income_amt)
  103. {
  104. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  105. if (_incomeAmt.ContainsKey(key))
  106. {
  107. _incomeAmt[key] = income_amt;
  108. }
  109. else
  110. {
  111. _incomeAmt.Add(key, income_amt);
  112. }
  113. string cache_key = $"RiskControl:{key}:income_amt";
  114. var result = EndPointCore.ProcessEndPointNodes<bool>(node =>
  115. {
  116. if (!node.is_public_api) return true;
  117. if (string.IsNullOrEmpty(node.redis_server)) return true;
  118. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  119. return redis.Set(cache_key, income_amt, 3 * 86400);
  120. });
  121. }
  122. public static decimal GetIncomeAmt(TkChannelEnum channel, string accountName)
  123. {
  124. try
  125. {
  126. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  127. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  128. string cache_key = $"RiskControl:{key}:income_amt";
  129. return RedisHelper.Get<decimal>(cache_key);
  130. }
  131. catch (Exception ex) { return 0; }
  132. }
  133. }
  134. }