Java 手写简陋 redis api,协议

redis 手写简陋api,协议


前言

记录redis简单的连接api


一、Redis connection 类

package redis.connection;

import redis.clients.jedis.Protocol;
import redis.protocol.Protoclol;

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

/**
 * @author xyq
 * @描述:
 * @创建时间:2021/7/2 11:17
 */
public class Connection {
    private Socket socket;
    private String host;
    private int port;
    private OutputStream outputStream;
    private InputStream inputStream;

    public Connection(String host, int port) {
        this.host = host;
        this.port = port;
    }
    public void  connect(){
        try {
            socket=new Socket(host,port);
            inputStream=socket.getInputStream();
            outputStream=socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Connection sendCommand(Protoclol.Command command,byte[] ...b) {
        connect();
        Protoclol.sendCommand(command,outputStream,b);
        return this;
    }

    public String getReply() {
        byte b[]=new byte[1024];
        try {
            socket.getInputStream().read(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(b);
    }
}

二、连接协议

*3 $3 SET $5 mykey $7 myvalue 每次以 \r\n 结尾 协议只要拼成以上的格式就行
package redis.protocol;

import org.checkerframework.checker.units.qual.K;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author xyq
 * @描述:
 * @创建时间:2021/7/2 14:21
 */
public class Protoclol {
    public static final String X="*";
    public static final String S="$";
    public static final String K="\r\n";
    public static void sendCommand(Protoclol.Command command, OutputStream outputStream,byte[]... b){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append(X).append(b.length+1).append(K);
        stringBuffer.append(S).append(command.name().length()).append(K);
        stringBuffer.append(command).append(K);
        for(byte []bytes:b){
            stringBuffer.append(S).append(bytes.length).append(K);
            stringBuffer.append(new String(bytes)).append(K);
        }
        try {
            outputStream.write(stringBuffer.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static enum Command{
        SET,GET
    }
}

三、api

代码如下(示例):

package redis.api;

import redis.connection.Connection;
import redis.protocol.Protoclol;

/**
 * @author xyq
 * @描述:
 * @创建时间:2021/7/2 11:23
 */
public class RedisConnect {
    private Connection connection;
    public RedisConnect(String host,int port){
        connection=new Connection(host,port);
    }

    public String set(final String key,final String value){
        connection.sendCommand(Protoclol.Command.SET,key.getBytes(),value.getBytes());
        return null;
    }
    public String get(final String key){
        connection.sendCommand(Protoclol.Command.GET,key.getBytes());
        return connection.getReply();
    }
}

四、测试

package redis;

import redis.api.RedisConnect;

/**
 * @author xyq
 * @描述:
 * @创建时间:2021/7/2 14:38
 */
public class RedisTest {
    public static void main(String[] args) {
        RedisConnect redisConnect=new RedisConnect("localhost",6379);
        redisConnect.set("xyq","456");
        System.out.println(redisConnect.get("xyq"));
    }
}

总结

csdn 记入美好生活(参考网上内容,总结!)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值