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"); } }
/// <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); }