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



我正在研究一个需求,我需要限制每个用户的请求数量(调用webservice),例如每个用户每天100次更新或创建。所以,我写了一些c#代码使用缓存来做这项工作,但它失败了,因为我没有得到每一个用户计数。它只是将所有用户每天的更新限制在100次。你能帮我一下吗?下面是我写的代码。现在我需要获得每个用户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;
}

最新更新