Redis redis basic types and Java operation

Time:2021-5-6
1 Introduction to redis
Redis (promote dictionary server) is an open source, network-based, memory based, persistent key value pair storage system written in ANSI C. At present, the most popular key value pair storage system. It is often used as database, cache and message queue.
Advantages of redis
  • The efficiency of memory based database is faster than that of disk based database;
  • The working mode of redis is single thread, which does not need synchronization between threads. Redis uses single thread mainly because its bottleneck is memory and broadband, not CPU;
  • The value of key value in redis can be string, linked list, set, hash table, etc;
  • Redis supports data persistence and can adopt RDB, AOF and RDB & AOF;
  • Redis supports the master-slave structure, and can use the slave instance for data backup;
2. Introduction of redis data structure
  1. String (string);
  2. List;
  3. Set (set)
  4. Hash (hash)
  5. Zset (ordered set)
Redis in Java
1. Introducing Maven dependency
redis.clients
    jedis
    3.3.0
2. Testing

1 @Test
2 public void test01(){
3     Jedis jedis = new Jedis("localhost");
4  
5     jedis.set("hello","world");
6  
7     System.out.println(jedis.get("hello"));
8 }

 

The following describes the use of five basic types;

2.1.1 string;
Redis string is a sequence of bytes, which can store three types of values;
  • character string
  • integer
  • Floating point number

Redis add and subtract commands

Java uses redis to implement auto increase and auto decrease commands
 1 @Test
 2 public void test02(){
 3     Jedis jedis = new Jedis("localhost");
 4 
 5 
 6     jedis.set("num","10");
 7     // 11
 8     System.out.println(jedis.incr("num"));
 9     // 31
10     System.out.println(jedis.incrBy("num",20));
11     // 11
12     System.out.println(jedis.decrBy("num",20));
13 }

Java uses redis to process substrings

1 @Test
 2 public void test03() {
 3     Jedis jedis = new Jedis("localhost");
 4 // redis stores the string
 5     jedis.set("str","hello");
 6     System.out.println(jedis.get("str"));
 7 // String appending
 8     jedis.append("str","world");
 9     System.out.println(jedis.get("str"));
10 // substring
11     System.out.println(jedis.getrange("str",5,9));
12 // insert a string at the specified location (the string after the specified location will be overwritten)
13     jedis.setrange("str",5,",jiz");
14     System.out.println(jedis.get("str"));
15 }

Results of operation:

2.1.2 list
 
    Redis allows users to push or pop elements from both ends of the sequence, obtain list elements and perform various common list operations; It can be used to store task information, recently browsed articles or frequently used contact information.
 

Redis’s basic operations on lists

 

1 @Test
 2 public void test04() {
 3     Jedis jedis = new Jedis("localhost");
 4     // 1 2
 5     jedis.rpush("list", "1", "2");
 6     // 1
 7     System.out.println(jedis.rpop("list")); //2
 8     // 3 1
 9     jedis.lpush("list","3");
10     // 1
11     System.out.println(jedis.lpop("list")); //3
12     // 3 2 1
13     jedis.lpush("list", "2", "3");
14 // get the element with position 1
15     System.out.println(jedis.lindex("list",1)); // 3
16 }

 

 

Blocking pop-up command and pop-up and push command, the most common use cases are message queue and task queue;

 

Redis blocking list operation

 

1 @Test
 2 public void test05() {
 3     Jedis jedis = new Jedis("localhost");
 4     // item1 item2 item3 item4
 5     jedis.rpush("list1", "item1", "item2", "item3", "item4");
 6 // the leftmost element Item2 Item3 item4 pops up
 7     System.out.println(jedis.blpop(0, "list1"));
 8 // the rightmost element Item2 Item3 pops up
 9     System.out.println(jedis.brpop(0, "list1"));
10 // pop up the rightmost element, push it to the leftmost end of the new queue, and return the element LIST1 = > Item2 List2 = > Item3 to the user
11     System.out.println(jedis.rpoplpush("list1", "list2")); // item3
12 // pop up the rightmost element, push it to the leftmost end of the new queue, and return the element to the user. If LIST1 has no element, wait for timeout LIST1 = > Item2 List2 = > Item2, Item3
13     System.out.println(jedis.brpoplpush("list1", "list2",10));
14 }

 

 

 
 
2.1.3 set
    
Redis’s collection stores multiple different elements in an unordered way. Users can quickly add elements, remove elements and check whether an element is in the collection;
 

Redis operations on collections

1 @Test
 2 public void test06() {
 3     Jedis jedis = new Jedis("localhost");
 4 // generate set element
 5     jedis.sadd("set", "a", "b", "c", "c");
 6     System.out.println(jedis.smembers("set"));
 7 // remove element B
 8     jedis.srem("set", "b");
 9     System.out.println(jedis.smembers("set"));
10 // check whether element a is in the collection
11     System.out.println(jedis.sismember("set", "a"));
12 // query the number of elements in set
13     System.out.println(jedis.scard("set"));
14 // one or more elements are returned randomly. If count is positive, the returned random elements will not be repeated; If count is negative, the random elements returned may be repeated;
15     System.out.println(jedis.srandmember("set", 1));
16 // remove elements randomly
17     System.out.println(jedis.spop("set"));
18 // remove the element specified by set to set2. If the removal is successful, 1 is returned. Otherwise, 0 is returned
19     if (jedis.smove("set", "set2", "b") > 0) {
20         System.out.println("success");
21         System.out.println(jedis.smembers("set2"));
22     }else {
23         System.out.println("error");
24         System.out.println(jedis.smembers("set"));
25     }

 

 

 

 

Commands can be used for union, intersection, and difference operations.
 
 
2.1.4 hash
Redis hash allows users to store multiple key value pairs in a redis building and store some related data together.
 

 

 

1 @Test
 2 public void test07() {
 3     Jedis jedis = new Jedis("localhost");
 4     Map map = new HashMap<>();
 5     map.put("k1", "value1");
 6     map.put("k2", "value2");
 7     map.put("k3", "value3");
 8 // store hash value
 9     jedis.hmset("hash-set", map);
10 // get the value of the specified key
11     System.out.println(jedis.hmget("hash-set", "k1", "k2"));
12     System.out.println(jedis.hmget("hash-set", "k1"));
13 // delete the specified key
14     jedis.hdel("hash-set", "k1");
15     System.out.println(jedis.hmget("hash-set", "k1"));
16 // number of Statistics
17     System.out.println(jedis.hlen("hash-set"));
18 }

 

 

1 @Test
 2 public void test08() {
 3     Jedis jedis = new Jedis("localhost");
 4     Map map = new HashMap<>();
 5     map.put("num1", "10");
 6     map.put("num2", "20");
 7     map.put("num3", "30");
 8 // store hash value
 9     jedis.hmset("num-set", map);
10 // check whether the specified key is stored in the hash
11     System.out.println(jedis.hexists("num-set", "num1"));
12     //System.out.println(jedis.hexists("num-set", "num4"));
13 // get all the keys contained in the hash table
14     System.out.println(jedis.hkeys("num-set"));
15 // get all the values contained in the hash table
16     System.out.println(jedis.hvals("num-set"));
17 // get all key value pairs
18     System.out.println(jedis.hgetAll("num-set"));
19 // add the value stored in the key to the integer increment
20     jedis.hincrBy("num-set", "num1", 20);
21     System.out.println(jedis.hgetAll("num-set"));
22 // add a floating-point number to the value stored in the key
23     jedis.hincrByFloat("num-set", "num2", 20.8);
24     System.out.println(jedis.hgetAll("num-set"));
25 }

 
2.1.4   Ordered set
    
It stores the mapping between members and scores, and provides score processing commands to fetch or scan members and scores in order according to the size of scores.
 

 

 

@Test
public void test09() {
    Jedis jedis = new Jedis("localhost");


    //Generating ordered sets
    jedis.zadd("zset-key", new HashMap() {{
        put("a", 1d);
        put("b", 2d);
        put("c", 3d);
    }});
    //Returns the number of members contained in an ordered collection
    System.out.println(jedis.zcard("zset-key"));
    //The value specified in the ordered set + increment
    jedis.zincrby("zset-key", 5d, "a");
    //Returns the value of member
    System.out.println(jedis.zscore("zset-key", "a"));
    //Returns the number of members with a score between min and max
    System.out.println(jedis.zcount("zset-key", 4, 7));
    //Returns the members in an ordered set ranking between start and stop,
    System.out.println(jedis.zrange("zset-key", 0, 3));
    System.out.println(jedis.zrange("zset-key", 2, 3));
    //If the optional with scores option is given, the score of the member is also returned
    System.out.println(jedis.zrangeByScoreWithScores("zset-key", 2, 3));
}

 

 
 

 

 

1 @Test
 2 public void test10() {
 3     Jedis jedis = new Jedis("localhost");
 4 // generating ordered sets
 5     jedis.zadd("zset2-key", new HashMap() {{
 6         put("a", 1d);
 7         put("b", 3d);
 8         put("c", 2d);
 9     }});
10 // returns the ranking of the specified member in the ordered set
11     System.out.println(jedis.zrevrank("zset2-key", "a"));
12 // returns the descending order of the member scores within the given ranking range of the ordered set
13     System.out.println(jedis.zrevrange("zset2-key", 0, jedis.zcard("zset-key")));
14 // returns all the members in the ordered set whose score is between min and Max, and sorts them according to the score
15     System.out.println(jedis.zrangeByScore("zset2-key", 2, 3));
16 // remove all members ranking between start and Max, and delete members ranking between 1-2
17     jedis.zremrangeByRank("zset2-key", 1, 2);
18 // query the removed result
19     System.out.println(jedis.zrevrange("zset2-key", 0, jedis.zcard("zset-key")));
20     jedis.zadd("zset2-key", new HashMap() {{
21         put("a", 1d);
22         put("b", 3d);
23         put("c", 2d);
24     }});
25 // remove all members with scores between min and max
26     jedis.zremrangeByScore("zset2-key", 1, 2);
27     System.out.println(jedis.zrevrange("zset2-key", 0, jedis.zcard("zset-key")));
28 }

 
Intersection and union
The intersection operation of redis is as follows
Add the scores of the members with the same key. If a member is unique in the set, it does not exist after merging;
 
The union operation of redis is as follows
Add the scores of the members with the same key. If a member is unique in the set, it will be placed in the new set after merging;
1 @Test
 2 public void test11() {
 3     Jedis jedis = new Jedis("localhost");
 4     jedis.zadd("zset3-key", new HashMap() {{
 5         put("a", 1d);
 6         put("b", 4d);
 7         put("c", 2d);
 8     }});
 9     jedis.zadd("zset4-key", new HashMap() {{
10         put("a", 7d);
11         put("e", 3d);
12         put("f", 1d);
13     }});
14 // performing intersection operations on ordered sets
15     jedis.zinterstore("zset5-key", "zset3-key", "zset4-key");
16     System.out.println(jedis.zrevrangeWithScores("zset5-key", 0, jedis.zcard("zset5-key")));
17 // Union of ordered sets
18     jedis.zunionstore("zset6-key", "zset3-key", "zset4-key");
19     System.out.println(jedis.zrevrangeWithScores("zset6-key", 0, jedis.zcard("zset6-key")));
20 }

 

 

 

 

Recommended Today

JavaScript serialization 32 – common mouse events

1、 Common mouse events window.onload = function (ev) { var logo = document.getElementsByTagName(“img”)[0]; //1. New events logo.onmouseover = function (e1) { console.log(“—–“);// Mouse into the picture will trigger, also hover over the picture this.setAttribute(“src”,”https://www.cnblogs.com/ruigege0000/p/img/img_02.png”); } logo.onmouseleave = function (e2) { this.setAttribute(“src”,” https://www.cnblogs.com/ruigege0000/p/img/img_ 05.png”);// When the mouse leaves the picture, it will trigger } }