/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>

#include <hiredis/hiredis.h>

static void test(redisContext *cache)
{
    char *vkey = "v", *counterkey = "counter";
    int vval, vvallen, counterval, countervallen, i;
    redisReply *reply, *reply2;
    /* basic put, get and remove */
    vval = 123;
    reply = redisCommand(cache, "SET %s %b", vkey, &vval, sizeof(vval));
    if (reply == NULL)
    {
        printf("set failed: %s\n", cache->errstr);
        exit(1);
    }
    freeReplyObject(reply);
    reply = redisCommand(cache, "GET %s", vkey);
    if (reply == NULL)
    {
        printf("get failed: %s\n", cache->errstr);
        exit(1);
    }
    vval = *((int *)reply->str);
    printf("%d\n", vval);
    freeReplyObject(reply);
    reply = redisCommand(cache, "DEL %s", vkey);
    if (reply == NULL)
    {
        printf("delete failed: %s\n", cache->errstr);
        exit(1);
    }
    freeReplyObject(reply);
    reply = redisCommand(cache, "GET %s", vkey);
    if (reply == NULL)
    {
        printf("get failed: %s\n", cache->errstr);
        exit(1);
    }
    if(reply->type == REDIS_REPLY_STRING)
    {
        vval = *((int *)reply->str);
        printf("%d\n", vval);
    }
    else
    {
        printf("<not found>\n");
    }
    freeReplyObject(reply);
    vval = 123;
    reply = redisCommand(cache, "SET %s %b", vkey, &vval, sizeof(vval));
    if (reply == NULL)
    {
        printf("set failed: %s\n", cache->errstr);
        exit(1);
    }
    freeReplyObject(reply);
    reply = redisCommand(cache, "GET %s", vkey);
    if (reply == NULL)
    {
        printf("get failed: %s\n", cache->errstr);
        exit(1);
    }
    vval = *((int *)reply->str);
    printf("%d\n", vval);
    freeReplyObject(reply);
    /* persistence */
    reply = redisCommand(cache, "GET %s", counterkey);
    if (reply == NULL)
    {
        printf("get failed: %s\n", cache->errstr);
        exit(1);
    }
    if(reply->type == REDIS_REPLY_STRING)
    {
        counterval = *((int *)reply->str);
    }
    else
    {
        counterval = 0;
    }
    freeReplyObject(reply);
    counterval++;
    printf("%d\n", counterval);
    reply = redisCommand(cache, "SET %s %b", counterkey, &counterval, sizeof(counterval));
    if (reply == NULL)
    {
        printf("set failed: %s\n", cache->errstr);
        exit(1);
    }
    freeReplyObject(reply);
	/* list all */
    reply = redisCommand(cache, "KEYS *");
    if (reply == NULL)
    {
        printf("keys failed: %s\n", cache->errstr);
        exit(1);
    }
    for(i = 0; i < reply->elements; i++)
    {
        reply2 = redisCommand(cache, "GET %s", reply->element[i]->str);
        if (reply2 == NULL)
        {
            printf("get failed: %s\n", cache->errstr);
            exit(1);
        }
        printf("%s = %d\n", reply->element[i]->str, *((int *)reply2->str));
        freeReplyObject(reply2);
    }
    freeReplyObject(reply);
	/* listener not supported */
}

int main()
{
    redisContext *cache;
    cache = redisConnect("localhost", 6379);
    test(cache);
    redisFree(cache);
    return 0;
}
