2021-03-02

,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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值