golang 从零实现一个redis框架(一)最简单的redis客户端

目的

通过搭建一个高可用的redis客户端来学习redis,go语言,以及go一些设计模式

参考资料

go-redis源码

redis-3.0源码

《redis设计与实现》

学习思路

循环渐进,从最简单的一步步迭代

一个最简单的能跑通的redis客户端例子

package main

import (
        "fmt"
        "net"
        "strings"
)

type options struct {
        Network string
        Addr    string
}

func (p *options) init() {
        if p.Addr == "" {
                p.Addr = "127.0.0.1:6379"
        }
        if p.Network == "" {
                p.Network = "tcp"
        }
}

//redis struct
type redisClient struct {
        opt     options
        conn    net.Conn
        sendCmd string
        reply   string
}

func (c *redisClient) connect() {
        conn, err := net.Dial(c.opt.Network, c.opt.Addr)
        if err != nil {
                fmt.Println("connect err ", err)
        }
        c.conn = conn
}

func (c *redisClient) sendCommand(cmd string) string {
        c.formatCommand(cmd)
        c.write()
        return c.getReply()
}

func (c *redisClient) formatCommand(cmd string) {
        var protocl_cmd string
        cmd_argv := strings.Fields(cmd)
        protocl_cmd = fmt.Sprintf("*%d\r\n", len(cmd_argv))
        for _, arg := range cmd_argv {
                protocl_cmd += fmt.Sprintf("$%d\r\n", len(arg))
                protocl_cmd += arg
                protocl_cmd += "\r\n"
        }
        //redis服务器接受的命令协议
        fmt.Printf("%q\n", protocl_cmd)
        c.sendCmd = protocl_cmd
}
func (c *redisClient) write() {
        c.conn.Write([]byte(c.sendCmd))
        c.sendCmd = ""
}

func (c redisClient) getReply() string {
        byte_len := 1024 * 16
        reply := make([]byte, byte_len)
        //暂时简化一次输出完
        c.conn.Read(reply)
        return string(reply)
}

func NewClient(opt options) *redisClient {
        c := redisClient{
                opt: opt,
        }
        c.opt.init()
        c.connect()
        return &c
}

func main() {

        c := NewClient(options{
                Addr: "127.0.0.1:6379",
        })
        rep := c.sendCommand("set hello world")
        fmt.Println(rep)
}

上面这个例子,暂时没考虑读写循环,错误处理,主要实现了发送redis服务器命令协议内容,以及客户端连接

猜你喜欢

转载自blog.csdn.net/u014270740/article/details/89929972
今日推荐