本笔记基于bilibili尚硅谷Redis学习视频整理而来
Redis 与 Jedis测试
Jedis所需要的jar包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.0</version>
</dependency>
连接Redis注意事项
- 禁用Linux的防火墙:Linux(CentOS7)里执行命令
systemctl stop/disable firewalld.service
- redis.conf中注释掉bind 127.0.0.1,修改 protected-mode 为 no
Jedis常用操作
创建动态的工程
选择Maven,创建对应的项目,然后引入依赖包即可。
创建测试程序
public class test {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
String value = jedis.ping();
System.out.println(value);
jedis.close();
}
}
测试相关数据类型
Jedis-API: Key
在类中编写如下测试方法:
@Test
public void demo1() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
jedis.set("k1", "v1");
jedis.set("k2", "v2");
jedis.set("k3", "v3");
Set<String> keys = jedis.keys("*");
System.out.println(keys.size());
for (String key : keys) {
System.out.println(key);
}
System.out.println(jedis.exists("k1"));
System.out.println(jedis.ttl("k1"));
System.out.println(jedis.get("k1"));
jedis.close();
}
运行即可看到信息:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kHRXOGE0-1648649787248)(https://cdn.jsdelivr.net/gh/senluoye/BadGallery@main/image/202203271952667.png)]
Jedis-API: String
@Test
public void demo() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
jedis.close();
}
结果如下:
Jedis-API: List
@Test
public void demo() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
jedis.lpush("str","str2","str1","str3","str4");
List<String> list = jedis.lrange("str", 0, -1);
for (String str : list) {
System.out.println(str);
}
jedis.close();
}
结果:
Jedis-API: set
@Test
public void demo() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
jedis.sadd("orders", "order01");
jedis.sadd("orders", "order02");
jedis.sadd("orders", "order03");
Set<String> members = jedis.smembers("orders");
System.out.println(members);
jedis.srem("orders", "order02");
Set<String> members2 = jedis.smembers("orders");
System.out.println(members2);
jedis.close();
}
结果如下:
Jedis-API: hash
@Test
public void demo() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
// 插入普通String
jedis.hset("hash1","userName","lisi");
System.out.println(jedis.hget("hash1","userName"));
// 插入Map
Map<String,String> map = new HashMap<>();
map.put("telephone","13810169999");
map.put("address","anywhere");
map.put("email","abc@163.com");
jedis.hmset("hash2",map);
List<String> result = jedis.hmget("hash2", "telephone","email");
for (String element : result) {
System.out.println(element);
}
jedis.close();
}
结果:
Jedis-API: zset
@Test
public void demo() {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
jedis.zadd("zset01", 100d, "z3");
jedis.zadd("zset01", 90d, "l4");
jedis.zadd("zset01", 80d, "w5");
jedis.zadd("zset01", 70d, "z6");
List<String> zrange = jedis.zrange("zset01", 0, -1);
System.out.println(zrange);
jedis.close();
}
结果如下:
Redis_Jedis_实例
完成一个手机验证码功能
要求:
1、输入手机号,点击发送后随机生成6位数字码,2分钟有效
2、输入验证码,点击验证,返回成功或失败
3、每个手机号每天只能输入3次
实现
往之前的Maven项目中天下Lombok依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
下面是本实例的实现代码:
@Data
public class RedisApp {
private Jedis jedis;
private String countKey;
private String codeKey;
private String code;
public static void main(String[] args) {
RedisApp redisApp = new RedisApp();
redisApp.setJedis(new Jedis("127.0.0.1", 6379));
redisApp.sentCode("13659000000"); // 生成验证码
String code = redisApp.getCode();
redisApp.verifyCode(code); // 校验
redisApp.getJedis().close();
}
// 生成6位数字验证码
public String generateCode() {
Random random = new Random();
StringBuilder str = new StringBuilder();
for (int i = 0; i < 10; i++) {
int t = random.nextInt(10);
str.append(i);
}
this.code = str.toString();
return this.code;
}
// 发送验证码
public void sentCode(String phone) {
countKey = "Verify" + phone + ":count";
codeKey = "Verify" + phone + ":code";
String count = jedis.get(countKey);
// 如果之前没有发送过,则新设立一个键,过期时间为一天,value为1
if (count == null) jedis.setex(countKey, 24 * 60 * 60, "1");
else if (Integer.parseInt(count) <= 2) jedis.incr(countKey); // 次数加1
else {
System.out.println("今天发送次数已经超过三次");
return;
}
jedis.setex(codeKey, 120, generateCode());
}
// 校验验证码
public void verifyCode(String code) {
String redisCode = this.jedis.get(codeKey);
if (redisCode.equals(code)) System.out.println("验证成功");
else System.out.println("验证失败");
}
}
只发送一次时:
发送超过三次后: