Web system access Frequency limits

Source: Internet
Author: User

Whether it's spring MVC or struts, you can add interceptors before the controller or Aciton executes.

With the logic control in the interceptor, the access frequency limit can be achieved.

First construct the access frequency data class

classFrequencydata {//using Ip_methodnameString Key; //Record start time    LongStartTime; //Record End Time    LongEndTime; //Access Frequency limit length of time    intTime ; //frequency of access limit times    intlimit; //record access point in timeList<long> accesspoints =NewArraylist<long>();  Public voidResetLongTime ) {StartTime= EndTime =Time ;        Accesspoints.clear ();    Accesspoints.add (time); }}

In the Spring MVC interceptor, you can get the method name to intercept and get the IP address of the client via request.

@Override  Public Boolean throws Exception {    (Handlermethod) handler). GetMethod (). GetName ();    ((Handlermethod) handler). Getbean (). GetClass (). GetName ();
Request.getremoteaddr () ...

Create a map data in the Interceptor class that holds each client access record

Private Static Map<string, frequencydata>    freqdatas            new hashmap<string, frequencydata> (max_size);

The access frequency limit execution process is as follows:

1, calculates the key value, namely is Ip_methodname, obtains the corresponding frequencydata.

2, if the Frequencydata data does not exist, new and saved to Freqdatas.

3. The time and limit in the Frequencydata data can be fixed to write dead, can also be obtained by annotations (add custom annotations on the called method, specify two parameters in the note).

4, add a record in the accesspoints of Frequencydata data, and update Endtime time.

5. Calculate whether the endtime-starttime is greater than the time value. If it is greater than, perform frequencydata reset ().

6, calculates whether accesspoints.size () is greater than the limit value. If it is less than, continue to execute the calling method, or , if it is greater than, do not proceed with the call .

At this point, a simple access frequency control function can be completed.

Web system access Frequency limits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.