Redis学习笔记六(Jedis操作)

通过Jedis连接redis

1. 引入依赖,测试是否成功

  1. 创建maven新项目,并打开pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
1. 如果出现包没有找到的问题,对前面笔记提到的 [maven插件配置](https://www.cnblogs.com/sleepyheadLK/p/16375963.html)
2. 重新更新pom.xml即可引入依赖
  1. 进行测试
@Test
public void test(){
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
String ping = jedis.ping();
System.out.println(ping);
}
  1. 如果出现以下错误
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
  1. pom.xml引入
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.36</version>
</dependency>
  1. 更新pom.xml

  2. 如果是远程连接失败

    1. redis.conf文件中的保护设置是否关闭
    2. 系统的防火墙是否关闭

2. 操作key

@Test
public void test1(){
//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
//操作key
Set<String> keys = jedis.keys("*");
keys.forEach(i-> System.out.println(i));
//添加
jedis.set("name","lucy");
//获取
String name = jedis.get("name");
System.out.println(name);
//是否存在
System.out.println(jedis.exists("name"));
//批量操作
jedis.mset("name","jack","name2","rose");
}

3. 操作数据类型

//创建Jedis对象
Jedis jedis = new Jedis("127.0.0.1", 6379);
// //操作list
// jedis.lpush("list1","v1","v2","v3");
// List<String> list1 = jedis.lrange("list1", 0, -1);
// System.out.println(list1);
// //操作set
// jedis.sadd("set2","v1","v2","v3");
// Set<String> set2 = jedis.smembers("set2");
// System.out.println(set2);
// //操作hash
// jedis.hset("hash1",new HashMap<String,String>(){
// {
// put("name","jack");
// put("age","12");
// }
// });
// //获取name对应的值
// System.out.println(jedis.hget("hash1","name"));
// //获取所有的key
// System.out.println(jedis.hkeys("hash1"));
//操作zset
jedis.zadd("zset1", new HashMap<String,Double>(){
{
put("jack",12.0);
put("rose",23.0);
put("boy",7d);
}
});
System.out.println(jedis.zrange("zset1",0,-1));
jedis.close();
}

4. 验证码测试

  1. 要求:
    image
  2. 功能设计
//发送
@Test
public void test(){
verifyCode("112332");
}
//验证
@Test
public void test2(){
getRedisCode("112332","374848");
}
public static String getCode(){
Random random = new Random();
return String.format("%06d",random.nextInt(1000000));
}
//
public static void verifyCode(String phone){
Jedis jedis = new Jedis("127.0.0.1", 6379);
String countKey = "VerifyCode"+phone+":count";
String codeKey = "VerifyCode"+phone+":code";
//能否取到
String s = jedis.get(countKey);
//发送验证码
if(s==null){
jedis.setex(countKey,24*60*60,"1");
}else if(Integer.valueOf(s)<3){
jedis.incr(countKey);
}else{
System.out.println("超出发送次数");
jedis.close();
return;
}
//发送验证码
String vcode = getCode();
jedis.setex(codeKey,120,vcode);
jedis.close();
}
public static void getRedisCode(String phone,String code){
Jedis jedis = new Jedis("127.0.0.1", 6379);
String codeKey = "VerifyCode"+phone+":code";
String s = jedis.get(codeKey);
if(code.equals(s)){
System.out.println("成功");
}else{
System.out.println("失败");
}
jedis.close();
}
posted @   小懒虫LK  阅读(365)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示