当前位置:网站首页>Jedis operation redis
Jedis operation redis
2022-07-23 08:14:00 【Annoying dog broke into your blog】
Jedis operation Redis
List of articles
1. Introduce dependencies
Here we use 3.2.0 Version as an example :
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
2. Jedis Basic operation
2.1 Test connection
If Redis Install in ECs , Pay attention to configuring the protective wall security group of the server .
public static void main(String[] args) {
Jedis jedis = new Jedis(" Yours redis Of the host server IP Address ", 6379);
// If your Redis Password set , Perform this step
jedis.auth(" Yours redis password ");
String ping = jedis.ping();
System.out.println(ping);
jedis.close();
}
Console printing :
PONG
Description connection successful .
2.2 operation Redis
Jedis It encapsulates a lot of API It can be used for Redis To operate , Next is the operation demonstration .
operation Redis The relevant commands of Jedis There are corresponding methods in .
- operation String type
@Test
public void testString() {
String set = jedis.set("key1", "v1");
System.out.println(set);
String key1 = jedis.get("key1");
System.out.println(key1);
jedis.close();
}
Console printing :
OK
v1
- operation List list
@Test
public void testList() {
jedis.lpush("list1", "v1", "v2", "v3");
List<String> list = jedis.lrange("list1", 0, -1);
list.forEach(System.out::println);
jedis.close();
}
Console printing :
v3
v2
v1
- operation Set aggregate
@Test
public void testSet() {
Long sadd = jedis.sadd("set1", "tom", "jerry", "jack");
Set<String> set = jedis.smembers("set1");
System.out.println(set);
jedis.close();
}
Console printing :
[tom, jack, jerry]
- operation Hash
@Test
public void testHash() {
Map<String, String> map = new HashMap<>();
map.put("id", "1");
map.put("name", "Tom");
map.put("age", "18");
jedis.hset("map1", map);
String id = jedis.hget("map1", "id");
String name = jedis.hget("map1", "name");
String age = jedis.hget("map1", "age");
System.out.println("id = " + id);
System.out.println("name = " + name);
System.out.println("age = " + age);
jedis.close();
}
Console printing :
id = 1
name = Tom
age = 18
- operation ZSet Ordered set
@Test
public void testZSet() {
jedis.zadd("zset1", 100, "java");
jedis.zadd("zset1", 50, "c++");
jedis.zadd("zset1", 25, "php");
Set<String> set = jedis.zrange("zset1", 0, -1);
System.out.println(set);
jedis.close();
}
Console printing :
[php, c++, java]
3. Analog verification code case
Business requirements :
1、 Enter phone number , Click send and randomly generate 6 Digit code ,2 Minutes are valid
2、 Enter verification code , Click verify , Return to success or failure
3、 Each mobile phone number can only be entered every day 3 Time
Code implementation :
package com.jc;
import redis.clients.jedis.Jedis;
import java.util.UUID;
public class PhoneCodeTest {
private static Jedis jedis = new Jedis("180.76.116.222", 6379);
private static final int TIMEOUT_CODE = 60 * 2;
private static final int TIMEOUT_PHONE = 60 * 60 * 24;
static {
jedis.auth("576a6aWjj");
}
// Generate verification code randomly
public static String getCode() {
String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
return code;
}
// Send verification code
public static void verifyCode(String phone, String code) {
String countKey = "VerifyCode" + phone + ":count";
String codeKey = "VerifyCode" + phone + ":code";
String count = jedis.get(countKey);
if (count == null) {
jedis.setex(countKey, TIMEOUT_PHONE, "1");
} else if (Long.parseLong(count) < 3) {
jedis.incr(countKey);
} else {
System.out.println(" It has been sent today 3 Secondary verification code !");
jedis.close();
return;
}
// Set the verification code
jedis.setex(codeKey, TIMEOUT_CODE, code);
}
public static void checkVerifyCode(String phone, String code) {
String countKey = "VerifyCode" + phone + ":code";
String redisCode = jedis.get(countKey);
if (code.equals(redisCode)) {
System.out.println(" success !");
} else {
System.out.println(" Failure !");
}
}
public static void main(String[] args) {
verifyCode("123123123", getCode());
// checkVerifyCode("123123123", "b9dd1c");
jedis.close();
}
}
边栏推荐
- Organizational structure of agile testing team
- matlab声音信号处理 频率图 信号过滤和播放声音
- LC:剑指 Offer 39. 数组中出现次数超过一半的数字
- 挖财和启牛都是干什么的开户安全吗?
- Google Earth engine app - a complete map legend app (land use classification of western United States)
- The boss asked me to do an IP territorial function and an open source library!
- Dispersion tensor analysis open source software DSI studio simplified Chinese version can be downloaded
- 大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
- matlab simulink 磷酸铁锂电池仿真
- Wechat applet project practice
猜你喜欢
随机推荐
Typescript对象扩展之对象原型__proto__与prototype
Web资源共享
Tensorrt plug-in practice (1)
This is not a true sense of the meta universe, which should have its own distinctive characteristics and unique development logic
buu web
这不是真正意义上的元宇宙,元宇宙应当具备自身鲜明的特质和独特的发展逻辑
postgresql数据库主从部署 主库挂了重新还原主库
There are 13 detailed methods for JMeter to view the response of the result tree!
云计算或成时代新拐点?从哪些点可以看出?
EMQX v4.4.5 发布:新增排他订阅及 MQTT 5.0 发布属性支持
Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction
如何高效安装MindSpore的GPU版本
uni-app进阶之内嵌应用【day14】
RequestContextHolder
Easily take you to the gate of turtle drawing
目标检测之锚点与锚框
Codeforces round 809 (Div. 2) (Questions C and D1)
技术干货 | 数据处理好难?来看MindSpore提供的解决思路!
直播预告 | 开源安全治理模型和工具直播研讨会
JS regular delete the span tag and the content in the tag









