redis

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hiredis.h"

int main() {
    // Connect to Redis server
    redisContext *redis = redisConnect("127.0.0.1", 6379);

    // Check if the connection was successful
    if (redis == NULL || redis->err) {
        printf("Connection to Redis failed: %s\n", redis->errstr);
        exit(EXIT_FAILURE);
    }

    // Set a key-value pair in Redis
    const char *key = "my_key";
    const char *value = "Hello, Redis!";
    redisReply *reply_set = redisCommand(redis, "SET %s %s", key, value);

    // Check if the SET command was successful
    if (reply_set == NULL || reply_set->type == REDIS_REPLY_ERROR) {
        printf("SET command failed: %s\n", reply_set->str);
        freeReplyObject(reply_set);
        redisFree(redis);
        exit(EXIT_FAILURE);
    }

    // Get the value for a key from Redis
    redisReply *reply_get = redisCommand(redis, "GET %s", key);

    // Check if the GET command was successful
    if (reply_get == NULL || reply_get->type == REDIS_REPLY_ERROR) {
        printf("GET command failed: %s\n", reply_get->str);
        freeReplyObject(reply_set);
        freeReplyObject(reply_get);
        redisFree(redis);
        exit(EXIT_FAILURE);
    }

    // Print the retrieved value
    printf("Value for key '%s': %s\n", key, reply_get->str);

    // Free the memory used by the replies
    freeReplyObject(reply_set);
    freeReplyObject(reply_get);

    // Disconnect from the Redis server
    redisFree(redis);

    return 0;
}