I am new to redis. I want to write a simple library (for testing) that is on top level of the hiredis. for example to implement SET command I wrote the code bellow:
#include<iostream>
#include<type_traits>
#include<hiredis.h>
#include<string>
using namespace std;
template<typename T>
string set(string key, T value)
{
/* set a key */
if(is_same<T, int>::value)
{
reply = (redisReply*) redisCommand(c, "SET %s %d", key, value) // c is redisContext*
}
else if(is_same<T, string>::value)
{
reply = (redisReply*) redisCommand(c, "SET %s %s", key, value)
}
// and so on for other data types ...
string replyStr = reply->str;
freeReplyObject(reply);
return replyStr;
}
is there any better solution to handle different data types as Value for SET command? (I mean avoiding using If statements for each datatype ). regards.