int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body) {
sds funcdef = sdsempty();
- lua_pop(lua,1); /* remove the nil from the stack */
funcdef = sdscat(funcdef,"function ");
funcdef = sdscatlen(funcdef,funcname,42);
funcdef = sdscatlen(funcdef," ()\n",4);
/* Try to lookup the Lua function */
lua_getglobal(lua, funcname);
if (lua_isnil(lua,1)) {
+ lua_pop(lua,1); /* remove the nil from the stack */
/* Function not defined... let's define it if we have the
* body of the funciton. If this is an EVALSHA call we can just
* return an error. */
if (evalsha) {
addReply(c, shared.noscripterr);
- lua_pop(lua,1); /* remove the nil from the stack */
return;
}
if (luaCreateFunction(c,lua,funcname,c->argv[1]) == REDIS_ERR) return;
addReply(c,shared.czero);
}
} else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"load")) {
- /* We obtain the script SHA1, then check if this function is already
- * defined into the Lua state */
char funcname[43];
+ sds sha;
funcname[0] = 'f';
funcname[1] = '_';
hashScript(funcname+2,c->argv[2]->ptr,sdslen(c->argv[2]->ptr));
- if (luaCreateFunction(c,server.lua,funcname,c->argv[2]) == REDIS_ERR)
- return;
+ sha = sdsnewlen(funcname+2,40);
+ if (dictFind(server.lua_scripts,sha) == NULL) {
+ if (luaCreateFunction(c,server.lua,funcname,c->argv[2])
+ == REDIS_ERR) {
+ sdsfree(sha);
+ return;
+ }
+ }
+ sdsfree(sha);
addReply(c,shared.ok);
} else {
addReplyError(c, "Unknown SCRIPT subcommand or wrong # of args.");
r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0
} {myval}
+ test {EVALSHA - Do we get an error on invalid SHA1?} {
+ catch {r evalsha NotValidShaSUM 0} e
+ set _ $e
+ } {NOSCRIPT*}
+
test {EVALSHA - Do we get an error on non defined SHA1?} {
- catch {r evalsha ffffffffffffffffffffffffffffffffffffffff 0} e
+ catch {r evalsha ffd632c7d33e571e9f24556ebed26c3479a87130 0} e
set _ $e
} {NOSCRIPT*}
} e
set e
} {*against a key*}
+
+ test {SCRIPTING FLUSH - is able to clear the scripts cache?} {
+ r set mykey myval
+ set v [r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0]
+ assert_equal $v myval
+ set e ""
+ r script flush
+ catch {r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0} e
+ set e
+ } {NOSCRIPT*}
+
+ test {SCRIPT EXISTS - can detect already defined scripts?} {
+ r eval "return 1+1" 0
+ r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda
+ } {1 0}
+
+ test {SCRIPT LOAD - is able to register scripts in the scripting cache} {
+ list \
+ [r script load "return 'loaded'"] \
+ [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0]
+ } {OK loaded}
}
start_server {tags {"scripting repl"}} {