zset.c 1.1 KB

123456789101112131415161718192021222324252627282930
  1. #include "redismodule.h"
  2. /* ZSET.REM key element
  3. *
  4. * Removes an occurrence of an element from a sorted set. Replies with the
  5. * number of removed elements (0 or 1).
  6. */
  7. int zset_rem(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  8. if (argc != 3) return RedisModule_WrongArity(ctx);
  9. RedisModule_AutoMemory(ctx);
  10. int keymode = REDISMODULE_READ | REDISMODULE_WRITE;
  11. RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], keymode);
  12. int deleted;
  13. if (RedisModule_ZsetRem(key, argv[2], &deleted) == REDISMODULE_OK)
  14. return RedisModule_ReplyWithLongLong(ctx, deleted);
  15. else
  16. return RedisModule_ReplyWithError(ctx, "ERR ZsetRem failed");
  17. }
  18. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  19. REDISMODULE_NOT_USED(argv);
  20. REDISMODULE_NOT_USED(argc);
  21. if (RedisModule_Init(ctx, "zset", 1, REDISMODULE_APIVER_1) ==
  22. REDISMODULE_OK &&
  23. RedisModule_CreateCommand(ctx, "zset.rem", zset_rem, "",
  24. 1, 1, 1) == REDISMODULE_OK)
  25. return REDISMODULE_OK;
  26. else
  27. return REDISMODULE_ERR;
  28. }