“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

👨🎓 Author: Bug Bacteria

✏️ blog: CSDN, Nuggets, etc

💌 public account: Magic House of the Circle of the Apes

🚫 special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

🙏 Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles 👍. Your likes (collect ⭐️+ pay attention to 👨 port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon 🏃🏻♀️, creation stopped 💕, refueling 🏻

One, foreword

After the last few sessions of Redis teaching, you should now have the following skills:

  • Can independently realize springboot framework integration redis and other related configuration.
  • Ability to independently write test-case and test redis class method instances.
  • Able to operate basic client redis commands independently.
  • Ability to implement redis related business code independently.
  • .

Some friends asked: “Bug bacteria you haven’t talked about how to specifically combine redis to achieve business logic?” I: “ha ha ha ha ha ha, you will not draw inferences from one another!” Me: “Ok ok…”

Then I will combine the specific scene and show you how to combine redis in the business scene. I hope you can listen carefully during my talk.

All right, I’m gonna get started. Please be quiet, listen carefully and take notes.

Second, scene interpretation

Combined with Redis to achieve a mobile phone verification code function.

The mobile verification code must meet the following requirements: Please listen carefully.

  1. The verification code contains six digits.
  2. A mobile phone number can only send captcha three times a day, more than three times refused to send.
  3. The validity period of the verification code is two minutes.
  4. The interval for sending the verification code is one minute.

Three, code implementation

Let’s go step by step according to the business requirements.

1, get randomly get six digits

** @return */ public String getCode() {int code = (int) ((math.random () * 9 + 1) * 100000); return String.valueOf(code); }Copy the code

2. Obtain the verification code

/** * obtain the verification code, Public void verifyCode(String phone) {//1. Define the phone number and verification code. String phoneKey = phone + "_count"; String codeKey = phone + "_code"; // Mediator.get(phoneKey); If (count == null) {// Indicates that this number is applied for the first time. // Insert the phone id. Set (phoneKey, "1", long.valueof (24 * 60 * 60)); } else if (integer.parseInt (count) <= 2) {long keyExpirationTime = redisMediator.getKeyExpirationTime(phoneKey); System.out.println("keyExpirationTime:" + keyExpirationTime); String codeCount = redismediator. get(phoneKey); // count +1 int newCount = integer.parseint (codeCount) +1; redisMediator.set(phoneKey, String.valueOf(newCount), keyExpirationTime); } else {system.out. println(" 3 opportunities used up! Please try again tomorrow "); return; } // Get a random 6-digit verification code String code = this.getCode(); // Mediator.set(codeKey, code, long.Valueof (2 * 60)); // Mediator.set(codeKey, code, long.Valueof (2 * 60)); }Copy the code

3. Verify that the verification code is correct

Public Boolean checkCode(String phone, String code) {String codeKey = phone + "_code"; String redisCode = redisMediator.get(codeKey); if (code.equals(redisCode)) { return true; } return false; }Copy the code

In fact, to be honest, this logic is very simple, the only thing to pay attention to is the number of times to obtain the verification code, nothing else.

And then explain why I’m looking for the expiration time of countKey in the logic 1<=count<=3, right. Why take the extra step? In fact, THIS is to avoid the user in the next application, reset the expiration time, that will cause some users in a long time unable to SMS application, you think about it, right?

If a user makes a second request long after the last one (but countKey hasn’t expired yet), the user’s count is +1. If the expiration date is reset, the user will have to wait 24 hours before applying again, right? It’s not a human operation. So pay attention to that, too.

Homework

I don’t know if you’ve noticed, but the number four requirement for sending a mobile phone verification code, it doesn’t involve that function at all, does it? I left that out for your homework.

So I hope you can finish it well, very simple, according to gourd gourd gourd gourd gourd.

Simply say my personal train of thought: for reference only

Idea 1:

The first method is to use [mobile phone number + fixed string] as a unique interval identifier, as you calculate the number of times the user obtains the verification code that day, and then write the verification code for the first time, and then set the expiration time to one minute. Then determine the first and also to obtain the number of cases to determine the existence of the interval logo and if so, then time two intervals of less than one minute, direct return to prompt the user “twice for verification code interval time is less than a minute, please try again later, or you want to more friendly remind the user what time can again, Then you can find the expiration time of the interval identifier, and then directly join the prompt statement together with the return. If there is no interval mark, it indicates that the interval time meets the requirements and is directly passed.

Idea 2:

Because the expiration time of the verification code is recorded, you can use the expiration time of the verification code as the reference. Since the expiration time of the verification code is two minutes, and the interval of obtaining the verification code time2 is one minute, because of the same time dimension, you can make the difference by comparing 2min-time1 with 1min. If (2min-time1) is greater than 1 minute, the interval between obtaining the verification code is less than 1 minute. If (2min-time1) is greater than 1 minute, the system returns to remind the user that the interval between obtaining the verification code is less than 1 minute. Please try again later. If (2min-time1) is less than 1 minute, the interval meets the requirements.

The above are my two views on this function point, each has its own benefits, starting point in the code, one in Redis. I don’t know which way of thinking you prefer, or if you have more innovative ideas, please leave a comment below, let me know, exchange and learn together, let me know what you think.

. .

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Five, the past popular recommendation

  • Springboot series (14) : Redis Zero-based teaching, you deserve it!
  • Springboot Series (thirteen) : How to project integrated Swagger online interface documentation, will you?
  • Springboot series (12) :How to code to send email reminders, have you written?
  • . .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

☘️ Be who you want to be, there is no time limit, you can start whenever you want,

🍀 You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

💌 If this article is helpful to you, please leave a like! (# ^. ^ #);

💝 if you like the article shared by bug fungus, please give bug fungus a point of concern! The danjun ‘ᴗ, you guys will have a cameo appearance with you.

💗 if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

💞 In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

💕 copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you