当前位置:网站首页>Jedis operation redis

Jedis operation redis

2022-07-23 08:14:00 Annoying dog broke into your blog

Jedis operation Redis

1. Introduce dependencies

Here we use 3.2.0 Version as an example :

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.2.0</version>
</dependency>

2. Jedis Basic operation

2.1 Test connection

If Redis Install in ECs , Pay attention to configuring the protective wall security group of the server .

public static void main(String[] args) {
    
    
    Jedis jedis = new Jedis(" Yours redis Of the host server IP Address ", 6379);
    //  If your Redis Password set , Perform this step 
    jedis.auth(" Yours redis password ");

    String ping = jedis.ping();
    System.out.println(ping);
    
    jedis.close();
}

Console printing :

PONG

Description connection successful .

2.2 operation Redis

Jedis It encapsulates a lot of API It can be used for Redis To operate , Next is the operation demonstration .

operation Redis The relevant commands of Jedis There are corresponding methods in .

  • operation String type
@Test
public void testString() {
    
    String set = jedis.set("key1", "v1");
    System.out.println(set);

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

}

Console printing :

OK
v1
  • operation List list
@Test
public void testList() {
    
    jedis.lpush("list1", "v1", "v2", "v3");

    List<String> list = jedis.lrange("list1", 0, -1);
    list.forEach(System.out::println);
    
    jedis.close();

}

Console printing :

v3
v2
v1
  • operation Set aggregate
@Test
public void testSet() {
    
    Long sadd = jedis.sadd("set1", "tom", "jerry", "jack");
    Set<String> set = jedis.smembers("set1");
    System.out.println(set);
    
    jedis.close();

}

Console printing :

[tom, jack, jerry]
  • operation Hash
@Test
public void testHash() {
    
    Map<String, String> map = new HashMap<>();
    map.put("id", "1");
    map.put("name", "Tom");
    map.put("age", "18");

    jedis.hset("map1", map);

    String id = jedis.hget("map1", "id");
    String name = jedis.hget("map1", "name");
    String age = jedis.hget("map1", "age");

    System.out.println("id = " + id);
    System.out.println("name = " + name);
    System.out.println("age = " + age);
    
    jedis.close();

}

Console printing :

id = 1
name = Tom
age = 18
  • operation ZSet Ordered set
@Test
public void testZSet() {
    
    jedis.zadd("zset1", 100, "java");
    jedis.zadd("zset1", 50, "c++");
    jedis.zadd("zset1", 25, "php");

    Set<String> set = jedis.zrange("zset1", 0, -1);

    System.out.println(set);
    
    jedis.close();
}

Console printing :

[php, c++, java]

3. Analog verification code case

Business requirements :

1、 Enter phone number , Click send and randomly generate 6 Digit code ,2 Minutes are valid

2、 Enter verification code , Click verify , Return to success or failure

3、 Each mobile phone number can only be entered every day 3 Time

Code implementation :

package com.jc;

import redis.clients.jedis.Jedis;

import java.util.UUID;

public class PhoneCodeTest {
    

    private static Jedis jedis = new Jedis("180.76.116.222", 6379);
    private static final int TIMEOUT_CODE = 60 * 2;
    private static final int TIMEOUT_PHONE = 60 * 60 * 24;

    static {
    
        jedis.auth("576a6aWjj");
    }

    //  Generate verification code randomly 
    public static String getCode() {
    
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
        return code;
    }

    //  Send verification code 
    public static void verifyCode(String phone, String code) {
    
        String countKey = "VerifyCode" + phone + ":count";
        String codeKey = "VerifyCode" + phone + ":code";

        String count = jedis.get(countKey);
        if (count == null) {
    
            jedis.setex(countKey, TIMEOUT_PHONE, "1");
        } else if (Long.parseLong(count) < 3) {
    
            jedis.incr(countKey);
        } else {
    
            System.out.println(" It has been sent today 3 Secondary verification code !");
            jedis.close();
            return;
        }

        //  Set the verification code 
        jedis.setex(codeKey, TIMEOUT_CODE, code);
    }


    public static void checkVerifyCode(String phone, String code) {
    
        String countKey = "VerifyCode" + phone + ":code";

        String redisCode = jedis.get(countKey);

        if (code.equals(redisCode)) {
    
            System.out.println(" success !");
        } else {
    
            System.out.println(" Failure !");
        }
    }
    
    public static void main(String[] args) {
    
        verifyCode("123123123", getCode());
// checkVerifyCode("123123123", "b9dd1c");
        jedis.close();
    }

}
原网站

版权声明
本文为[Annoying dog broke into your blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222228477956.html