• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > go语言实现的memcache协议服务的方法

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了go语言,go语言环境搭建,go语言程序设计,let it go 25种语言,go语言教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了go语言实现的memcache协议服务的方法。分享给大家供大家参考。具体如下:

完整实例代码点击此处本站下载。

1. Go语言代码如下:
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
}</div>
2. Go语言代码:
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
}</div>
3. Go语言代码如下:
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)
}</div>

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

</div>

您可能想查找下面的文章:

  • 利用Go语言搭建WebSocket服务端方法示例
  • Go语言字典(map)用法实例分析【创建,填充,遍历,查找,修改,删除】
  • Go语言中更优雅的错误处理
  • Go语言实现的排列组合问题实例(n个数中取m个)
  • Go语言Cookie用法分析
  • GO语言运行环境下载、安装、配置图文教程
  • go语言文件正则表达式搜索功能示例
  • Go语言正则表达式用法实例小结【查找、匹配、替换等】
  • Go语言正则表达式示例
  • Go语言中三种不同md5计算方式的性能比较

相关文章

  • 服务器端Go程序对长短链接的处理及运行参数的保存
  • 创建第一个Go语言程序Hello,Go!
  • 深入解析golang编程中函数的用法
  • go语言实现通过FTP库自动上传web日志
  • Go语言通过Luhn算法验证信用卡卡号是否有效的方法
  • GO语言数组和切片实例详解
  • Go语言操作redis用法实例
  • go语言里包的用法实例
  • golang struct扩展函数参数命名警告解决方法
  • GO语言基本类型分析

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • go语言实现抓取高清图片
    • go语言对文件按照指定块大小进行分割的方法
    • Go语言实现类似c++中的多态功能实例
    • Go语言编程中字符串切割方法小结
    • GO语言标准错误处理机制error用法实例
    • Go语言interface详解
    • Golang正整数指定规则排序算法问题分析
    • Go语言多值替换的HTML模板实例分析
    • Go语言通过http抓取网页的方法
    • Go语言结构体定义和使用方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有