Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Redis Protocol

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of a sample analysis of the Redis protocol. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

Preface

We have used a lot of redis clients, have you ever tried to create a redis client yourself? In fact, it is very simple, based on socket, listen to port 6379, parse the data on it.

Redis protocol

The process of parsing the data mainly depends on the protocol of redis. Let's write a simple example and take a look at redis's protocol:

Public class RedisTest {public static void main (String [] args) {Jedis jedis = new Jedis ("127.0.0.1", 6379); jedis.set ("eat", "I want to eat");}}

Monitor socket:

Public static void main (String [] args) throws IOException {ServerSocket server = new ServerSocket (6379); Socket socket = server.accept (); byte [] chars = new byte [64]; socket.getInputStream (). Read (chars); System.out.println (new String (chars));}

Take a look at the data:

* 3 $3SET$3eat$13I want to eat

Refer to the official agreement document https://redis.io/topics/protocol to parse the data.

(1) simple string Simple Strings, beginning with "+" plus sign (2) error Errors, starting with "-" minus sign (3) integer type Integer, beginning with ":" colon (4) large string type Bulk Strings, beginning with "$" dollar sign, length limit 512m (5) group type Arrays, beginning with "*" asterisk and Each part of the agreement ends with "\ r\ n" (CRLF).

So the meaning of the above data is:

* 3 the array contains three elements, namely, SET, eat, I want to eat$3 is a string, and the string length is 3SET string content $3 is a string, and the string length is 3eat string content $13 is a string, and the string length is the content of 13I want to eat string

The data for performing get 'eat' is as follows:

* 2 $3GET$3eat to hire a client

Once we have mastered the redis protocol and socket, we can try to play a client.

Socket:

Public RedisClient (String host, int port) {try {this.socket = new Socket (host,port); this.outputStream = this.socket.getOutputStream (); this.inputStream = this.socket.getInputStream ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}

Set protocol:

Public String set (final String key, String value) {StringBuilder sb = new StringBuilder (); / / although the output will be escaped, we still have to send it with\ r\ nsb.append ("* 3"). Append ("\ r\ n"); sb.append ("$3"). Append ("\ r\ n"); sb.append ("SET"). Append ("\ r\ n") Sb.append ("$") .append (key.length ()) .append ("\ r\ n"); sb.append (key) .append ("\ r\ n"); sb.append ("$") .append (value.length ()) .append ("\ r\ n"); sb.append (value) .append ("\ r\ n"); byte [] bytes= new byte [1024] Try {outputStream.write (sb.toString (). GetBytes ()); inputStream.read (bytes);} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return new String (bytes);}

Test:

RedisClient redisClient = new RedisClient ("127.0.0.1", 6379); String result = redisClient.set ("eat", "please eat"); System.out.println (result)

Execution result:

+ OK Thank you for your reading! This is the end of the sample analysis of Redis protocol. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report