| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using System.Threading.Channels;
- namespace molilian.core
- {
- public partial class RiskControlCore
- {
- private static Dictionary<string, int> _calls = new();
- private static Dictionary<string, decimal> _incomeAmt = new();
- public static void Refresh()
- {
- _incomeAmt = [];
- }
- public static void ResetCalls()
- {
- _calls = [];
- }
- internal static void CallsIncrBy(TkChannelEnum channel, int accountId)
- {
- CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMdd"));
- CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMddHH"));
- }
- internal static void CallsIncrBy(TkChannelEnum channel, int accountId, string flag)
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] += 1;
- }
- else
- {
- _calls[key] = 1;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 3 * 86400);
- }
- internal static void SetCalls(TkChannelEnum channel, int accountId, string flag, int val)
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] = val;
- }
- else
- {
- _calls[key] = val;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 3 * 86400);
- }
- public static int GetAllNodesCalls(TkChannelEnum channel, int accountId, string flag)
- {
- string key = $"{channel}:{accountId}:{flag}";
- var result = EndPointCore.ProcessEndPointNodes<int>(node =>
- {
- if (!node.is_public_api) return 0;
- if (string.IsNullOrEmpty(node.redis_server)) return 0;
- #if DEBUG
- switch (node.name)
- {
- case "bj":
- node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
- break;
- case "gz":
- node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
- break;
- case "coupon1":
- node.redis_server = "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
- break;
- default: return 0;
- }
- #endif
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- var redis = RedisClientManager.GetRedisClient(node.redis_server);
- return redis.Get<int>(cache_key);
- });
- int num = result.Sum();
- _calls.TryAdd(key, num);
- return num;
- }
- public static int GetCalls(TkChannelEnum channel, int accountId, string flag)
- {
- try
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key)) return _calls[key];
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- int num = RedisHelper.Get<int>(cache_key);
- _calls.TryAdd(key, num);
- return num;
- }
- catch (Exception ex) { return 0; }
- }
- internal static void SaveIncomeAmt(TkChannelEnum channel, string accountName, decimal income_amt)
- {
- string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
- if (_incomeAmt.ContainsKey(key))
- {
- _incomeAmt[key] = income_amt;
- }
- else
- {
- _incomeAmt.Add(key, income_amt);
- }
- string cache_key = $"RiskControl:{key}:income_amt";
- var result = EndPointCore.ProcessEndPointNodes<bool>(node =>
- {
- if (!node.is_public_api) return true;
- if (string.IsNullOrEmpty(node.redis_server)) return true;
- var redis = RedisClientManager.GetRedisClient(node.redis_server);
- return redis.Set(cache_key, income_amt, 3 * 86400);
- });
- }
- public static decimal GetIncomeAmt(TkChannelEnum channel, string accountName)
- {
- try
- {
- string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
- if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
- string cache_key = $"RiskControl:{key}:income_amt";
- return RedisHelper.Get<decimal>(cache_key);
- }
- catch (Exception ex) { return 0; }
- }
- }
- }
|