国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Golang > 正文

go語(yǔ)言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法

2020-04-01 19:15:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了go語(yǔ)言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法,實(shí)例分析了Go語(yǔ)言使用memcache的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
 

本文實(shí)例講述了go語(yǔ)言實(shí)現(xiàn)的memcache協(xié)議服務(wù)的方法。分享給大家供大家參考。具體如下:

1. Go語(yǔ)言代碼如下:

復(fù)制代碼代碼如下:
package memcachep
import (
    "bufio"
    "fmt"
    "io"
    "strconv"
    "strings"
)
//mc請(qǐng)求產(chǎn)生一個(gè)request對(duì)象
type MCRequest struct {
    //請(qǐng)求命令
    Opcode CommandCode
    //key
    Key string
    //請(qǐng)求內(nèi)容
    Value []byte
    //請(qǐng)求標(biāo)識(shí)
    Flags int
    //請(qǐng)求內(nèi)容長(zhǎng)度
    Length int
    //過(guò)期時(shí)間
    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請(qǐng)求內(nèi)容 解析為一個(gè)MCRequest對(duì)象
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語(yǔ)言代碼:
復(fù)制代碼代碼如下:
package memcachep
import (
    "fmt"
    "io"
)
type MCResponse struct {
    //命令
    Opcoed CommandCode
    //返回狀態(tài)
    Status Status
    //key
    Key string
    //返回內(nèi)容
    Value []byte
    //返回標(biāo)識(shí)
    Flags int
    //錯(cuò)誤
    Fatal bool
}
//解析response 并把返回結(jié)果寫入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語(yǔ)言代碼如下:
復(fù)制代碼代碼如下:
package memcachep
import (
    "fmt"
)
type action func(req *MCRequest, res *MCResponse)
var actions = map[CommandCode]action{
    STATS: StatsAction,
}
//等待分發(fā)處理
func waitDispatch(rc chan chanReq) {
    for {
        input := <-rc
        input.response <- dispatch(input.request)
    }
}
//分發(fā)請(qǐng)求到響應(yīng)的action操作函數(shù)上去
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)
}

 

希望本文所述對(duì)大家的Go語(yǔ)言程序設(shè)計(jì)有所幫助。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 呈贡县| 舒兰市| 昌宁县| 海林市| 班玛县| 沛县| 德钦县| 益阳市| 邛崃市| 翼城县| 佛冈县| 石家庄市| 寻甸| 沁水县| 宜阳县| 灵川县| 望谟县| 康马县| 日照市| 福鼎市| 泰顺县| 承德市| 凤翔县| 莱芜市| 始兴县| 阿荣旗| 友谊县| 绥宁县| 清原| 英吉沙县| 临沂市| 巴林右旗| 云林县| 井陉县| 华安县| 虎林市| 股票| 海门市| 新蔡县| 涪陵区| 吉木萨尔县|