Jedis操作

1、准备工作

在IDEA中创建一个maven工程,加入需要的依赖


    redis.clients
    jedis
    3.2.0

要操作Jedis首先得链接上Linux虚拟机上,查看Linux主机号host,以及redis的端口号port,如何查看Linux上的主机host呢?

ye@ubuntu:~/Desktop$ ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.42.128  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::9632:f2af:11e8:fb30  prefixlen 64  scopeid 0x20
        ether 00:0c:29:9c:73:2b  txqueuelen 1000  (Ethernet)
        RX packets 4401  bytes 5930620 (5.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2349  bytes 177744 (177.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 436  bytes 50726 (50.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 436  bytes 50726 (50.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ye@ubuntu:~/Desktop$ 

其中inet 192.168.42.128就是host号,而redis的端口号默认是6379.

public class RedisTest {
    public static void main(String[] arge){
        Jedis jedis = new Jedis("192.168.42.128",6379);
        String ping = jedis.ping();
        System.out.println(ping);
    }
}

如果出现burg,需要注意两点:(保证你在Linux上打开了redis-server /opt/redis.conf)

1、禁用Linux的防火墙  systemctl stop/disable firewalld.service

2、找到redis.conf文件,并在其中注释掉bind 127.0.0.1 ,然后 protected-mode no (之前是yes,修改成no)

没出现错误:上面的测试就会输出:PONG,现在好了测试搞好可以开始对Jedis操作了。

2、相关数据类型测试

      1)Key

public class RedisTest {
    public static void main(String[] arge){
        Jedis jedis = new Jedis("192.168.42.128",6379);
        jedis.set("k1","value1");
        jedis.set("k2","value2");
        jedis.set("k3","value3");
        Set keys = jedis.keys("*");
        for(String key:keys){
            System.out.println(key);
        }
        System.out.println(jedis.exists("k1"));
        System.out.println(jedis.ttl("k1"));//-1 永久
        System.out.println(jedis.get("k1"));
        jedis.close();
    }
}

      2)String

public class RedisTest {
    public static void main(String[] arge){
        Jedis jedis = new Jedis("192.168.42.128",6379);
        jedis.mset("k4","123","k5","456","k6","789");
        System.out.println(jedis.get("k5"));//456
        jedis.close();
    }
}

      3)List

public class RedisTest {
    public static void main(String[] arge){
        Jedis jedis = new Jedis("192.168.42.128",6379);
        //List可以元素重复
        jedis.lpush("key1","123");
        jedis.rpush("key1","123");
        jedis.rpush("key1","456");
        List list = jedis.lrange("key1", 0, -1);
        for(String li:list){
            System.out.println(li);
        }
        jedis.lrem("key1",2,"123");
        System.out.println("===");
        for(String li:list){
            System.out.println(li);
        }
        jedis.close();
    }
}

      4)set

@Test
public void test04(){
    //set集合
    Jedis jedis = new Jedis("192.168.42.128",6379);
    jedis.sadd("Myset","C++","java","python");
    //获取所有
    Set myset = jedis.smembers("Myset");
    for (String s : myset) {
        System.out.println(s);
    }
    jedis.close();
}

      5)hash

@Test
public void test02(){
    //hash类型的操作
    Jedis jedis = new Jedis("192.168.42.128",6379);
    jedis.hset("myhash","name","wangwu");
    jedis.hset("myhash","age","21");
    jedis.hset("myhash","sex","男");
    System.out.println(jedis.hget("myhash", "age"));
    System.out.println(jedis.hget("myhash", "sex"));
    //获取hash的所有的map中的数据
    Map myhash = jedis.hgetAll("myhash");
    Set filed = myhash.keySet();
    for (String string : filed) {
        System.out.println(string+":"+myhash.get(string));
    }
    jedis.close();
}

      6)zset

@Test
public void test05(){
    //setsorted集合
    Jedis jedis = new Jedis("192.168.42.128",6379);
    jedis.zadd("Mysorted",1,"项羽");
    jedis.zadd("Mysorted",21,"亚瑟");
    jedis.zadd("Mysorted",122,"孙悟空");
    //获取
    Set mysort = jedis.zrange("Mysorted", 0, -1);
    for (String s : mysort) {
        System.out.println(s);
    }
    jedis.close();
}

3、手机验证码功能案例

要求:

1)输入手机号,点击发送后随机生成6位数字码,2分钟有效

2)输入验证码,点击验证,返回成功或失败

3)每个手机号每天只能输入3次

public class RedisTest {
    public static void main(String[] arge){
        Jedis jedis = new Jedis("192.168.42.128",6379);
        Scanner in = new Scanner(System.in);
        while (true){
            System.out.println("输入手机号:");
            String phone = in.next();
            String rNum = RedisTest.getRandNumber();
            String countKey ="count"+phone;//保存次数一天三次
            String codeKey ="code"+phone;//保存验证码 60秒有效
            jedis.setex(codeKey, 60, rNum);
            System.out.println("验证码:"+rNum);
            System.out.println("输入验证码:");
            String set1 = in.next();
            if(set1.equals(jedis.get(codeKey))){
                System.out.println("输入验证码正确");
                break;
            }else {
                System.out.println("输入验证码错误");
            }
            String s = jedis.get(countKey);
            if(s==null){
                jedis.setex(countKey,24*60*60,"1");
            }
            if(Integer.parseInt(jedis.get(countKey))<=2){
                jedis.incr(countKey);
            }else {
                System.out.println("超过一天发送验证码的次数");
                break;
            }
        }

        jedis.close();
    }
    //随机生成6位数字
    public static String getRandNumber(){
        String temp ="";
        Random random = new Random();
        for(int i=0;i<6;i++){
            int randNum = random.nextInt(10);
            temp+=randNum;
        }
        return temp;
    }
}

 

你可能感兴趣的:(redis,redis)