目录
介绍:
jedis类似jdbc,在客户端下java代码实现redis操作;
1、new一个Jedis实例(ip地址,端口号);
2、认证密码
一些问题
记得在redis.conf里面把默认访问端口给注释,还有那个保护机制设置为no,防火墙需要放行6379端口;
java代码实现Redis操作
package com.atguigu.jedis;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Set;
/**
* @author diao 2022/3/9
*/
public class jedisDemo1 {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
String ping = jedis.ping();
System.out.println(ping);
}
@Test
public void demo1(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
//添加
jedis.set("name","Fairy");
//获取
String name = jedis.get("name");
System.out.println("键:"+name);
//看key还有多长时间过期
Long ttl = jedis.ttl(name);
System.out.println("还有"+ttl+"就过期");
//看键是否存在
Boolean exists = jedis.exists(name);
System.out.println(name+"存在状态:"+exists);
//设置过期时间
// jedis.expire("name",0);
//设置多个key,并且获取
String mset = jedis.mset("k1", "v1", "k2", "v2");
System.out.println(jedis.mget("v1","v2"));
System.out.println("------------------------");
//得到所有的键
Set<String> keys = jedis.keys("*");
for (String key : keys) {
System.out.println(key);
}
}
/*操作list
* */
@Test
public void demo2(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
jedis.lpush("key1","lucy","merry","mike");
List<String> list = jedis.lrange("key1", 0, -1);
System.out.println("key1的值为:"+list);
}
/*操作set集合
* */
@Test
public void demo3(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
//set集合有去重效果(key不能重)
jedis.sadd("addr","beijing","changsha","shenzhen");
Set<String> addr = jedis.smembers("addr");
//删除键中某值
jedis.srem("addr","shenzhen");
System.out.println("set集合中键addr的值"+addr);
}
/*操作hash
* */
@Test
public void demo4(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
//键中的变量——>还有值:就跟哈希表一样,哈希表对应着键,表中链对应变量
jedis.hset("school","gaozhong","changjun");
String schools = jedis.hget("school", "gaozhong");
System.out.println("值为:"+schools);
}
/*操作zset
* */
@Test
public void demo5(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
jedis.zadd("china",100d,"shanghai");
jedis.zadd("china",200d,"changsha");
//zset会按照score的值进行排序,并且具有去重效果
Set<String> address = jedis.zrange("china", 0, -1);
System.out.println(address);
}
//操作字符串
@Test
public void demo6(){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
jedis.setnx("str","1");
Long str = jedis.incr("str");
Long str1 = jedis.decr("str");
System.out.println("最后字符串长度为:"+str1);
}
}
案例:手机验证码
2、首先是设置key:一个用户次数的key,里面是用key对应的值,也就是请求验证码的次数,我们用setex()设置用户的值以及过期时间;
然后进行判断,如果小于3次,发送次数+1,利用incr(对应key),如果是第一次就设置用户次数的键值对以及过期时间
然后得到验证码,将其放到缓存中,并且设置缓存时间
3、验证码校验:
从缓存中取出验证码,利用get(对应的键)得到对应缓存中的验证码,然后与用户输入的验证码对比即可;
package com.atguigu.jedis;
import redis.clients.jedis.Jedis;
import java.util.Random;
/**
* @author diao 2022/3/9
*/
public class PhoneCode {
public static void main(String[] args) {
// verityCode("18175143063");
getRedisCode("18175143063","696349");
}
// 1.生成6位数字验证码
public static String getCode(){
Random random = new Random();
String code="";
for(int i=0;i<6;i++){
//随机生成的数字为10以内
int rand = random.nextInt(10);
//每次拼接->生成验证码
code+=rand;
}
return code;
}
// 2.手机每天只能发送三次,验证码放到redis中,设置过期时间
public static void verityCode(String phone){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
//key:手机发送次数、验证码key
String countKey="VerifyCode"+phone+":count";
String codeKey="VerifyCode"+phone+":code";
String count = jedis.get(countKey);
//对发送次数的键值进行验证
if(count==null){
//第一次发送,setex设置过期时间(当前时间下的24小时只能有三次机会),若要0点刷新,还要时间戳
jedis.setex(countKey,24*60*60,"1");
}else if(Integer.parseInt(count)<=2){
//发送次数+1
jedis.incr(countKey);
}else if(Integer.parseInt(count)>2){
System.out.println("今天发送次数已经超过三次");
jedis.close();
return;//次数超过三次了,后面不能发送验证码了
}
//发送验证码到redis里面
String ycode = getCode();
jedis.setex(codeKey,120,ycode);//给键赋值,value为随机验证码,存活时间为2分钟
jedis.close();
}
//3.验证码校验
public static void getRedisCode(String phone,String code){
Jedis jedis = new Jedis("192.168.184.131", 6379);
jedis.auth("123456");
String countKey="VerifyCode"+phone+":count";
String codeKey="VerifyCode"+phone+":code";
//提取验证码
String ycode = jedis.get(codeKey);
//判断
if(code==null){
System.out.println("请输入验证码");
}else if(ycode.equals(code)){
System.out.println("成功");
}else if(!ycode.equals(code)){
System.out.println("失败");
}else if(ycode==null){
System.out.println ("完了,验证码发送失败");
}
jedis.close();
}
}