go语言实现的memcache协议服务的方法

memcache
这篇文章主要介绍了go语言实现的memcache协议服务的方法,实例分析了Go语言使用memcache的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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 (
    "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

3. 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

希望本文所述对大家的Go语言程序设计有所帮助。