规定一个手机号一天只能请求三次验证码,且每次请求的验证码只有一分钟就会过期
package com.doit.demo;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Random;
import java.util.Scanner;
public class PhoneNum {
public static void main(String[] args) {
JedisPool pool = new JedisPool("linux01",6379);
Jedis jedis = pool.getResource();
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的手机号码:");
String phone = sc.nextLine();
String code = getCode();
System.out.println("您的验证码是:"+code);
jedis.setex("number",60,code);
String regex = "^((13[0-9])|(14[5,6,7,9])|(15[^4])|(16[5,6])|(17[0-9])|(18[0-9])|(19[1,8,9]))\\d{8}$";
int count =3;
while(count>=1){
if(jedis.get("number")==null){
System.out.println("您的验证码已失效");
break;
}
else if (phone.matches(regex)){
String num= sc.nextLine();
if (num.equals(jedis.get("number"))){
System.out.println("恭喜您,登陆成功");
break;
}else{
count--;
if (count==0){
System.out.println("您的机会已用完");
}
System.out.println("验证码错误,您还有 "+count+" 次机会");
}
}else{
System.out.println("手机号格式输入错误");
break;
}
}
}
public static String getCode(){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
int i1 = new Random().nextInt(10);
sb.append(i1);
}
return sb.toString();
}
}
模拟任务队列
需求:任务调度系统:生产者不断产生任务,放入task-queue排队
消费者不断拿出任务来处理,同时放入一个tmp-queue暂存,如果任务处理成功,则清除tmp-queue,否则,将任务弹回task-queue
Producer
package com.doit.demo;
import redis.clients.jedis.Jedis;
import java.util.Random;
import java.util.UUID;
public class Producer {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("linux01", 6379);
while(true){
UUID uuid = UUID.randomUUID();
System.out.println("任务..........."+uuid);
jedis.lpush("task",uuid.toString());
Thread.sleep(2000);
if (jedis.llen("task")==10){
Thread.sleep(6000);
}
}
}
}
Consumer
package com.doit.demo;
import redis.clients.jedis.Jedis;
import java.util.Random;
public class Consumer {
public static void main(String[] args) throws InterruptedException {
Jedis jedis = new Jedis("linux01", 6379);
while(true){
jedis.rpoplpush("task","hcq");
int num = new Random().nextInt(10000);
String data = jedis.rpop("hcq");
if (num%3==0){
System.out.println("该任务失败,重新进入缓存区...."+data);
jedis.lpush("hcq",data);
}
else{
System.out.println("读取成功"+data);
}
Thread.sleep(2000);
}
}
}