/// <summary>
        /// 获取公众号的AccessToken
        /// </summary>
        /// <returns>微信提供的AccessToken</returns>
        public string GetAccessToken()
        {
            if (WeChatConfig == null)
            {
                throw new NullReferenceException("请使用构造ConnectLinkUp(idOrAppId),或初始化Initialize(idOrAppId)");
            }
            string accessToken = String.Empty;
            //先从数据库获取Access_Token,如果不存在或已过期,则重新跟微信拿Access_Token
            string appId          = WeChatConfig.AppID;
            string accessTokenKey = string.Format(RedisKeyPrefix.WECHAT_ACCESS_TOKEN, appId);
            string updateTokenKey = string.Format(RedisKeyPrefix.WECHAT_TOKEN_CONCURRENT, appId);

            do
            {
                if ("Concurrent".Equals(accessToken))
                {
                    SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "GetAccessToken(),已有线程直接去微信获取AccessToken,在此等待");
                    Thread.Sleep(500);//发生并发的线程需要回到这里,等待单线程更新完成
                }
                AccessToken wechatAccessToken = redisHelper.StringGet <AccessToken>(accessTokenKey);
                if (wechatAccessToken != null)
                {
                    accessToken = wechatAccessToken.access_token;
                }
                else//Redis里面的Token已经失效,需要单线程去更新Token
                {
                    accessToken = ConcurrentControl.SingleUserFunc(updateTokenKey, "Concurrent", () =>
                    {
                        SystemLogHelper.Info(MethodBase.GetCurrentMethod(), "获取公众号的AccessToken,GetAccessToken(),直接去微信获取AccessToken");
                        return(GetWeChatAccessToken());
                    });
                }
            } while ("Concurrent".Equals(accessToken));
            return(accessToken);
        }
示例#2
0
        /// <summary>
        /// 保存笑话数据集合
        /// </summary>
        /// <param name="jokeInfoDtos">多个笑话数据的集合</param>
        /// <returns>保存成功的记录数</returns>
        public int SaveRangeJokes(List <JokeInfoDto> jokeInfoDtos)
        {
            if (jokeInfoDtos == null || jokeInfoDtos.Count == 0)
            {
                return(0);
            }
            Guid   firstId        = jokeInfoDtos[0].Id;
            string concurrencyKey = string.Format(RedisKeyPrefix.SAVE_JOKE_CONCURRENCY, firstId);
            int    saveResult     = ConcurrentControl.SingleUserFunc(concurrencyKey, 0, () =>
            {
                List <JokeInfo> jokeInfoList = AutoMapperHelper.MapToList <JokeInfo>(jokeInfoDtos);
                jokeInfoList.ForEach(joke =>
                {
                    joke.CreateBy = nameof(SaveRangeJokes) + Thread.CurrentThread.ManagedThreadId;
                    joke.Version  = SysConfigReader.Version;
                });
                int row = jokeInfoService.AddNeedJokeInfo(jokeInfoList);
                return(row);
            });

            return(saveResult);
        }