简体   繁体   中英

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. 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. it's just limiting the updates to 100 per day for all the users. 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).

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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