對hiredis介面的封裝

2021-09-01 05:41:57 字數 3627 閱讀 4150

tg_redis.h

#ifndef tg_redis_h

#define tg_redis_h

#include #include #include #include"string"

#include#include#includestruct tg_redis_param

;class tg_redis_result

~tg_redis_result()

}public:

bool is_null()

bool is_error()

bool is_ok()

const redisreply* get_reply()

bool try_get_value(int64_t& out_val)

out_val = std::atoll(_reply->str);

return true;

}bool try_get_value(double& out_val)

out_val = std::atof(_reply->str);

return true;

}bool try_get_value(std::string& out_val)

out_val = _reply->str;

return true;

}bool try_get_value(bool& out_val)

out_val = strcasecmp("true", _reply->str) == 0;

return true;

}uint64_t get_len()

bool is_arr()

uint64_t get_len_arr()

int64_t get_integer()

const char* get_str()

};class tg_redis_conn

~tg_redis_conn()

bool connect()

;if (_context_ptr)

else

return false;

}if(!_param_ptr->pwd.empty() && !exec_cmd("auth %s", _param_ptr->pwd.c_str()))

if(!exec_cmd("select %d", _param_ptr->db_index))

_db_no = _param_ptr->db_index;

return true;

}void close()

}std::shared_ptrexec_cmd(const char* command, ...)

if(reply->is_error())

return reply;

}std::string& get_last_err()

bool rpush(std::string key,std::vector& value)

len=reply->get_integer();

for(int i=0; iget_reply()->type != redis_reply_integer)

if(++len != reply->get_integer())

}return true;

}bool lrange_all(std::string key, std::vector& out_data)

len=reply->get_integer();

reply = exec_cmd("lrange %s %d %d", key.c_str(),0,len-1);

if(!reply || reply->get_reply()->type != redis_reply_array)

redisreply** replyvector = reply->get_reply()->element;//獲取陣列指標

out_data.reserve(len);

for(int i=0;istr;//遍歷redisreply*陣列,存入vector向量

int a =std::atoi(temp.c_str());

out_data.push_back(a);

replyvector++;

}return true;

}///設定乙個字串到key

bool set(const char* key, const char* value, unsigned int expire=0)

///設定乙個字串到hash

bool hset(const char* hash_name, const char* key, const char* value)

bool rpush(const char* list_name, const char* value)

bool lset(const char* list_name, int index, const char* value)

///新增乙個資料到有序集合中

bool zadd(const char* set_name, int score, const char* value)

///根據對應的資料得到乙個排名

int64_t zrank(const char* set_name, const char* value)

return -1;

}///根據key得到乙個value

std::shared_ptrget(const char* key)

///獲取hash內的value

std::shared_ptrhget(const char* hash_name, const char* key)

///從有序集合中得到資料

std::shared_ptrzrange(const char* set_name, uint64_t start, uint64_t end)

///從列表中獲取資料

std::shared_ptrlrange(const char* list_name, size_t start, size_t end)

///列表長度

uint64_t llen(const char* list_name)

return 0;

}std::shared_ptrlpop(const char* list_name)

///移除list的尾部並且返回

std::shared_ptrrpop(const char* list_name)

bool exists(const char* key)

bool del(const char* key)

///當前使用中的db

int db_no()

};#endif // tg_redis_h

測試:

#include #include #include #include #include "tg_redis.h"

void test()

if(!conn.rpush("list8", arr))

{std::cout<< "conn.rpush(\"list8\", arr)" if(!conn.lrange_all("list8", out))

{std::cout<< "conn.lrange_all(\"list8\", out)" <

封裝通訊介面的方法

1.json方式封裝 注意 該函式只能接收utf 8編碼的資料,如果傳遞其他型別的資料則會返回null。arr array id 1,name singwa echo json encode arr php字串編碼轉換函式 iconv iconv utf 8 gbk data 第乙個引數為當前字串格...

對介面的理解

以下是我在ms news group裡,對 什麼情況下定義inte ce 這一問題的回答,基本上體現我現在對inte ce的理解,可能有不足或者完全就是謬誤,希望能得到大家的指正 乙個類實現了某個介面,就相當於宣告我能夠完成某項工作。比如乙個類實現了icomparable介面,就相當於宣告了我可以完...

對UART介面的理解

uart是一種通用序列資料匯流排,用於非同步通訊。該匯流排雙向通訊,可以實現全雙工傳輸和接收。在嵌入式設計中,uart用來與pc進行通訊,包括與監控偵錯程式和其它器件,如eeprom通訊。uart通訊 uart首先將接收到的並行資料轉換成序列資料來傳輸。訊息幀從乙個低位起始位開始,後面是7個或8個資...