Skip to content

Commit 26a47d6

Browse files
authored
Merge pull request #4 from flightaware/BCK-2485
BCK 2485 - add 'server clear' command
2 parents 3b456f5 + c56e32d commit 26a47d6

File tree

5 files changed

+97
-23
lines changed

5 files changed

+97
-23
lines changed

Makefile.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ TCL_SRC_DIR = @TCL_SRC_DIR@
116116
#TK_BIN_DIR = @TK_BIN_DIR@
117117
#TK_SRC_DIR = @TK_SRC_DIR@
118118

119+
# If this isn't set, resetting it later on will break things
120+
@LD_LIBRARY_PATH_VAR@ ?= /usr/lib:/usr/local/lib
121+
119122
# Not used, but retained for reference of what libs Tcl required
120123
#TCL_LIBS = @TCL_LIBS@
121124

README.FreeBSD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
pkg install databases/libmemcached
1+
#pkg install databases/libmemcached
2+
echo install [email protected]:awesomized/libmemcached.git
23

34
env CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib ./configure --with-tcl=/usr/local/lib/tcl8.6
45

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ The actual memcached server installation is independent of the
1414
installation of this client package and is not addressed by this
1515
document.
1616

17+
The underlying libmemcached installation recommended is awesomized/libmemcached
18+
because the official libmemcached has not had a release since 2014 and there are
19+
crashing bugs that have had fixes provided that haven't been rolled up into a release.
1720

1821
On FreeBSD:
19-
* pkg install databases/libmemcached
22+
* install [email protected]:awesomized/libmemcached.git
2023
* env CPPFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib ./configure --with-tcl=/usr/local/lib/tcl8.6
2124
* make
2225
* make install
@@ -54,12 +57,16 @@ on success or some other integer error. If the returned value is
5457
non-zero then the request failed, and you should not expect any
5558
varname arguments to have been modified.
5659

60+
Use `memcache strerror` to get a human readable version of the error code.
61+
5762

5863
Available Commands
5964
------------------
6065

6166
memcache server add hostname port
6267

68+
memcache server clear
69+
6370
memcache get key varname ?lengthVar? ?flagsVar?
6471

6572
memcache add key value ?expires? ?flags?
@@ -83,3 +90,5 @@ Available Commands
8390
memcache version
8491

8592
memcache behavior flagname ?flagvalue?
93+
94+
memcache strerror errorcode

generic/tclMemcache.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,20 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
6161
uint32_t expires = 0;
6262
uint64_t size64;
6363
int cmd;
64+
int errorcode;
6465

6566

6667
// list of supported commands that we expose.
6768
enum {
6869
cmdGet, cmdAdd, cmdAppend, cmdPrepend, cmdSet, cmdReplace,
69-
cmdDelete, cmdFlush, cmdIncr, cmdDecr, cmdVersion, cmdServer, cmdBehavior
70+
cmdDelete, cmdFlush, cmdIncr, cmdDecr, cmdVersion, cmdServer, cmdBehavior,
71+
cmdStringError
7072
};
7173

7274
static CONST char *sCmd[] = {
7375
"get", "add", "append", "prepend", "set", "replace",
7476
"delete", "flush", "incr", "decr", "version", "server", "behavior",
77+
"strerror",
7578
0
7679
};
7780

@@ -132,22 +135,30 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
132135
case cmdServer:
133136
/*
134137
* Server list manipulation:
135-
* - server add hostname port
136-
* - memcache server delete hostname port
138+
* - memcache server add hostname port
139+
* - memcache server clear
137140
*/
138-
if (objc != 5) {
139-
Tcl_WrongNumArgs(interp, 2, objv, "cmd server port");
141+
if (objc < 3) {
142+
Tcl_WrongNumArgs(interp, 2, objv, "(add|clear) ...");
140143
return TCL_ERROR;
141144
}
142145
if (!strcmp(Tcl_GetString(objv[2]), "add")) {
146+
// adds a TCP memcache server
147+
if (objc != 5) {
148+
Tcl_WrongNumArgs(interp, 2, objv, "add hostname port");
149+
return TCL_ERROR;
150+
}
143151
result = memcached_server_add(get_memc(), Tcl_GetString(objv[3]), atoi(Tcl_GetString(objv[4])));
144-
} else if (!strcmp(Tcl_GetString(objv[2]), "delete")) {
145-
// TODO: not supported
146-
//mc_server_delete(mc, mc_server_find(mc, Tcl_GetString(objv[3]), 0));
147-
Tcl_AppendResult(interp, "server delete not supported.", NULL);
148-
return TCL_ERROR;
152+
} else if (!strcmp(Tcl_GetString(objv[2]), "clear")) {
153+
// clear the entire memcache server list.
154+
if (objc != 3) {
155+
Tcl_WrongNumArgs(interp, 2, objv, "clear");
156+
return TCL_ERROR;
157+
}
158+
memcached_servers_reset(get_memc());
159+
result = 0;
149160
} else {
150-
Tcl_AppendResult(interp, "server command not recognized.", NULL);
161+
Tcl_AppendResult(interp, "server subcommand not recognized.", NULL);
151162
return TCL_ERROR;
152163
}
153164
Tcl_SetObjResult(interp, Tcl_NewIntObj(result));
@@ -293,16 +304,16 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
293304
switch (cmd) {
294305
case cmdIncr:
295306
if (objc > 5) {
296-
result = memcached_increment_with_initial(get_memc(), key, strlen(key), size, size64, expires, &size64);
307+
result = memcached_increment_with_initial(get_memc(), key, strlen(key), size, size64, expires, &size64);
297308
} else {
298-
result = memcached_increment(get_memc(), key, strlen(key), size, &size64);
309+
result = memcached_increment(get_memc(), key, strlen(key), size, &size64);
299310
}
300311
break;
301312
case cmdDecr:
302313
if (objc > 5) {
303-
result = memcached_decrement_with_initial(get_memc(), key, strlen(key), size, size64, expires, &size64);
314+
result = memcached_decrement_with_initial(get_memc(), key, strlen(key), size, size64, expires, &size64);
304315
} else {
305-
result = memcached_decrement(get_memc(), key, strlen(key), size, &size64);
316+
result = memcached_decrement(get_memc(), key, strlen(key), size, &size64);
306317
}
307318
break;
308319
}
@@ -341,6 +352,20 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
341352
uint64_t currentVal = memcached_behavior_get(get_memc(), cmd);
342353
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(currentVal));
343354
}
355+
case cmdStringError:
356+
/*
357+
* Return the string associated with a libmemcached error code.
358+
*
359+
* - memcached strerror integer
360+
*/
361+
if (objc != 3) {
362+
Tcl_WrongNumArgs(interp, 2, objv, "errorcode");
363+
return TCL_ERROR;
364+
}
365+
if (Tcl_GetIntFromObj(interp, objv[2], &errorcode) != TCL_OK) {
366+
return TCL_ERROR;
367+
}
368+
Tcl_SetResult(interp, memcached_strerror(get_memc(), errorcode), TCL_VOLATILE);
344369
}
345370
return TCL_OK;
346371
}

tests/all.tcl

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,57 @@
22
package require Memcache
33
#load ./memcache[info sharedlibextension]
44

5-
memcache server add localhost 11211
6-
memcache set moo "cows go moo"
7-
memcache get moo value
5+
set result [memcache server add localhost 11211]
6+
if {$result} {
7+
puts "memcache server add: [memcache strerror $result]"
8+
exit 1
9+
}
10+
11+
# Clear server list just to make sure.
12+
set result [memcache server clear]
13+
if {$result} {
14+
puts "memcache server clear: [memcache strerror $result]"
15+
exit 1
16+
}
17+
18+
# add again
19+
set result [memcache server add localhost 11211]
20+
if {$result} {
21+
puts "memcache server add: [memcache strerror $result]"
22+
exit 1
23+
}
24+
25+
# actually test something
26+
set result [memcache set moo "cows go moo"]
27+
if {$result} {
28+
puts "memcache set: [memcache strerror $result]"
29+
exit 1
30+
}
31+
set result [memcache get moo value]
32+
if {$result} {
33+
puts "memcache get: [memcache strerror $result]"
34+
exit 1
35+
}
836
if {$value != "cows go moo"} {
937
puts "Error. value=$value!\n";
1038
exit 1
1139
}
1240

1341
set value "Boeing 777-200 (طائرة نفاثة ثنائية المحرك)"
1442

15-
memcache set unicodeTest $value
16-
memcache get unicodeTest newvalue
43+
set result [memcache set unicodeTest $value]
44+
if {$result} {
45+
puts "memcache set: [memcache strerror $result]"
46+
exit 1
47+
}
48+
set result [memcache get unicodeTest newvalue]
49+
if {$result} {
50+
puts "memcache set: [memcache strerror $result]"
51+
exit 1
52+
}
1753
if {$value != $newvalue} {
1854
puts "Error. newvalue=$newvalue!\n";
19-
exit 1
55+
exit 1
2056
}
2157

2258
puts "Success"

0 commit comments

Comments
 (0)