6.1 Jedis所需要的jar包

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>3.2.0</version>
  5. </dependency>

6.2 连接Redis注意事项

禁用Linux的防火墙:
Linux(CentOS7)里执行命令:systemctl stop/disable firewalld.service
Linux(CentOS6)里执行命令:
关闭 /开启/重启防火墙
# service iptables stop
# service iptables start
# service iptables restart

redis.conf中注释掉bind 127.0.0.1 ,然后设置 protected-mode no

6.3 Jedis常用操作

public class JedisDemol {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.142.131",6379);
//        Jedis jedis = new Jedis("127.0.0.1",6379);

        String ping = jedis.ping();
        System.out.println(ping);
    }
    @Test
    public void demo5() {
        Jedis jedis = new Jedis("192.168.142.131", 6379);
        jedis.zadd("china", 100d, "shanghai");
        Set<String> china = jedis.zrange("china", 0, -1);
        System.out.println(china);
    }

    @Test
    public void demo4(){
        Jedis jedis = new Jedis("192.168.142.131",6379);

        jedis.hset("users", "age","20");
        String hget = jedis.hget("users", "age");
        System.out.println(hget);
    }

    @Test
    public void demo3(){
        Jedis jedis = new Jedis("192.168.142.131",6379);

        jedis.sadd("names", "lucy","jack");

        Set<String> names = jedis.smembers("names");
        System.out.println(names);
    }

    @Test
    public void demo2(){
        Jedis jedis = new Jedis("192.168.142.131",6379);

        jedis.lpush("key1", "lucy","mary","jack");
        List<String> key1 = jedis.lrange("key1", 0, -1);
        System.out.println(key1);
    }


    @Test
    public void demo1(){
        Jedis jedis = new Jedis("192.168.142.131",6379);

        jedis.set("name", "lucy");

        String name = jedis.get("name");
        System.out.println(name);

        jedis.mset("k1","v1","k2","v2");
        List<String> mget = jedis.mget("k1", "k2");
        System.out.println(mget);

        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
    }

}

6.4 RedisJedis实例

1.1. 完成一个手机验证码功能
要求:
1、输入手机号,点击发送后随机生成6位数字码,2分钟有效
2、输入验证码,点击验证,返回成功或失败
3、每个手机号每天只能输入3次

public class PhoneCode {

    public static void main(String[] args) {
//        verifyCode("13542746647");
        getRedisCode("13542746647","618840");
    }

    public static void getRedisCode(String phone,String code){

        Jedis jedis = new Jedis("192.168.142.131", 6379);
        String codeKey = "VerifyCode" + phone + ":code";
        String redisCode = jedis.get(codeKey);

        if(redisCode.equals(code)){
            System.out.println("成功");

        }else {
            System.out.println("失败");
        }


    }


    //每个手机每天只能发送三次,验证码放到redis中,设置过期时间120
    public static void verifyCode(String phone){
        Jedis jedis = new Jedis("192.168.142.131", 6379);

        String countKey = "VerifyCode" + phone + ":count";
        String codeKey = "VerifyCode" + phone + ":code";

        String count = jedis.get(codeKey);
        if(count == null){
            jedis.setex(countKey, 24*60*60, "1");
        }else if(Integer.parseInt(count) <= 2){
            jedis.incr(codeKey);
        }else if(Integer.parseInt(count) > 2){
            System.out.println("今天发送次数已经超过三次");
            jedis.close();
        }

        String vcode = getCode();
        jedis.setex(codeKey, 120, vcode);
        jedis.close();

    }

    //生成6位数字验证码
    public static String getCode(){
        Random random = new Random();
        String code = "";

        for(int i = 0;i < 6;i++){
            int rand = random.nextInt(10);
            code += rand;
        }

        return code;
    }
}