Java uses Redis (jedis)

Novice tutorial: https://www.runoob.com/redis/redis-java.html

installation

  • Ordinary projects need to import the downloaded jedis jar package
  • Maven project imports jedis dependencies
<dependencies>
   <!-- Jedis 所需要的jar包 -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

Test connection

Before connecting, please comment out bind 127.0.0.1 -::1 in the redis.conf configuration file. Otherwise, external access to redis on this terminal is not allowed.

Insert picture description here

Here is a remote connection to the Alibaba Cloud server, and port 6379 must be set in the firewall before connecting.

	public static void main(String[] args) {

        // 创建jedis对象
        Jedis jedis = new Jedis("redis所在终端的ip地址",6379);

        String pong = jedis.ping();

        System.out.println("连接成功"+pong);
        
        jedis.close();

    }
Insert picture description here

Test data type

1. Test key operation
	/**
     * 测试键的操作
     * @param jedis
     */
    public static void testKey(Jedis jedis){
        System.out.println("===========测试键的操作=============");

        jedis.mset("k1","v1","k2","v2","k3","v3");
        Set<String> keys = jedis.keys("*");

        for (String key : keys) {
            System.out.println(key);
            jedis.del(key);
        }

        System.out.println(jedis.exists("k1"));

        System.out.println(jedis.ttl("k1"));

        System.out.println(jedis.get("k1"));

    }
2. Test the operation of the String type
	/**
     * 测试String类型的操作
     * @param jedis
     */
    public static void testString(Jedis jedis){
        System.out.println("===========测试String类型的操作=============");

        jedis.mset("k1","v1","k2","v2","k3","v3");

        System.out.println(jedis.mget("k1",  "k2",  "k3"));
    }
Insert picture description here
3. Test the operation of the List type
	/**
     * 测试List类型的操作
     * @param jedis
     */
    public static void testList(Jedis jedis){
        System.out.println("===========测试List类型的操作=============");

        jedis.lpush("arr", "1", "2", "3", "4", "5");

        jedis.lrange("arr",0,-1).forEach(System.out::println);

    }
Insert picture description here
4. Test the operation of the Set type
	/**
     * 测试Set类型的操作
     * @param jedis
     */
    public static void testSet(Jedis jedis){
        System.out.println("===========测试Set类型的操作=============");

        jedis.sadd("sset","1","2","1","2","3");

        jedis.smembers("sset").forEach(System.out::println);

    }
Insert picture description here
5. Test Hash type operations
	/**
     * 测试Hash类型的操作
     * @param jedis
     */
    public static void testHash(Jedis jedis){
        System.out.println("===========测试Hash类型的操作=============");

        HashMap<String,String> map = new HashMap<>();

        map.put("name","zs");
        map.put("age","12");
        map.put("gender","man");

        jedis.hmset("user",map);

        jedis.hmget("user","name","age","gender").forEach(System.out::println);

    }
Insert picture description here
6. Test the operation of the ZSet type
    /**
     * 测试ZSet类型的操作
     * @param jedis
     */
    public static void testZSet(Jedis jedis){
        System.out.println("===========测试ZSet类型的操作=============");

        jedis.zadd("language",1,"java");

        jedis.zadd("language",2,"other");

        jedis.zrange("language",0,-1).forEach(System.out::println);

    }
Insert picture description here

to sum up

  1. To use redis in java, you must first import the jedis package or dependency
  2. When using jedis to connect to the redis of the remote server, the corresponding port should be opened to the outside, and bind 127.0.0.1 -::1 should be annotated in the redis configuration file
  3. When using jedis for redis related operations, the method name is the same as the command directly in the redis client