本文实例讲述了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语言程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 张琍敏1978-雪中莲[台湾复刻版][WAV+CUE]
- 叶蕴仪1993-睡美人[日本版][WAV+CUE]
- 夜晚助兴音乐-群星《新时代床头音乐-性能量》2CD[WAV]
- 24K德国HD金碟《历届奥斯卡获奖金曲》3CD[WAV整轨]
- 邰正宵《重燃爱恋 贰 Walk On》[FLAC/分轨][431.72MB]
- 苏文劭《雨停出来走走》[320K/MP3][81.11MB]
- 苏文劭《雨停出来走走》[FLAC/分轨][210.76MB]
- 群星《2024好听新歌04》十倍音质WAV分轨
- 陈宁《弹指之间HQ》头版限量[低速原抓WAV+CUE]
- 陈宁《故人还》HQCDII限量签名版[低速原抓WAV+CUE]
- 苏文劭《春曲(Lessons)》[320K/MP3][39.8MB]
- 苏文劭《春曲(Lessons)》[FLAC/分轨][97.78MB]
- 群星《2006香港高级视听展原音精选 SACD》[ISO][2G]
- 张琍敏1977-枫林小雨[台湾复刻版][WAV+CUE]
- 林一峰2014-COOKINGMUSIC[香港首版][WAV+CUE]