forked from xiaojiong/memcachep
/
request.go
67 lines (58 loc) · 1.28 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
}