赞
踩
import redis.clients.jedis.Jedis; import java.util.Random; public class RedisConfig { static String code = null; public static void main(String[] args) { //1,先判断该手机验证次数几次了,有没有超时间 //2,然后生成随机码 //3,再判断填写的随机码是否和生成的随机码相同。 verifyPhone("12345678912"); } public static void verifyPhone(String phone){ Jedis jedis = new Jedis("192.168.110.110", 6379); String keyPhone = "verify" + phone + "count"; String codePhone = phone + "code"; String s = jedis.get(keyPhone); //第一次,没有key的记录,所以需要判断null if(s == null){ jedis.setex(keyPhone, 24*60*60, "1"); }else if(Integer.valueOf(s) < 3){ //第二,三次,有记录了 jedis.incr(keyPhone); }else { System.out.println("今天已经超过三次"); jedis.close(); return; } //如果还在三次之内可以,可以执行这个获取值的操作 code = getRandomCode(); jedis.setex(codePhone, 60, code); jedis.close(); } public static String getRandomCode(){ Random random = new Random(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < 6; ++i){ int tmp = random.nextInt(10); builder.append(tmp); } return builder.toString(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。