@@ -61,17 +61,20 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
61
61
uint32_t expires = 0 ;
62
62
uint64_t size64 ;
63
63
int cmd ;
64
+ int errorcode ;
64
65
65
66
66
67
// list of supported commands that we expose.
67
68
enum {
68
69
cmdGet , cmdAdd , cmdAppend , cmdPrepend , cmdSet , cmdReplace ,
69
- cmdDelete , cmdFlush , cmdIncr , cmdDecr , cmdVersion , cmdServer , cmdBehavior
70
+ cmdDelete , cmdFlush , cmdIncr , cmdDecr , cmdVersion , cmdServer , cmdBehavior ,
71
+ cmdStringError
70
72
};
71
73
72
74
static CONST char * sCmd [] = {
73
75
"get" , "add" , "append" , "prepend" , "set" , "replace" ,
74
76
"delete" , "flush" , "incr" , "decr" , "version" , "server" , "behavior" ,
77
+ "strerror" ,
75
78
0
76
79
};
77
80
@@ -132,22 +135,30 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
132
135
case cmdServer :
133
136
/*
134
137
* Server list manipulation:
135
- * - server add hostname port
136
- * - memcache server delete hostname port
138
+ * - memcache server add hostname port
139
+ * - memcache server clear
137
140
*/
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) ... " );
140
143
return TCL_ERROR ;
141
144
}
142
145
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
+ }
143
151
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 ;
149
160
} else {
150
- Tcl_AppendResult (interp , "server command not recognized." , NULL );
161
+ Tcl_AppendResult (interp , "server subcommand not recognized." , NULL );
151
162
return TCL_ERROR ;
152
163
}
153
164
Tcl_SetObjResult (interp , Tcl_NewIntObj (result ));
@@ -293,16 +304,16 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
293
304
switch (cmd ) {
294
305
case cmdIncr :
295
306
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 );
297
308
} else {
298
- result = memcached_increment (get_memc (), key , strlen (key ), size , & size64 );
309
+ result = memcached_increment (get_memc (), key , strlen (key ), size , & size64 );
299
310
}
300
311
break ;
301
312
case cmdDecr :
302
313
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 );
304
315
} else {
305
- result = memcached_decrement (get_memc (), key , strlen (key ), size , & size64 );
316
+ result = memcached_decrement (get_memc (), key , strlen (key ), size , & size64 );
306
317
}
307
318
break ;
308
319
}
@@ -341,6 +352,20 @@ static int Memcache_Cmd(ClientData arg, Tcl_Interp * interp, int objc, Tcl_Obj *
341
352
uint64_t currentVal = memcached_behavior_get (get_memc (), cmd );
342
353
Tcl_SetObjResult (interp , Tcl_NewWideIntObj (currentVal ));
343
354
}
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 );
344
369
}
345
370
return TCL_OK ;
346
371
}
0 commit comments