public void OCRFilter() { var Total = ConfigurationUtil.TencentOCR_KingKey_Total; //购买总数 var Warning = ConfigurationUtil.TencentOCR_KingKey_Warning; //预警次数 var RedisKey = ConfigurationUtil.TencentOCR_KingKey_RedisKey; var HashId = ConfigurationUtil.TencentOCR_KingKey_HashId; var WarningHz = ConfigurationUtil.TencentOCR_KingKey_WarningHz;//预警频率 PooledRedisClientHelper.SetEntryInHashIfNotExists(HashId, RedisKey, "0"); var OCRUseCount = int.Parse(PooledRedisClientHelper.GetHashField(HashId, RedisKey)); //已用次数 var Balance = Total - OCRUseCount; //剩余次数 if (Balance <= 0) { Send($@"【余额不足】 购买的OCR次数不足。 【已用次数】:{OCRUseCount} 【可用次数】:{Balance} 【总购买数】:{Total}"); throw new Exception("余额不足"); } if (Balance > 0 && Balance <= Warning && Balance % WarningHz == 0)//可用,次数小于预警,1000次提醒一次 { Send($@"【警告】购买的OCR次数预警。 【已用次数】:{OCRUseCount} 【可用次数】:{Balance} 【总购买数】:{Total}"); } }
public EnterpriseWeChatHelper(string CorpId, string CorpSecret) { corpid = CorpId; corpsecret = CorpSecret; if (!PooledRedisClientHelper.ContainsKey("WeChat_Token")) { var result = GetToken(corpid, corpsecret); if (result == null) { throw new Exception("请求企业微信token失败"); } if (result.errcode != 0) { throw new Exception(result.errmsg); } if (string.IsNullOrWhiteSpace(result.access_token)) { throw new Exception("请求企业微信token失败"); } token = result.access_token; PooledRedisClientHelper.Set <string>("WeChat_Token", result.access_token, TimeSpan.FromSeconds(result.expires_in)); } else { token = PooledRedisClientHelper.GetValueString("WeChat_Token"); } }
public void SetHashIncr(int value) { var HashId = "OCR"; var redisKey = "Kingkey_UseCount"; try { PooledRedisClientHelper.SetEntryInHashIfNotExists(HashId, redisKey, "0"); PooledRedisClientHelper.SetHashIncr(HashId, redisKey, value); } catch (Exception ex) { Send($@"【操作Redis失败】 : 【HashId】:【 {HashId} 】 【redisKey】:【 {redisKey} 】 【异常消息】:【 {ex.Message} 】 【堆栈信息】:【 {ex.StackTrace} 】"); throw new Exception("操作Redis失败"); } }
/// <summary> /// 根据委托取数据 /// </summary> /// <typeparam name="P">委托参数类型</typeparam> /// <typeparam name="T">返回类型</typeparam> /// <param name="Key">缓存键</param> /// <param name="Hour">缓存绝对时间</param> /// <param name="func">委托</param> /// <param name="param">委托参数</param> /// <returns></returns> public static T GetCacheData <P, T>(string Key, double Hour, Func <P, T> func, P param) where T : class { T Value = default; if (PooledRedisClientHelper.ContainsKey(Key)) { Value = PooledRedisClientHelper.GetT <T>(Key); } else { Value = func(param); } if (Hour != 0) //有效时长设置为0时 不设置缓存 { if (Value != null) { PooledRedisClientHelper.Set(Key, Value, TimeSpan.FromHours(Hour)); } } return(Value); }
public TokenResponse CreateToken(string appid, string appsecret) { Account account = _accountDAO.GetByAppId(appid); if (account == null || account.AppSecret != appsecret || account.Status == 0) { throw new Exception("账号不存在或账号未启用"); } var RedisKey = $"GetToken_AppId_{appid}"; if (PooledRedisClientHelper.ContainsKey(RedisKey)) { return(PooledRedisClientHelper.GetT <TokenResponse>(RedisKey)); } var token = BuildToken(account); PooledRedisClientHelper.Set <TokenResponse>(RedisKey, token, TimeSpan.FromMinutes(30)); return(token); }
/// <summary> /// 根据传入T与sqlwhere获取结果,转化为Tretrun /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <typeparam name="Tretrun">返回类型</typeparam> /// <param name="Key">缓存键</param> /// <param name="Hour">缓存绝对时间</param> /// <param name="sqlWhere">sql where</param> /// <returns></returns> public static Tretrun GetCacheFromEntity <T, Tretrun>(string Key, double Hour, string sqlWhere = "") where T : AbstractEntity where Tretrun : class { Tretrun Value = default; if (PooledRedisClientHelper.ContainsKey(Key)) { Value = PooledRedisClientHelper.GetT <T>(Key) as Tretrun; } else { lock (cacheLocker)//避免缓存并发 { if (!PooledRedisClientHelper.ContainsKey(Key)) { AS.OCR.Dapper.Base.Infrastructure <T> infrastructure = new Dapper.Base.Infrastructure <T>(); if (typeof(Tretrun).IsGenericType) { Value = infrastructure.GetList(sqlWhere) as Tretrun; } else { Value = infrastructure.GetModel(sqlWhere) as Tretrun; } } } if (Hour != 0) //有效时长设置为0时 不设置缓存 { if (Value != null) { PooledRedisClientHelper.Set(Key, Value, TimeSpan.FromHours(Hour)); } } } return(Value); }