-
Notifications
You must be signed in to change notification settings - Fork 68
/
mc_storage.go
92 lines (79 loc) · 2.22 KB
/
mc_storage.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/binary"
"github.com/dustin/gomemcached"
"log"
)
type storage struct {
data map[string]gomemcached.MCItem
cas uint64
}
type handler func(req *gomemcached.MCRequest, s *storage) *gomemcached.MCResponse
var handlers = map[gomemcached.CommandCode]handler{
gomemcached.SET: handleSet,
gomemcached.GET: handleGet,
gomemcached.DELETE: handleDelete,
gomemcached.FLUSH: handleFlush,
}
func RunServer(input chan chanReq) {
var s storage
s.data = make(map[string]gomemcached.MCItem)
for {
req := <-input
log.Printf("Got a request: %s", req.req)
req.res <- dispatch(req.req, &s)
}
}
func dispatch(req *gomemcached.MCRequest, s *storage) (rv *gomemcached.MCResponse) {
if h, ok := handlers[req.Opcode]; ok {
rv = h(req, s)
} else {
return notFound(req, s)
}
return
}
func notFound(req *gomemcached.MCRequest, s *storage) *gomemcached.MCResponse {
var response gomemcached.MCResponse
response.Status = gomemcached.UNKNOWN_COMMAND
return &response
}
func handleSet(req *gomemcached.MCRequest, s *storage) (ret *gomemcached.MCResponse) {
ret = &gomemcached.MCResponse{}
var item gomemcached.MCItem
item.Flags = binary.BigEndian.Uint32(req.Extras)
item.Expiration = binary.BigEndian.Uint32(req.Extras[4:])
item.Data = req.Body
ret.Status = gomemcached.SUCCESS
s.cas += 1
item.Cas = s.cas
ret.Cas = s.cas
s.data[string(req.Key)] = item
return
}
func handleGet(req *gomemcached.MCRequest, s *storage) (ret *gomemcached.MCResponse) {
ret = &gomemcached.MCResponse{}
if item, ok := s.data[string(req.Key)]; ok {
ret.Status = gomemcached.SUCCESS
ret.Extras = make([]byte, 4)
binary.BigEndian.PutUint32(ret.Extras, item.Flags)
ret.Cas = item.Cas
ret.Body = item.Data
} else {
ret.Status = gomemcached.KEY_ENOENT
}
return
}
func handleFlush(req *gomemcached.MCRequest, s *storage) (ret *gomemcached.MCResponse) {
ret = &gomemcached.MCResponse{}
delay := binary.BigEndian.Uint32(req.Extras)
if delay > 0 {
log.Printf("Delay not supported (got %d)", delay)
}
s.data = make(map[string]gomemcached.MCItem)
return
}
func handleDelete(req *gomemcached.MCRequest, s *storage) (ret *gomemcached.MCResponse) {
ret = &gomemcached.MCResponse{}
delete(s.data, string(req.Key))
return
}