Redis simulates jedis operation string type data

Time:2020-2-7

 

1、 Thinking analysis

The data transmission of redis follows the resp protocol. You only need to transfer the data to the redis server through the socket according to the resp protocol

 

Resp data format:

 

 

 

 

2、 Specific implementation

package com.ahd.jedis;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/***
 *Simulate jedis to operate redis
 */
public class MyJedis {
    private Socket socket;

    public MyJedis(String url,Integer port) throws IOException {
        socket=new Socket(url,port);
    }

    public static void main(String[] args) throws IOException {
        MyJedis myJedis=new MyJedis("127.0.0.1",6379);

        //myJedis.set("sex","sdf");
        //myJedis.get("sex");
        myJedis.del("sex");
    }

    /***
     *Analog set string type
     * @param key
     * @param value
     */
    public void set(String key,String value) throws IOException {
        StringBuffer stringBuffer=new StringBuffer();
        //GetBytes is because the key and value may be in Chinese. If you get the length directly, you will make an error
        stringBuffer.append("*3"+"\r\n")
                .append("$3"+"\r\n").append("SET"+"\r\n")
                .append("$"+key.getBytes().length+"\r\n").append(key+"\r\n")
                .append("$"+value.getBytes().length+"\r\n").append(value+"\r\n");
        OutputStream outputStream = socket.getOutputStream();

        System.out.println(stringBuffer);
        outputStream.write(stringBuffer.toString().getBytes());

    }

    /***
     *Simulate get string
     * @param key
     * @return
     */
    public String get(String key) throws IOException {
        StringBuffer stringBuffer=new StringBuffer();
        //GetBytes is because the key and value may be in Chinese. If you get the length directly, you will make an error
        stringBuffer.append("*2"+"\r\n")
                .append("$3"+"\r\n").append("get"+"\r\n")
                .append("$"+key.getBytes().length+"\r\n").append(key+"\r\n");
        //1. Pass the command to redis
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(stringBuffer.toString().getBytes());

        //2. Receive the response result of redis
        InputStream inputStream = socket.getInputStream();
        byte[] b=new byte[2048];
        inputStream.read(b);

        return new String(b);
    }

    /***
     *Specify key to delete
     * @param key
     * @return
     * @throws IOException
     */
    public void del(String key) throws IOException {
        StringBuffer stringBuffer=new StringBuffer();
        //GetBytes is because the key and value may be in Chinese. If you get the length directly, you will make an error
        stringBuffer.append("*2"+"\r\n")
                .append("$3"+"\r\n").append("del"+"\r\n")
                .append("$"+key.getBytes().length+"\r\n").append(key+"\r\n");
        //1. Pass the command to redis
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(stringBuffer.toString().getBytes());
        outputStream.flush();
    }

}

 

 

3、 Operation results