您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关go语言中怎么实现一个memcache协议服务,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
1. Go语言代码如下:
package memcachep import ( "bufio" "fmt" "io" "strconv" "strings" ) //mc请求产生一个request对象 type MCRequest struct { //请求命令 Opcode CommandCode //key Key string //请求内容 Value []byte //请求标识 Flags int //请求内容长度 Length int //过期时间 Expires int64 } //request to string func (req *MCRequest) String() string { return fmt.Sprintf("{MCRequest opcode=%s, bodylen=%d, key='%s'}", req.Opcode, len(req.Value), req.Key) } //将socket请求内容 解析为一个MCRequest对象 func (req *MCRequest) Receive(r *bufio.Reader) error { line, _, err := r.ReadLine() if err != nil || len(line) == 0 { return io.EOF } params := strings.Fields(string(line)) command := CommandCode(params[0]) switch command { case SET, ADD, REPLACE: req.Opcode = command req.Key = params[1] req.Length, _ = strconv.Atoi(params[4]) value := make([]byte, req.Length+2) io.ReadFull(r, value) req.Value = make([]byte, req.Length) copy(req.Value, value) case GET: req.Opcode = command req.Key = params[1] RunStats["cmd_get"].(*CounterStat).Increment(1) case STATS: req.Opcode = command req.Key = "" case DELETE: req.Opcode = command req.Key = params[1] } return err }
2. Go语言代码:
package memcachep import ( "fmt" "io" ) type MCResponse struct { //命令 Opcoed CommandCode //返回状态 Status Status //key Key string //返回内容 Value []byte //返回标识 Flags int //错误 Fatal bool } //解析response 并把返回结果写入socket链接 func (res *MCResponse) Transmit(w io.Writer) (err error) { switch res.Opcoed { case STATS: _, err = w.Write(res.Value) case GET: if res.Status == SUCCESS { rs := fmt.Sprintf("VALUE %s %d %d\r\n%s\r\nEND\r\n", res.Key, res.Flags, len(res.Value), res.Value) _, err = w.Write([]byte(rs)) } else { _, err = w.Write([]byte(res.Status.ToString())) } case SET, REPLACE: _, err = w.Write([]byte(res.Status.ToString())) case DELETE: _, err = w.Write([]byte("DELETED\r\n")) } return }
3. Go语言代码如下:
package memcachep import ( "fmt" ) type action func(req *MCRequest, res *MCResponse) var actions = map[CommandCode]action{ STATS: StatsAction, } //等待分发处理 func waitDispatch(rc chan chanReq) { for { input := <-rc input.response <- dispatch(input.request) } } //分发请求到响应的action操作函数上去 func dispatch(req *MCRequest) (res *MCResponse) { if h, ok := actions[req.Opcode]; ok { res = &MCResponse{} h(req, res) } else { return notFound(req) } return } //未支持命令 func notFound(req *MCRequest) *MCResponse { var response MCResponse response.Status = UNKNOWN_COMMAND return &response } //给request绑定上处理程序 func BindAction(opcode CommandCode, h action) { actions[opcode] = h } //stats func StatsAction(req *MCRequest, res *MCResponse) { res.Fatal = false stats := "" for key, value := range RunStats { stats += fmt.Sprintf("STAT %s %s\r\n", key, value) } stats += "END\r\n" res.Value = []byte(stats) }
上述就是小编为大家分享的go语言中怎么实现一个memcache协议服务了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。