This is the 22nd day of my participation in the More text Challenge. For details, see more text Challenge


Related articles

Redis combat summary: Redis combat


preface

Redis publish subscribe (PUB/SUB) is a message communication mode: the sender (PUB) sends the message and the subscriber (SUB) receives the message.

The Redis client can subscribe to any number of channels!

First, the implementation of subscription publishing function

(1) command:

These commands are widely used to build instant messaging applications such as chatrooms and live broadcasts, live alerts, and so on.

② Implementation of publish-subscribe:

1. Subscriber:

127.0. 01.:6379> ping
PONG
127.0. 01.:6379> SUBSCRIBE dingdada # SUBSCRIBE dingdada # SUBSCRIBE dingdada... (press Ctrl-C to quit)1) "subscribe"
2) "dingdada"
3) (integer) 1# Waiting for information to be pushed1) "message"# message2) "dingdada"# what channel is it from3) "hello world\xef\xbc\x81"# Details of the message1) "message"
2) "dingdada"
3) "my name is dyj\x81"
Copy the code

2. Sending end:

127.0. 01.:6379> ping
PONG
127.0. 01.:6379> PUBLISH dingdada "hello world!"Send message to dingdada channel (INTEGER)1
127.0. 01.:6379> PUBLISH dingdada "my name is dyj"Send message to dingdada channel (INTEGER)1
Copy the code

As shown in the figure:

Subscribe to the end:

The sender:

PSUBSCRIBE command: subscribe to the specified channel!

PSUBSCRIBE + channel. # Subscribe to a given schema, more than oneCopy the code

PUBLISH command: send messages to a specified channel!

PUBLISH + channel + message # Send message to specified channelCopy the code

⑤PUNSUBSCRIBE command: Unsubscribe!

Instructs the client to unsubscribe from the specified mode, or exit all modes if no mode is provided.Copy the code

⑥SUBSCRIBE. No fine!!

⑦UNSUBSCRIBE!

End summary:

Pub/Sub literally means Publish and Subscribe. In Redis, you can set a key to Publish and Subscribe to. When a key is published, all clients that Subscribe to it will receive the corresponding message.

The most obvious use of this feature is as a real-time messaging system, such as general im, group chat, etc.


Use Jedis to operate Redis

In short, Jedis is an official Java connection development tool recommended by Redis!

Although the current springboot2.x version has changed Jedis to Lettuce, I think it is necessary to know about the use of Jedis!

  1. How to integrate Jedis and connect to Redis database in Java project?

Create a Maven project

Empty can ~ how to create I do not need to repeat!

Import Jedis and FastJSON dependencies and wait for the download to complete!

<! <groupId> <artifactId>jedis</artifactId> <version>3.2.0< version> </dependency> <! -- dependency> <groupId>com.alibaba</groupId> <artifactId> <version>1.2.62</version> </dependency>Copy the code

③ Connect Redis test, here in order to facilitate the test, connect is local Redis service, connect remote need to change the configuration file and close the firewall, later will make a separate article to introduce this!

// 1, new Jedis object
Jedis jedis = new Jedis("127.0.0.1".6379);
// All jedis commands are all our previous commands
System.out.println(jedis.ping());
Copy the code

As shown in the figure:

Return to PONG to prove that the connection is successful!

(4) How to use the API?

 		// 1, new Jedis object
        Jedis jedis = new Jedis("127.0.0.1".6379);
        jedis.flushDB();// Delete all data from the current library
        jedis.set("name"."dingyongjun");
        jedis.set("age"."23");
        jedis.set("high"."173");
        System.out.println("name:"+jedis.get("name") +"\nage:"+jedis.get("age") +"\nhigh"+jedis.get("high"));
Copy the code

As shown in the figure:

		jedis.lpush("list"."1"."2"."3"."4");
        System.out.println("list: "+jedis.lrange("list".0, -1));
Copy the code

As shown in the figure:

⑤ Conclusion: Connect to use Redis in Jedis, and Redis console command is exactly the same, I do not repeat one command to write again, later if there is time, I will give all the commands to improve slowly!


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah