,1,模拟并发:
var result = new RepositoryResult<Dictionary<string, string>>() { IsSuccess = false };
var taskList = new List<Task>();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (var i = 0; i < 1000; i++)
{
taskList.Add(getUrltest());
}
Task t = Task.WhenAll(taskList.ToArray());
try
{
await t;
}
catch { }
sw.Stop();
//{sw.ElapsedMilliseconds.ToString()}");
if (t.Status == TaskStatus.RanToCompletion)
result.Message = "success " + sw.ElapsedMilliseconds.ToString();
else if (t.Status == TaskStatus.Faulted)
result.Message = "failed " + sw.ElapsedMilliseconds.ToString();
return result;
2,MemoryCache 使用
public async Task GetOrCreateGoodsInfo(GetGoodsInfoRequest request)
{
var key = string.Concat(prefix, request.DesignKey.ToUpper(), request.GoodsCode.ToUpper());
var cacheEntity = memoryCache.GetOrCreate(key, (ICacheEntry cacheEntry) =>
{
cacheEntry.SlidingExpiration = TimeSpan.FromMinutes(180);
cacheEntry.Size = 1;
var goodsInfo = AssetManagementService.GetGoodsByDesignKey(request.GoodsCode, request.DesignKey);
if (goodsInfo.Result.Success == false || goodsInfo.Result.Data == null || goodsInfo.Result.Data.Count == 0)
{
throw new Exception(Messages.AssetManagementGoodsMsg0009);
}
var galaxyInfo = GalaxyService.GetGoodsSkuByGoodsSkuId(request.DesignKey);
if (galaxyInfo.Result.Success == false || galaxyInfo.Result.Data == null)
{
throw new Exception(Messages.AssetManagementGoodsMsg0009);
}
var response = new GetGoodsInfoResponse()
{
GoodsName = goodsInfo.Result.Data.FirstOrDefault().GoodsName,
MetaData = CommonExtensions.DeserializeJson<object>(goodsInfo.Result.Data.FirstOrDefault().MetaData.ToString()),
GalaxyInfo = galaxyInfo.Result.Data
};
return response;
});
if (cacheEntity == null)
{
memoryCache.Remove(key);
}
return cacheEntity;
}