tg_redis.h
#ifndef TG_REDIS_H
#define TG_REDIS_H
#include <stdio.h>
#include <stdarg.h>
#include <hiredis/hiredis.h>
#include"string"
#include<string.h>
#include<memory>
#include<vector>
struct tg_redis_param
{
std::string host;
int port;
int db_index;
std::string pwd;
struct timeval timeout;
};
class tg_redis_result
{
private:
redisReply* _reply;
public:
tg_redis_result(redisReply* reply):_reply(reply)
{
}
~tg_redis_result()
{
if(_reply)
{
freeReplyObject(_reply);
_reply=nullptr;
}
}
public:
bool is_null()
{
return !_reply || _reply->type == REDIS_REPLY_NIL;
}
bool is_error()
{
return _reply->type == REDIS_REPLY_ERROR;
}
bool is_ok()
{
return !is_null() && !is_error();
}
const redisReply* get_reply()
{
return _reply;
}
bool try_get_value(int64_t& out_val)
{
if(is_error())
{
return false;
}
out_val = std::atoll(_reply->str);
return true;
}
bool try_get_value(double& out_val)
{
if(is_error())
{
return false;
}
out_val = std::atof(_reply->str);
return true;
}
bool try_get_value(std::string& out_val)
{
if(is_error())
{
return false;
}
out_val = _reply->str;
return true;
}
bool try_get_value(bool& out_val)
{
if(is_error())
{
return false;
}
out_val = strcasecmp("true", _reply->str) == 0;
return true;
}
uint64_t get_len()
{
return _reply->len;
}
bool is_arr()
{
return _reply->type == REDIS_REPLY_ARRAY;
}
uint64_t get_len_arr()
{
return _reply->elements;
}
int64_t get_integer()
{
return _reply->integer;
}
const char* get_str()
{
return _reply->str;
}
};
clas