static int UpdateTransformedImage(RedisModuleCtx *ctx, RedisModuleKey *img_key, const char *transformed_img, size_t img_len) { int res = REDISMODULE_ERR; // Resize the key to contain the transformed image buffer if (RedisModule_StringTruncate(img_key, img_len) == REDISMODULE_ERR) { RedisModule_ReplyWithError(ctx, "Error resizing string key"); return res; } // Get write access to the resized key char *buf = RedisModule_StringDMA(img_key, &img_len, REDISMODULE_WRITE); if (!buf) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return res; } // Write transformed blob to output memcpy(buf, transformed_img, img_len); // Setup success reply RedisModule_ReplyWithSimpleString(ctx, "OK"); res = REDISMODULE_OK; return res; }
/* This is an example of strings DMA access. Given a key containing a string * it toggles the case of each character from lower to upper case or the * other way around. * * No automatic memory management is used in this example (for the sake * of variety). * * HELLO.TOGGLE.CASE key */ int HelloToggleCase_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { if (argc != 2) return RedisModule_WrongArity(ctx); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); int keytype = RedisModule_KeyType(key); if (keytype != REDISMODULE_KEYTYPE_STRING && keytype != REDISMODULE_KEYTYPE_EMPTY) { RedisModule_CloseKey(key); return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE); } if (keytype == REDISMODULE_KEYTYPE_STRING) { size_t len, j; char *s = RedisModule_StringDMA(key,&len,REDISMODULE_WRITE); for (j = 0; j < len; j++) { if (isupper(s[j])) { s[j] = tolower(s[j]); } else { s[j] = toupper(s[j]); } } } RedisModule_CloseKey(key); RedisModule_ReplyWithSimpleString(ctx,"OK"); RedisModule_ReplicateVerbatim(ctx); return REDISMODULE_OK; }
/* HELLOTIMER.TIMER*/ int TimerCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { REDISMODULE_NOT_USED(argv); REDISMODULE_NOT_USED(argc); for (int j = 0; j < 10; j++) { int delay = rand() % 5000; char *buf = RedisModule_Alloc(256); snprintf(buf,256,"After %d", delay); RedisModuleTimerID tid = RedisModule_CreateTimer(ctx,delay,timerHandler,buf); REDISMODULE_NOT_USED(tid); } return RedisModule_ReplyWithSimpleString(ctx, "OK"); }
int TestModule(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* TODO: calling flushall but checking only for db 0. */ RedisModuleCallReply *r = RedisModule_Call(ctx, "DBSIZE", ""); if (RedisModule_CallReplyInteger(r) != 0) { RedisModule_ReplyWithError(ctx, "ERR test must be run on an empty instance"); return REDISMODULE_ERR; } RMUtil_Test(testMSIsMember); RedisModule_ReplyWithSimpleString(ctx, "PASS"); return REDISMODULE_OK; }
int GMGetTypeCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { int res = REDISMODULE_ERR; if (argc != 2) { return RedisModule_WrongArity(ctx); } // init auto memory for created strings RedisModule_AutoMemory(ctx); // open the key RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ); // If key doesn't exist then return immediately if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) { RedisModule_ReplyWithError(ctx, "empty key"); return REDISMODULE_OK; } // Validate key is a string if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_STRING) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return REDISMODULE_ERR; } // Get access to the image size_t key_len; char *buf = RedisModule_StringDMA(key, &key_len, REDISMODULE_READ); if (!buf) { RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); return REDISMODULE_ERR; } struct GoGetImgType_return r = GoGetImgType(buf, key_len); if (r.r3 != 0) { RedisModule_ReplyWithError(ctx, r.r2); free(r.r0); free(r.r2); return REDISMODULE_ERR; } RedisModule_ReplyWithSimpleString(ctx, r.r0); free(r.r0); free(r.r2); return REDISMODULE_OK; }
/* HELLO.MORE.EXPIRE key milliseconds. * * If they key has already an associated TTL, extends it by "milliseconds" * milliseconds. Otherwise no operation is performed. */ int HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModule_AutoMemory(ctx); /* Use automatic memory management. */ if (argc != 3) return RedisModule_WrongArity(ctx); mstime_t addms, expire; if (RedisModule_StringToLongLong(argv[2],&addms) != REDISMODULE_OK) return RedisModule_ReplyWithError(ctx,"ERR invalid expire time"); RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1], REDISMODULE_READ|REDISMODULE_WRITE); expire = RedisModule_GetExpire(key); if (expire != REDISMODULE_NO_EXPIRE) { expire += addms; RedisModule_SetExpire(key,expire); } return RedisModule_ReplyWithSimpleString(ctx,"OK"); }