Summary of Redis | Day5 and Java operation in seven days

Posted by cmburns69 on Wed, 26 Jan 2022 04:40:30 +0100

1. jedis class

1. First, we need to import the jedis package
Latest jedis

After that, we put it into the directory at the same level as src, and then right-click the item to find:


Next, let's test. At the beginning, I encountered such a problem

The reason is that I didn't open the server, so you must remember to open the server.

Now let's test:

    public static void main(String[] args) throws InterruptedException {
//        New Jedis class object
        Jedis jedis = new Jedis("127.0.0.1", 6379);
//       Test Redis connection
        System.out.println("redis Connection test:" + jedis.ping());
    }


Redis database operation
In Jedis class, the data operation method is basically the same as the command, and the return value type is the same. We can call it directly

    public static void main(String[] args) throws InterruptedException {
//        New Jedis class object
        Jedis jedis = new Jedis("127.0.0.1", 6379);
//       Test Redis connection
        System.out.println("redis Connection test:" + jedis.ping());
//        Determine whether a key exists
        System.out.println("Determine whether a key exists:" + jedis.exists("mykey"));
//        Get all keys
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
 
//        Delete key value pairs according to key
        System.out.println("Delete key" + jedis.del("mykey"));
 
 
        /**
         * String type
         */
//        Set a key value pair
        jedis.set("mykey1", "myValue1");
        jedis.set("mykey2", "myValue2");
        jedis.set("mykey3", "myValue3");
//        Get the value of a data according to the index
        System.out.println("The value obtained is:" + jedis.get("mykey2"));
        System.out.println("Get multiple key value pairs:" + jedis.mget("mykey1", "mykey2", "mykey3"));
        System.out.println("obtain mykey1 Length of data:" + jedis.strlen("mykey1"));
        System.out.println("return key Substring of string value in:" + jedis.getrange("mykey2", 1, 5));
        System.out.println("String append:" + jedis.append("mykey3", "hello"));
        System.out.println("Get mykey3 Value of:" + jedis.get("mykey3"));
 
        /**
         * Hash type
         */
//        Set a Hash data
        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("name", "huixiaoyuan");
        valueMap.put("sex", "male");
        valueMap.put("age", "3");
        System.out.println("Set a Hash data" + jedis.hmset("myHash", valueMap));
        System.out.println("Get all fields and values in the specified hash table:" + jedis.hgetAll("myHash"));
        System.out.println("Gets the value of the specified field stored in the hash table:" + jedis.hget("myHash", "name"));
        System.out.println("Delete one or more hash table fields:" + jedis.hdel("myHash", "sex"));
        System.out.println("Get the number of fields in the hash table:" + jedis.hlen("myHash"));
 
        /**
         * List type
         */
        System.out.println("Insert one or more elements into the list header:" + jedis.lpush("myList", "l1", "l2", "l3"));
        System.out.println("Inserts one or more elements at the end of the list:" + jedis.rpush("myList", "l4"));
        System.out.println("Get list length:" + jedis.llen("myList"));
        System.out.println("Get elements in the list by index:" + jedis.lindex("myList", 2));
        System.out.println("Remove and get the first element of the list:" + jedis.lpop("myList"));
        System.out.println("Remove and get the last element of the list:" + jedis.rpop("myList"));
 
 
        /**
         * Hash type
         */
        System.out.println("Set a Hash data" + jedis.hmset("myHash", valueMap));
        System.out.println("Get all fields and values in the specified hash table:" + jedis.hgetAll("myHash"));
        System.out.println("Gets the value of the specified field stored in the hash table:" + jedis.hget("myHash", "name"));
        System.out.println("Delete one or more hash table fields:" + jedis.hdel("myHash", "sex"));
        System.out.println("Get the number of fields in the hash table:" + jedis.hlen("myHash"));
 
        /**
         * Set type
         */
        System.out.println("Add one or more members to the collection:" + jedis.sadd("mySet","s1","s2","s3"));
        System.out.println("Add one or more members to the collection:" + jedis.sadd("mySet2","s3","s4","s5"));
        System.out.println("Gets the number of members in the collection:" + jedis.scard("mySet"));
        System.out.println("Returns the difference between the first set and other sets:" + jedis.sdiff("mySet","mySet2"));
        System.out.println("Returns all elements in the collection:" + jedis.smembers("mySet"));
 
 
        /**
         *ZSet type
         */
        System.out.println("Adds one or more members to an ordered collection:" + jedis.zadd("myZset",1,"m1"));
        System.out.println("Adds one or more members to an ordered collection:" + jedis.zadd("myZset",2,"m2"));
        System.out.println("Adds one or more members to an ordered collection:" + jedis.zadd("myZset",3,"m3"));
        System.out.println("Gets the number of members of an ordered collection:" + jedis.zcard("myZset"));
        System.out.println("Calculates the number of members of a specified interval score in an ordered set:" + jedis.zcount("myZset",0,2));
        System.out.println("Removes one or more elements from an ordered collection:" + jedis.zrem("myZset","m2"));
 
    }

redis connection test: PONG
Determine whether a key exists: false
[]
Delete key 0
The value obtained is myValue2
Get multiple key value pairs: [myValue1, myValue2, myValue3]
Length of data obtained from mykey1: 8
Returns the substring of the string value in the key: yValu
String append: 13
Get the value of mykey3: myValue3hello
Set a Hash data OK
Get all fields and values in the specified hash table: {name=huixiaoyuan, age=3, sex = male}
Get the value of the specified field stored in the hash table: ABCD
Delete one or more hash table fields: 1
Get the number of fields in the hash table: 2
Insert one or more elements into the list header: 3
Insert one or more elements at the end of the list: 4
Get list length: 4
Get elements in the list by index: l1
Remove and get the first element of the list: l3
Remove and get the last element of the list: l4
Set a Hash data OK
Get all fields and values in the specified hash table: {name=huixiaoyuan, age=3, sex = male}
Get the value of the specified field stored in the hash table: ABCD
Delete one or more hash table fields: 1
Get the number of fields in the hash table: 2
Add one or more members to the collection: 3
Add one or more members to the collection: 3
Get the number of members in the collection: 3
Returns the difference between the first set and other sets: [s1, s2]
Returns all elements in the collection: [s2, s1, s3]
Add one or more members to the ordered collection: 1
Add one or more members to the ordered collection: 1
Add one or more members to the ordered collection: 1
Get the number of members of the ordered collection: 3
Calculate the number of members of the specified interval score in the ordered set: 2
Remove one or more elements from an ordered collection: 1

Topics: Java Redis Cache