Java connection Redis

From the previous study, I have a general understanding of redis.
So what is the first step for java to operate the redis database in the development process?

Of course, it's connected to redis~

Here are some basic operations for connecting redis;

1, Import dependency

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

2, Connect to Redis

2.1 connecting Redis

Compared with JDBC or Mybatis, connecting to redis is much simpler

//Connect to the specified redis
Jedis jedis = new Jedis("host ip","port");
//If you have a password, you need the following line
jedis.auth("password");
//Check whether the service is running. If it is running normally, a PONG will be returned; otherwise, a connection error will be returned
jedis.ping();

If you only connect the local address and the default port, you can directly use the parameterless constructor to generate the object.
Because Jedis inherits the Connection class, the default port and host IP are 6379 and localhost.

2.2 data operation

redis has its own operation commands, which can be referenced
Introduction to Redis (I)

2.2.1 String

methoddescribe
String set(String key, String value)Set redis string data
String get(String key)Get stored string data

2.2.2 list

methoddescribe
Long lpush(String key, String... strings)Inserts one or more values into the list header
Long rpush(String key, String... strings)Inserts one or more values at the end of the list
List lrange(String key, long start, long end)Gets the elements within the specified range of the list
String lindex(String key, long index)Get elements in the list by index
Long llen(String key)Get list length

2.2.3 hash

methoddescribe
String hmset(String key, Map<String, String> hash)Create hash table, assign fields and values
List hmget(String key, String... fields)Get hash table field value
Map<String, String> hgetAll(String key)Get all fields and values of the hash table
Set hkeys(String key)Get all fields
Long hlen(String key)Get the number of fields
Boolean hexists(String key, String field)Determine whether the specified field exists

2.2.4 Set

Similar to the set set in Java, it is an unordered set in which the elements have no order.

methoddescribe
Long sadd(String key, String... members)Add one or more members to the collection
Long scard(String key)Gets the number of members of the collection
Set smembers(String key)Gets the members in the collection
Boolean sismember(String key, String member)Determines whether the collection contains specified members
Set sinter(String... keys)Gets the intersection of multiple sets
Set sunion(String... keys)Gets the union of multiple sets
Set sdiff(String... keys)Returns the difference between the first set and other sets

2.2.5 sorted Set

Like a collection, it is also a collection of string type elements, and duplicate members are not allowed

methoddescribe
Long zadd(String key, double score, String member)Add one or more members to an ordered collection, or update the scores of existing members
Long zcard(String key)Gets the number of members of an ordered collection
Long zrank(String key, String member)Get the index corresponding to the element
Set zrange(String key, long start, long end)Gets the elements of the specified range of an ordered collection
Long zrem(String key, String... members)Remove one or more elements from the collection

3, Sample code

public static void main(String[] args){
        //Connect to local redis
        Jedis jedis = new Jedis();
        //If you have a password, you need the following line
        jedis.auth("password");
        //Check whether the service is running. If it is running normally, a PONG will be returned; otherwise, a connection error will be returned
        System.out.println(jedis.ping());

        /*****************String Example*****************/
        //Set string data
        jedis.set("word","helloWorld");
        //Read string data
        System.out.println(jedis.get("word"));
        //Delete data
        jedis.del("word");

        /*****************List Example*****************/
        jedis.lpush("list","google");
        jedis.lpush("list","aLi");
        jedis.rpush("list","Mi");

        List<String> stringList = jedis.lrange("list",0l,-1l);
        for(String str:stringList){
            System.out.println(str);
        }

        /*****************Hash Example*****************/
        HashMap<String,String> map = new HashMap<>();
        map.put("name","tom");
        map.put("age","81");
        jedis.hmset("man",map);
        System.out.println(jedis.hmget("man","name"));
        System.out.println(jedis.hgetAll("man"));
        System.out.println("Get all fields:"+jedis.hkeys("man"));
        System.out.println("Get number of fields:"+jedis.hlen("man"));
        System.out.println("judge age Does the field exist:"+jedis.hexists("man","age"));

        /*****************Set Example*****************/
        jedis.sadd("set1","1");
        jedis.sadd("set1","2");
        jedis.sadd("set1","1");
        jedis.sadd("set2","1");
        jedis.sadd("set2","4");
        System.out.println("Gets the number of members of the collection"+jedis.scard("set1"));
        System.out.println("Gets the members in the collection"+jedis.smembers("set2"));
        System.out.println("Determines whether the collection contains specified members"+jedis.sismember("set1","2"));
        System.out.println("Gets the intersection of multiple sets"+jedis.sinter("set1","set2"));
        System.out.println("Gets the union of multiple sets"+jedis.sunion("set1","set2"));
        System.out.println("Returns the difference between the first set and other sets"+jedis.sdiff("set1","set2"));

        /*****************sorted Set Example*****************/
        jedis.zadd("set1",1,"a");
        jedis.zadd("set1",3,"b");
        System.out.println("Gets the number of members of an ordered collection"+jedis.zcard("set1"));
        System.out.println("Get the index corresponding to the element"+jedis.zrank("set1","b"));
        System.out.println("Gets the elements of the specified range of an ordered collection"+jedis.zrange("set1",0,-1));
        System.out.println("Remove one or more elements from the collection"+jedis.zrem("set1","a"));
        jedis.zadd("set1",3,"c");
        System.out.println("Gets the elements of the specified range of an ordered collection"+jedis.zrange("set1",0,-1));
        jedis.zadd("set1",5,"b");
        System.out.println("Gets the elements of the specified range of an ordered collection"+jedis.zrange("set1",0,-1));


    }

Keywords: Java Redis

Added by winggundamth on Sat, 01 Jan 2022 09:15:53 +0200