Skip to content

Commit 56b1fbd

Browse files
committed
Add AoF routines
1 parent 6a3b796 commit 56b1fbd

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

rmutil/util.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,19 @@ RedisModuleString **RMUtil_ParseVarArgs(RedisModuleString **argv, int argc, int
275275
*nargs = n;
276276
return argv + 1;
277277
}
278+
279+
void RMUtil_DefaultAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value) {
280+
RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
281+
RedisModuleCallReply *rep = RedisModule_Call(ctx, "DUMP", "s", key);
282+
if (rep != NULL && RedisModule_CallReplyType(rep) == REDISMODULE_REPLY_STRING) {
283+
size_t n;
284+
const char *s = RedisModule_CallReplyStringPtr(rep, &n);
285+
RedisModule_EmitAOF(aof, "RESTORE", "slb", key, 0, s, n);
286+
} else {
287+
RedisModule_Log(RedisModule_GetContextFromIO(aof), "warning", "Failed to emit AOF");
288+
}
289+
if (rep != NULL) {
290+
RedisModule_FreeCallReply(rep);
291+
}
292+
RedisModule_FreeThreadSafeContext(ctx);
293+
}

rmutil/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ int rmutil_vparseArgs(RedisModuleString **argv, int argc, int offset, const char
7272
RedisModuleString **RMUtil_ParseVarArgs(RedisModuleString **argv, int argc, int offset,
7373
const char *keyword, size_t *nargs);
7474

75+
/**
76+
* Default implementation of an AoF rewrite function that simply calls DUMP/RESTORE
77+
* internally. To use this function, pass it as the .aof_rewrite value in
78+
* RedisModuleTypeMethods
79+
*/
80+
void RMUtil_DefaultAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value);
81+
7582
// A single key/value entry in a redis info map
7683
typedef struct {
7784
const char *key;

0 commit comments

Comments
 (0)