Redis 4. Jedis 测试
参考课程:https://www.bilibili.com/video/BV1Rv41177Af
参考书:https://blog.csdn.net/liu8490631/article/details/124290851
0. 引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.1.1</version>
</dependency>
1. 测试
public class jedisDemoTest {
private Jedis jedis;
@BeforeEach
public void initAll() {
jedis = new Jedis("10.1.1.128", 6379);
jedis.auth("secret");
System.out.println("==========建立连接==========");
}
@AfterEach
public void closeAll() {
jedis.close();
System.out.println("==========关闭连接==========");
}
@DisplayName("key")
@Test
public void testKey() {
System.out.println(jedis.keys("*"));
}
@DisplayName("字符串")
@Test
public void test1() {
// System.out.println(jedis.keys("*"));
// jedis.set("name", "nami", new SetParams().ex(10));
// System.out.println("value: " + jedis.get("name"));
// System.out.println("生命剩余时常: " + jedis.ttl("name"));
jedis.mset("k1", "v1", "k2", "v2");
System.out.println(jedis.mget("k1", "k2"));
}
@DisplayName("列表")
@Test
public void test2() {
// 从左添加元素
// jedis.lpush("titanic", "jack", "rose", "mike");
// System.out.println(jedis.lrange("titanic", 0, -1));
// jedis.rpush("sanguo", "刘备", "关羽", "张飞");
jedis.rpush("sanguo", "孙权", "周瑜", "黄盖");
System.out.println(jedis.lrange("sanguo", 0, -1));
}
@DisplayName("集合")
@Test
public void test3() {
jedis.sadd("xiyouji", "孙悟空", "唐僧");
jedis.srem("xiyouji", "沙僧");
System.out.println(jedis.smembers("xiyouji"));
}
@DisplayName("哈希")
@Test
public void test4() {
// jedis.hset("users", "xz", "I'm xz");
Map<String, String> map = new HashMap<>();
map.put("username1", "password1");
map.put("username2", "password2");
map.put("username3", "password3");
map.put("username4", "password4");
jedis.hmset("users", map);
System.out.println(jedis.hmget("users", "username1", "username2", "121221"));
}
@DisplayName("有序集合")
@Test
public void test5() {
// jedis.zadd("china", 100, "shanghai");
// jedis.zadd("china", 200, "beijing");
System.out.println(jedis.zrange("china", 0, -1));
System.out.println(jedis.zrevrangeWithScores("china", 0, -1));
}
}
2. 实现手机验证码
要求:
- 输入手机号,点击发送后随机生成 6 位数字码,2 分钟有效
- 输入验证码,点击验证,返回成功或者失败
- 每个手机号每天只能输入 3 次
public class SendVerifyCode {
public static void main(String[] args) {
// 发送验证码
// sendCode("15670007000");
// 验证
System.out.println(verifyRedisCode("15670007000", "214449"));
}
public static boolean verifyRedisCode(String phoneNumber, String code) {
Jedis jedis = new Jedis("10.1.1.128", 6379);
jedis.auth("secret");
String redisCode = jedis.get("VerifyCode" + phoneNumber + ":code");
jedis.close();
return code.equals(redisCode);
}
/**
* @param phoneNumber 手机号码
* 每个手机每天只能发送三次, 验证码放到 Redis 中, 设置过期时间
*/
public static void sendCode(String phoneNumber) {
Jedis jedis = new Jedis("10.1.1.128", 6379);
jedis.auth("secret");
// 拼接 key, 规则自定
// 手机发送次数 key
String countKey = "VerifyCode" + phoneNumber + ":count";
// 每个手机每天只能发送三次
String count = jedis.get(countKey);
// 没有发送次数
if (count == null) {
// 设置发送次数 3
jedis.setex(countKey, 24*60*60, "2");
} else if (Integer.parseInt(count) > 0) {
// 发送次数 - 1
jedis.decr(countKey);
} else {
System.out.println("今天发送次数已超过 3 次, 不能再发送!");
jedis.close();
return;
}
// 手机验证码 key
String codeKey = "VerifyCode" + phoneNumber + ":code";
String code = genCode();
jedis.setex(codeKey, 120, code);
jedis.close();
}
/**
* @return 生成 6 位随机数字的验证码
*/
public static String genCode() {
Random random = new Random();
StringBuilder code = new StringBuilder();
for (int i = 0; i < 6; i++) {
code.append(random.nextInt(10));
}
return code.toString();
}
}