Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis chat room anti-brush screen

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This project will limit how often the same nickname can send exactly the same information. The interval between two identical messages is not less than 120 seconds.

1. Understand the principle of limiting screen brushing

Redis naturally has the ability to achieve this function. In Redis, Key can set the expiration time. When the time is up

Red is automatically deletes the Key.

What if you set a string? Key is: "nickname-speech content", such as "Qingnan-I am watering", and then set the expiration time of this Key to 120 seconds. The value of this string doesn't matter. You can set whatever you want. So: if you want the Key in Redis, the user nickname has already sent this message within 120 seconds: if the Key is not in Redis, the user has never sent this message, or the Key has been automatically deleted by Redis after it has been sent for more than two minutes.

Moreover, Redis can use the "hour" command to query how many seconds a Key has left to expire, which can also implement the reminder function.

two。 The method of limiting screen brushing

The method to set the expiration time of Key is set_nick_msg_expire_time (), and modify the code as follows:

The main code is as follows:

Line 13: first convert the information to MD5. The advantage of this is to shorten the length of the information and avoid being so long that the export exceeds the limit of Redis Key.

Line 14: combine the user's nickname and the MD5 value of the message into a long string as a Key.

Line 15: set a string in Redis, Key is "nickname + MD5 value of message", and the value is 1; set the expiration time to 120 through the ex parameter, and the Key will be deleted as soon as the expiration time reaches Redis

The modified code is shown in figure 6-20.

The remaining time for reading the screen brushing limit

When users want to send new content, the website first checks whether Red is has the Key of "nickname + new information MD 5 value".

If so, the user sent the same content within 120 seconds. At this point, return to the remaining time for lifting the screen brushing restriction.

If not, None is returned.

1. The method of setting query to limit the time of brushing the screen

The corresponding method is get_ nick_ msg_ expire_ time (). The improved code is as follows:

Among them, the main code is described below.

Line 16: get the MD5 value of the message.

Line 17: combine the nickname with the MD5 value of the message into a Key.

Line 18: use the hours command of Redis to check the remaining time of Key. Returns None if Key does not exist,-1 if Key has no expiration time, and remaining time (positive integer) if Key has expiration time.

The modified code is shown in figure 6-21.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report