summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-03-06 16:24:44 -0800
committerantirez <antirez@gmail.com>2015-03-06 16:24:44 -0800
commit9545957a67a4b51cfede58e6e42f9a9c0d89a029 (patch)
tree03609e72edff79150a92939df785a9d6322d0b55
parent2bf27c3361b8524bb76d77f50e4e7e0f2da0fcd6 (diff)
downloadredis-multi-if.tar.gz
Scripting: native lua.exists() implementation.multi-if
This was a test to check how much faster native implementation would be. In initial tests it does not look like is this huge win. After all this is at least in part obvious. Now scripting.c tries to avoid allocations of argument vectors, and turning ":1" accumulated in the client buffer into a Lua type is a fast operation.
-rw-r--r--src/scripting.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/scripting.c b/src/scripting.c
index c5dd4e718..8a7954cdf 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -458,6 +458,35 @@ int luaRedisPCallCommand(lua_State *lua) {
return luaRedisGenericCommand(lua,0);
}
+int luaExistsCommand(lua_State *lua) {
+ int argc = lua_gettop(lua);
+ redisClient *c = server.lua_client;
+ char *obj_s;
+ size_t obj_len;
+ robj *key, *val;
+
+ /* Require at least one argument */
+ if (argc != 1) {
+ luaPushError(lua,
+ "redis.exists() needs exactly one argument");
+ return 1;
+ }
+
+ obj_s = (char*)lua_tolstring(lua,1,&obj_len);
+ if (obj_s == NULL) {
+ luaPushError(lua,
+ "redis.exists() argument must be a string");
+ return 1;
+ }
+
+ key = createStringObject(obj_s,obj_len);
+ val = lookupKeyRead(c->db,key);
+ decrRefCount(key);
+
+ lua_pushnumber(lua,val != NULL);
+ return 1;
+}
+
/* This adds redis.sha1hex(string) to Lua scripts using the same hashing
* function used for sha1ing lua scripts. */
int luaRedisSha1hexCommand(lua_State *lua) {
@@ -664,6 +693,11 @@ void scriptingInit(void) {
lua_pushcfunction(lua,luaRedisPCallCommand);
lua_settable(lua,-3);
+ /* redis.exists */
+ lua_pushstring(lua,"exists");
+ lua_pushcfunction(lua,luaExistsCommand);
+ lua_settable(lua,-3);
+
/* redis.log and log levels. */
lua_pushstring(lua,"log");
lua_pushcfunction(lua,luaLogCommand);