Site IP Access frequency limit for a single site
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;//using System.Linq;usingsystem.web;//<summary>//IP Access Frequency control//</summary> Public Static classipcachemanager{/// <summary> ///IP Cache Collection/// </summary> Private StaticList<ipcacheinfo> dataList =NewList<ipcacheinfo>(); Private Static ObjectLockobj =New Object(); /// <summary> ///the maximum number of requests must be greater than or equal to 1 over time/// </summary> Private Static intMaxtimes =3; /// <summary> ///length of time (in seconds), must be greater than or equal to 1/// </summary> Private Static intPartsecond = -; /// <summary> ///request denied whether to join request number/// </summary> Private Static BOOLIsfailaddin =false; StaticIpcachemanager () {}/// <summary> ///set time, default maxtimes=3, partsecond=30/// </summary> /// <param name= "_maxtimes" >Maximum number of requests</param> /// <param name= "_partsecond" >Request Unit Time</param> Public Static voidSetTime (int_maxtimes,int_partsecond) {Maxtimes=_maxtimes; Partsecond=_partsecond; } /// <summary> ///detects whether the number of IP requests can continue to be requested over a period of time///and use/// </summary> /// <param name= "IP" ></param> /// <returns></returns> Public Static BOOLCheckisable (stringIP) { Lock(lockobj) {varitem = Datalist.find (p = = P.ip = =IP); if(Item = =NULL) {Item=NewIpcacheinfo (); Item. IP=IP; Item. Reqtime.add (DateTime.Now); Datalist.add (item); return true; } Else { if(item. Reqtime.count >maxtimes) {Item. Reqtime.removeat (0); } varNowtime =DateTime.Now; if(Isfailaddin) {#regionRequest denied also need to join the requestitem. 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 { #regionIf the request is denied, you don't need to join the request.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 classipcacheinfo{ Public stringIP {Get;Set; } PrivateList<datetime> Reqtime =NewList<datetime>(); PublicList<datetime>Reqtime {Get{return This. Reqtime;} Set{ This. Reqtime =value;} }}
C # site IP access frequency limit for a single site