简体   繁体   English

应该允许每个用户每天使用 ASP.NET C# 代码进行 100 次更新/创建

[英]should allow 100 update/create per day per user using ASP.NET C# code

I am working on a requirement where i need to limit the number of request(to call webservice) Per user like 100 updates or creates per day per each user.我正在处理一个要求,我需要限制每个用户的请求数量(调用 web 服务),例如每个用户每天 100 次更新或创建。 So, I have written some C# code using Cache to do that job but it's failing since i am not getting each and every user count.因此,我使用 Cache 编写了一些 C# 代码来完成这项工作,但它失败了,因为我没有得到每个用户的计数。 it's just limiting the updates to 100 per day for all the users.它只是将所有用户的更新限制为每天 100 次。 Could you please help me with it below is the code that i have written.你能帮我吗下面是我写的代码。 Now i need to get each user id and each user operation count(whether he updated anything).现在我需要获取每个用户 ID 和每个用户操作计数(他是否更新了任何内容)。

String Id = request.UserCode + "" + "Synchronize";
System.Runtime.Caching.MemoryCache cache = MemoryCache.Default;
var _Limitcount = 100;

string key = Id;
SynchronizeProposalRequestOutput sPRO = null;
if (cache[key] == null)
{
    sPRO= ProposalServiceControl.SynchronizeProposal(request);( operation for update)
    cache.Set(key, count, DateTime.UtcNow.AddDays(1), null);
    count++;

}
else if (cache[key] != null && count > _Limitcount)
{
    PrimaLog.Error(request.UserCode + " Proposal synchronization failed user reached the number of limits");
    throw new Exception("You have reached the Maximum number of Proposal updates per today");

}
else if (cache[key] != null && count <= _Limitcount)
{
    sPRO = ProposalServiceControl.SynchronizeProposal(request);
       
    count++;
    cache.Set(key, count, DateTime.UtcNow.AddDays(1), null);
}
//3 execution
return sPRO;
 string Id = request.UserCode + "" + "Synchronize";
 System.Runtime.Caching.MemoryCache cache = MemoryCache.Default;
 var _Limitcount = LimitProposalNumber;
 string key = Id;
 if (cache[key] == null)
  {
   sPRO = ProposalStorageServiceController.SynchronizeProposal(request);
   int count = 0;
   cache.Set(key, ++count, DateTime.UtcNow.AddDays(1), null);
  }
  else
  {
   int currentUserCount = (int)cache.Get(key);
   if (currentUserCount <= _Limitcount)
       {
  sPRO = ProposalStorageServiceController.SynchronizeProposal(request);
                    cache.Set(key, ++currentUserCount, DateTime.UtcNow.AddDays(1), null);
                }
                else
                {
                    PrimaLog.Error(request.UserCode + " Proposal synchronization failed user reached the number of limits");
                    throw new Exception("You have reached the Maximum number of Proposal updates per today");
                }
            }
            //3 execution
            return sPRO;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM