コード例 #1
0
        protected PutRecordsResult InnerPutRecordsSupportingCache(PutRecordsRequest putRecordsParam, PutRecordMethod putRecordMethod)
        {
            if (_disConfig.IsDataCacheEnabled())
            {
                // 开启本地缓存
                PutRecordsResult putRecordsResult = null;
                try
                {
                    putRecordsResult = InnerPutRecordsWithRetry(putRecordsParam, putRecordMethod);
                }
                catch (Exception e)
                {
                    string errorMsg   = e.InnerException.Message;
                    int    statusCode = int.Parse(errorMsg.Split('\n')[0]);
                    // 如果不是可以重试的异常 或者 已达到重试次数,则直接抛出异常
                    if (Utils.Utils.IsCacheData(statusCode))
                    {
                        // 网络异常 全部记录上传失败
                        logger.Info("Local data cache is enabled, try to put failed records to local.");
                        CacheUtils.PutToCache(putRecordsParam, _disConfig); // 写入本地缓存
                    }
                    throw e;
                }

                try
                {
                    // 部分记录上传失败
                    if (putRecordsResult.FailedRecordCount > 0)
                    {
                        // 过滤出上传失败的记录
                        List <PutRecordsResultEntry>  putRecordsResultEntries        = putRecordsResult.Records;
                        List <PutRecordsRequestEntry> failedPutRecordsRequestEntries = new List <PutRecordsRequestEntry>();
                        int index = 0;
                        foreach (PutRecordsResultEntry putRecordsResultEntry in putRecordsResultEntries)
                        {
                            if (!String.IsNullOrEmpty(putRecordsResultEntry.ErrorCode))
                            {
                                failedPutRecordsRequestEntries.Add(putRecordsParam.Records[index]);
                            }
                            index++;
                        }
                        putRecordsParam.Records = failedPutRecordsRequestEntries;

                        logger.Info("Local data cache is enabled, try to put failed records to local.");

                        CacheUtils.PutToCache(putRecordsParam, _disConfig); // 写入本地缓存
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(putRecordsResult);
            }
            else
            {
                return(InnerPutRecordsWithRetry(putRecordsParam, putRecordMethod));
            }
        }