hiredis
#include <stdio.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>
int main(void) {
// Connect to Redis server
redisContext *ctx = redisConnect("127.0.0.1", 6379);
if (ctx == NULL || ctx->err) {
if (ctx) {
printf("Error: %s\n", ctx->errstr);
redisFree(ctx);
} else {
printf("Error: Can't allocate redis context\n");
}
exit(1);
}
// Push items from top and bottom of the deque
redisReply *reply;
reply = redisCommand(ctx, "LPUSH mydeque item1"); // Push at the front
freeReplyObject(reply);
reply = redisCommand(ctx, "RPUSH mydeque item2"); // Push at the back
freeReplyObject(reply);
// Pop items from top and bottom of the deque
reply = redisCommand(ctx, "LPOP mydeque"); // Pop from the front
if (reply && reply->type == REDIS_REPLY_STRING) {
printf("Popped item from the front: %s\n", reply->str);
}
freeReplyObject(reply);
reply = redisCommand(ctx, "RPOP mydeque"); // Pop from the back
if (reply && reply->type == REDIS_REPLY_STRING) {
printf("Popped item from the back: %s\n", reply->str);
}
freeReplyObject(reply);
// Free the Redis context
redisFree(ctx);
return 0;
}
/* Set some keys and then read them back. Once we do that, Redis will deliver
* invalidation push messages whenever the key is modified */
for (j = 0; j < KEY_COUNT; j++) {
reply = redisCommand(c, "SET key:%d initial:%d", j, j);
assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);
reply = redisCommand(c, "GET key:%d", j);
assertReplyAndFree(c, reply, REDIS_REPLY_STRING);
}
/* Trigger invalidation messages by updating keys we just read */
for (j = 0; j < KEY_COUNT; j++) {
printf(" main(): SET key:%d update:%d\n", j, j);
reply = redisCommand(c, "SET key:%d update:%d", j, j);
assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);
printf(" main(): SET REPLY OK\n");
}
```