欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C# 站點(diǎn)IP訪問(wèn)頻率限制 針對(duì)單個(gè)站點(diǎn)的實(shí)現(xiàn)方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

 站點(diǎn)IP訪問(wèn)頻率限制 針對(duì)單個(gè)站點(diǎn)

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

// <summary>
// IP訪問(wèn)頻率控制
// </summary> 
public static class IPCacheManager
{

  /// <summary>
  /// IP緩存集合 
  /// </summary> 
  private static List<IPCacheInfo> dataList = new List<IPCacheInfo>();
  private static object lockObj = new object();

  /// <summary> 
  /// 一段時(shí)間內(nèi),最大請(qǐng)求次數(shù),必須大于等于1 
  /// </summary> 
  private static int maxTimes = 3;

  /// <summary> 
  /// 一段時(shí)間長(zhǎng)度(單位秒),必須大于等于1 
  /// </summary> 
  private static int partSecond = 30;

  /// <summary> 
  /// 請(qǐng)求被拒絕是否加入請(qǐng)求次數(shù) 
  /// </summary> 
  private static bool isFailAddIn = false;

  static IPCacheManager()
  {
  }

  /// <summary> 
  /// 設(shè)置時(shí)間,默認(rèn)maxTimes=3, partSecond=30 
  /// </summary> 
  /// <param name="_maxTimes">最大請(qǐng)求次數(shù)</param> 
  /// <param name="_partSecond">請(qǐng)求單位時(shí)間</param> 
  public static void SetTime(int _maxTimes, int _partSecond)
  {
    maxTimes = _maxTimes;
    partSecond = _partSecond;
  }

  /// <summary> 
  /// 檢測(cè)一段時(shí)間內(nèi),IP的請(qǐng)求次數(shù)是否可以繼續(xù)請(qǐng)求 
  /// 和使用 
  /// </summary> 
  /// <param name="ip"></param> 
  /// <returns></returns> 
  public static bool CheckIsAble(string ip)
  {
    lock (lockObj)
    {
      var item = dataList.Find(p => p.IP == ip);
      if (item == null)
      {
        item = new IPCacheInfo();
        item.IP = ip;
        item.ReqTime.Add(DateTime.Now);
        dataList.Add(item);

        return true;
      }
      else
      {
        if (item.ReqTime.Count > maxTimes)
        {
          item.ReqTime.RemoveAt(0);
        }

        var nowTime = DateTime.Now;
        if (isFailAddIn)
        {
          #region 請(qǐng)求被拒絕也需要加入當(dāng)次請(qǐng)求
          item.ReqTime.Add(nowTime);
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              return true;
            }
          }
          else
          {
            return true;
          }
          #endregion
        }
        else
        {
          #region 請(qǐng)求被拒絕就不需要加入當(dāng)次請(qǐng)求了
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              item.ReqTime.Add(nowTime);
              return true;
            }
          }
          else
          {
            item.ReqTime.Add(nowTime);
            return true;
          }
          #endregion
        }
      }
    }
  }
}

public class IPCacheInfo
{
  public string IP { get; set; }

  private List<DateTime> reqTime = new List<DateTime>();
  public List<DateTime> ReqTime
  {
    get { return this.reqTime; }
    set { this.reqTime = value; }
  }
}

以上這篇C# 站點(diǎn)IP訪問(wèn)頻率限制 針對(duì)單個(gè)站點(diǎn)的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有