zoukankan      html  css  js  c++  java
  • asp.net中限制用户单位时间请求数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace WebApplication1
    {
        
    public class RequestCounter
        {
            
    private static Dictionary<string, RequestCounter> Counters = new Dictionary<string, RequestCounter>();
            
    public const int MaxRequestCount = 3;
            
    public const int ResetSeconds = 2;
            
    public int CurrentCount { get;
                
    private set; }
            
    public string UserID { getset; }
            
    private DateTime LastResetTime;
            
    public bool AddCount()
            {
                DateTime now 
    = DateTime.Now;
                
    if ((now - LastResetTime).Seconds >= ResetSeconds)
                {
                    
    this.CurrentCount = 0;
                    
    this.LastResetTime = now;
                }
           this.CurrentCount++;
                if (this.CurrentCount <= MaxRequestCount)
                    
    return true;
                
    else
                    
    return false;
            }
            
    public static RequestCounter GetCounter(string userID)
            {
                
    if (Counters.ContainsKey(userID))
                    
    return Counters[userID];
                
    else
                {
                    Counters[userID] 
    = new RequestCounter();
                    
    return Counters[userID];
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
        
    public partial class _Default : System.Web.UI.Page
        {
            
    protected void Page_Load(object sender, EventArgs e)
            {
                RequestCounter counter 
    = RequestCounter.GetCounter("xhan");
                
    bool canAccess = counter.AddCount();

                Response.Write(canAccess);
                Response.Write(counter.CurrentCount);

            }
        }
    }


  • 相关阅读:
    定时器
    javascript之循环保存数值
    Web 前端之HTML和CSS
    [讲解]容斥原理
    [vijos1048]送给圣诞夜的贺卡<DFS剪枝>
    [vijos1145]小胖吃巧克力<概率dp>
    [noip2012]国王游戏<贪心+高精度>
    [codevs3118]高精度除法<高精度>
    [noip2016]组合数问题<dp+杨辉三角>
    [codevs2370]小机房的树<LCA>
  • 原文地址:https://www.cnblogs.com/xhan/p/1621293.html
Copyright © 2011-2022 走看看