-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemclient.go
301 lines (273 loc) · 7.48 KB
/
memclient.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"net"
"bufio"
"os"
"regexp"
"strconv"
"strings"
"github.com/jawher/mow.cli"
)
const (
VERSION = "0.1.0dev"
)
/*
The CommandExecuter interface defines an entity that is able to execute memcached
commands against a memcached server.
*/
type CommandExecuter interface {
execute(command string, delimiters []string) []string
Close()
}
type MemcachedCommandExecuter struct {
connection net.Conn
}
type memClient struct {
server string
executer CommandExecuter
}
type Stat struct {
name string
value string
}
func MemClient(server string) (*memClient, error) {
conn, err := net.Dial("tcp", server)
if err != nil {
return nil, err
}
return &memClient{
server: server,
executer: &MemcachedCommandExecuter{
connection: conn,
},
}, nil
}
func (client *memClient) Close() {
if client.executer != nil {
client.executer.Close()
}
}
func (executer *MemcachedCommandExecuter) execute(command string, responseDelimiters []string) []string {
fmt.Fprintf(executer.connection, command)
scanner := bufio.NewScanner(executer.connection)
var result []string
OUTER:
for scanner.Scan() {
line := scanner.Text()
for _, delimeter := range responseDelimiters {
if line == delimeter {
break OUTER
}
}
result = append(result, line)
// if there is no delimiter specified, then the response is just a single line and we should return after
// reading that first line (e.g. version command)
if len(responseDelimiters) == 0 {
break OUTER
}
}
return result
}
func (executer *MemcachedCommandExecuter) Close() {
executer.connection.Close()
}
/*
Retrieves a given cache key from the memcached server.
Returns a string array with the value and a boolean indicating whether a value was found or not.
*/
func (client *memClient) Get(key string) ([]string, bool) {
command := fmt.Sprintf("get %s\r\n", key)
result := client.executer.execute(command, []string{"END"})
if len(result) >= 2 {
// ditch the first "VALUE <key> <expiration> <length>" line
return result[1:], true
}
return []string{}, false
}
/*
Sets a given cache key on the memcached server to a given value.
*/
func (client *memClient) Set(key string, value string, expiration int) {
flags := "0" // TODO(jorisroovers): support flags
command := fmt.Sprintf("set %s %s %d %d\r\n%s\r\n", key, flags, expiration, len(value), value)
client.executer.execute(command, []string{"STORED"})
}
/*
Deletes a given cache key on the memcached server.
*/
func (client *memClient) Delete(key string) {
command := fmt.Sprintf("delete %s\r\n", key)
client.executer.execute(command, []string{"DELETED", "NOT_FOUND"})
}
/*
List all cache keys on the memcached server.
*/
func (client *memClient) ListKeys() []string {
keys := []string{}
result := client.executer.execute("stats items\r\n", []string{"END"})
// identify all slabs and their number of items by parsing the 'stats items' command
r, _ := regexp.Compile("STAT items:([0-9]*):number ([0-9]*)")
slabCounts := map[int]int{}
for _, stat := range result {
matches := r.FindStringSubmatch(stat)
if len(matches) == 3 {
slabId, _ := strconv.Atoi(matches[1])
slabItemCount, _ := strconv.Atoi(matches[2])
slabCounts[slabId] = slabItemCount
}
}
// For each slab, dump all items and add each key to the `keys` slice
r, _ = regexp.Compile("ITEM (.*?) .*")
for slabId, slabCount := range slabCounts {
command := fmt.Sprintf("stats cachedump %v %v\n", slabId, slabCount)
commandResult := client.executer.execute(command, []string{"END"})
for _, item := range commandResult {
matches := r.FindStringSubmatch(item)
keys = append(keys, matches[1])
}
}
return keys
}
/*
Get the server version.
*/
func (client *memClient) Version() string {
result := client.executer.execute("version \r\n", []string{})
if len(result) == 1 {
return result[0]
}
return "UNKNOWN"
}
/*
Retrieve all server statistics.
*/
func (client *memClient) Stats() []Stat {
result := client.executer.execute("stats\r\n", []string{"END"})
stats := []Stat{}
for _, stat := range result {
parts := strings.SplitN(stat, " ", 3)
stats = append(stats, Stat{parts[1], parts[2]})
}
return stats
}
/*
Retrieve a specific server statistic.
*/
func (client *memClient) Stat(statName string) (Stat, bool) {
stats := client.Stats()
for _, stat := range stats {
if stat.name == statName {
return stat, true
}
}
return Stat{}, false
}
func (client *memClient) Flush() {
client.executer.execute("flush_all \r\n", []string{"OK"})
}
/*
Creates a memClient and deals with any errors that might occur (e.g. unable to connect to server).
*/
func createClient(host, port *string) (*memClient) {
server := *host + ":" + *port
client, err := MemClient(server)
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to connect to", server)
os.Exit(1)
}
return client
}
/*
This is where the magic happens.
Creates a CLI app using mow.cli and to calls the appropriate functions on a Memclient instance according to the
passed parameters.
*/
func main() {
cp := cli.App("memclient", "Simple command-line client for Memcached")
cp.Version("v version", "memclient " + VERSION)
host := cp.StringOpt("host h", "localhost", "Memcached host (or IP)")
port := cp.StringOpt("port p", "11211", "Memcached port")
cp.Command("set", "Sets a key value pair", func(cmd *cli.Cmd) {
key := cmd.StringArg("KEY", "", "Key to set a value for")
value := cmd.StringArg("VALUE", "", "Value to set")
expiration := cmd.IntOpt("expire", 0, "expiration time (in seconds)")
cmd.Action = func() {
client := createClient(host, port)
client.Set(*key, *value, *expiration)
client.Close()
}
})
cp.Command("get", "Retrieves a key", func(cmd *cli.Cmd) {
key := cmd.StringArg("KEY", "", "Key to set a value for")
cmd.Action = func() {
client := createClient(host, port)
result, ok := client.Get(*key)
if ok {
for _, line := range result {
fmt.Println(line)
}
} else {
fmt.Println("[NOT FOUND]")
}
client.Close()
}
})
cp.Command("delete", "Deletes a key", func(cmd *cli.Cmd) {
key := cmd.StringArg("KEY", "", "Key to delete")
cmd.Action = func() {
client := createClient(host, port)
client.Delete(*key)
client.Close()
}
})
description := "Flush all cache keys (they will still show in 'list', but will return 'NOT FOUND')"
cp.Command("flush", description, func(cmd *cli.Cmd) {
cmd.Action = func() {
client := createClient(host, port)
client.Flush()
client.Close()
}
})
cp.Command("version", "Show server version", func(cmd *cli.Cmd) {
cmd.Action = func() {
client := createClient(host, port)
fmt.Println(client.Version())
client.Close()
}
})
cp.Command("list", "Lists all keys", func(cmd *cli.Cmd) {
cmd.Action = func() {
client := createClient(host, port)
keys := client.ListKeys()
for _, item := range keys {
fmt.Println(item)
}
client.Close()
}
})
cp.Command("stats", "Print server statistics", func(cmd *cli.Cmd) {
cmd.Action = func() {
client := createClient(host, port)
stats := client.Stats()
for _, stat := range stats {
fmt.Printf("%-25v %v\n", stat.name, stat.value)
}
client.Close()
}
})
cp.Command("stat", "Print a specific server statistic", func(cmd *cli.Cmd) {
statName := cmd.StringArg("STAT", "", "Name of the statistic to get")
cmd.Action = func() {
client := createClient(host, port)
stat, ok := client.Stat(*statName)
if ok {
fmt.Printf("%-25v %v\n", stat.name, stat.value)
} else {
fmt.Println("[NOT FOUND]")
}
client.Close()
}
})
cp.Run(os.Args)
}