{"id":80693,"date":"2026-03-05T20:07:48","date_gmt":"2026-03-05T12:07:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/80693.html"},"modified":"2026-03-05T20:07:48","modified_gmt":"2026-03-05T12:07:48","slug":"linux-c-c-%e5%ad%a6%e4%b9%a0%e6%97%a5%e8%ae%b0%ef%bc%8867%ef%bc%89%ef%bc%9aredis%ef%bc%88%e5%85%ab%ef%bc%89%ef%bc%9a%e5%ae%9e%e7%8e%b0redis%e8%bf%9e%e6%8e%a5%e6%b1%a0%ef%bc%8c%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/80693.html","title":{"rendered":"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d"},"content":{"rendered":"<p>\u5b8c\u6574\u4ee3\u7801\u89c1&#xff1a;deng-c-f\/redis_pool (gitee.com)<\/p>\n<\/p>\n<h2>cache_pool.h<\/h2>\n<p>#ifndef CACHEPOOL_H_<br \/>\n#define CACHEPOOL_H_<\/p>\n<p>#include &lt;condition_variable&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;list&gt;<br \/>\n#include &lt;map&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;vector&gt;<\/p>\n<p>#include &lt;hiredis\/hiredis.h&gt;<\/p>\n<p>using std::list;<br \/>\nusing std::map;<br \/>\nusing std::string;<br \/>\nusing std::vector;<\/p>\n<p>#define REDIS_COMMAND_SIZE 300 \/* redis Command \u6307\u4ee4\u6700\u5927\u957f\u5ea6 *\/<br \/>\n#define FIELD_ID_SIZE 100      \/* redis hash\u8868field\u57df\u5b57\u6bb5\u957f\u5ea6 *\/<br \/>\n#define VALUES_ID_SIZE 1024    \/* redis        value\u57df\u5b57\u6bb5\u957f\u5ea6 *\/<br \/>\ntypedef char (<br \/>\n    *RFIELDS)[FIELD_ID_SIZE]; \/* redis hash\u8868\u5b58\u653e\u6279\u91cffield\u5b57\u7b26\u4e32\u6570\u7ec4\u7c7b\u578b *\/<\/p>\n<p>\/\/\u6570\u7ec4\u6307\u9488\u7c7b\u578b&#xff0c;\u5176\u53d8\u91cf\u6307\u5411 char[1024]<br \/>\ntypedef char (<br \/>\n    *RVALUES)[VALUES_ID_SIZE]; \/* redis \u8868\u5b58\u653e\u6279\u91cfvalue\u5b57\u7b26\u4e32\u6570\u7ec4\u7c7b\u578b *\/<\/p>\n<p>class CachePool;<\/p>\n<p>class CacheConn {<br \/>\n  public:<br \/>\n    CacheConn(const char *server_ip, int server_port, int db_index,<br \/>\n              const char *password, const char *pool_name &#061; &#034;&#034;);<br \/>\n    CacheConn(CachePool *pCachePool);<br \/>\n    virtual ~CacheConn();<\/p>\n<p>    int Init();<br \/>\n    void DeInit();<br \/>\n    const char *GetPoolName();<br \/>\n    \/\/ \u901a\u7528\u64cd\u4f5c<br \/>\n    \/\/ \u5224\u65ad\u4e00\u4e2akey\u662f\u5426\u5b58\u5728<br \/>\n    bool IsExists(string &amp;key);<br \/>\n    \/\/ \u5220\u9664\u67d0\u4e2akey<br \/>\n    long Del(string key);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \u5b57\u7b26\u4e32\u76f8\u5173 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n    string Get(string key);<br \/>\n    string Set(string key, string value);<br \/>\n    string SetEx(string key, int timeout, string value);<\/p>\n<p>    \/\/ string mset(string key, map);<br \/>\n    \/\/\u6279\u91cf\u83b7\u53d6<br \/>\n    bool MGet(const vector&lt;string&gt; &amp;keys, map&lt;string, string&gt; &amp;ret_value);<br \/>\n    \/\/\u539f\u5b50\u52a0\u51cf1<br \/>\n    int Incr(string key, int64_t &amp;value);<br \/>\n    int Decr(string key, int64_t &amp;value);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;- \u54c8\u5e0c\u76f8\u5173 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n    long Hdel(string key, string field);<br \/>\n    string Hget(string key, string field);<br \/>\n    int Hget(string key, char *field, char *value);<br \/>\n    bool HgetAll(string key, map&lt;string, string&gt; &amp;ret_value);<br \/>\n    long Hset(string key, string field, string value);<\/p>\n<p>    long HincrBy(string key, string field, long value);<br \/>\n    long IncrBy(string key, long value);<br \/>\n    string Hmset(string key, map&lt;string, string&gt; &amp;hash);<br \/>\n    bool Hmget(string key, list&lt;string&gt; &amp;fields, list&lt;string&gt; &amp;ret_value);<\/p>\n<p>    \/\/ &#8212;&#8212;&#8212;&#8212; \u94fe\u8868\u76f8\u5173 &#8212;&#8212;&#8212;&#8212;<br \/>\n    long Lpush(string key, string value);<br \/>\n    long Rpush(string key, string value);<br \/>\n    long Llen(string key);<br \/>\n    bool Lrange(string key, long start, long end, list&lt;string&gt; &amp;ret_value);<\/p>\n<p>    \/\/ zset \u76f8\u5173<br \/>\n    int ZsetExit(string key, string member);<br \/>\n    int ZsetAdd(string key, long score, string member);<br \/>\n    int ZsetZrem(string key, string member);<br \/>\n    int ZsetIncr(string key, string member);<br \/>\n    int ZsetZcard(string key);<br \/>\n    int ZsetZrevrange(string key, int from_pos, int end_pos, RVALUES values,<br \/>\n                      int &amp;get_num);<br \/>\n    int ZsetGetScore(string key, string member);<\/p>\n<p>    \/\/ \u83b7\u53d6\u6d88\u606f\u961f\u5217\u76f8\u5173\u547d\u4ee4<br \/>\n    \/**<br \/>\n     * key &#xff1a;\u961f\u5217\u540d<br \/>\n        end &#xff1a;\u7ed3\u675f\u503c&#xff0c; &#043; \u8868\u793a\u6700\u5927\u503c<br \/>\n        start &#xff1a;\u5f00\u59cb\u503c&#xff0c; &#8211; \u8868\u793a\u6700\u5c0f\u503c<br \/>\n        count &#xff1a;\u6570\u91cf<br \/>\n     *\/<br \/>\n    bool  GetXrevrange(const string &amp; key,<br \/>\n      const string start, const string end, int count, std::vector&lt;std::pair&lt;string, string&gt;&gt; &amp;msgs);<br \/>\n    \/\/ \/ \u6dfb\u52a0\u6d88\u606f\u5230\u6d41<br \/>\n    bool Xadd(const string&amp; key, string&amp; id, const std::vector&lt;std::pair&lt;string, string&gt;&gt;&amp; field_value_pairs);<\/p>\n<p>    bool FlushDb();<\/p>\n<p>  private:<br \/>\n    CachePool *cache_pool_;<br \/>\n    redisContext *context_; \/\/ \u6bcf\u4e2aredis\u8fde\u63a5 redisContext redis\u5ba2\u6237\u7aef\u7f16\u7a0b\u7684\u5bf9\u8c61<br \/>\n    uint64_t last_connect_time_;<br \/>\n    uint16_t server_port_;<br \/>\n    string server_ip_;<br \/>\n    string password_;<br \/>\n    uint16_t db_index_;<br \/>\n    string pool_name_;<br \/>\n};<\/p>\n<p>class CachePool {<br \/>\n  public:<br \/>\n    \/\/ db_index\u548cmysql\u4e0d\u540c\u7684\u5730\u65b9<br \/>\n    CachePool(const char *pool_name, const char *server_ip, int server_port,<br \/>\n              int db_index, const char *password, int max_conn_cnt);<br \/>\n    virtual ~CachePool();<\/p>\n<p>    int Init();<br \/>\n    \/\/ \u83b7\u53d6\u7a7a\u95f2\u7684\u8fde\u63a5\u8d44\u6e90<br \/>\n    CacheConn *GetCacheConn(const int timeout_ms &#061; 0);<br \/>\n    \/\/ Pool\u56de\u6536\u8fde\u63a5\u8d44\u6e90<br \/>\n    void RelCacheConn(CacheConn *cache_conn);<\/p>\n<p>    const char *GetPoolName() { return pool_name_.c_str(); }<br \/>\n    const char *GetServerIP() { return server_ip_.c_str(); }<br \/>\n    const char *GetPassword() { return password_.c_str(); }<br \/>\n    int GetServerPort() { return m_server_port; }<br \/>\n    int GetDBIndex() { return db_index_; }<\/p>\n<p>  private:<br \/>\n    string pool_name_;<br \/>\n    string server_ip_;<br \/>\n    string password_;<br \/>\n    int m_server_port;<br \/>\n    int db_index_; \/\/ mysql \u6570\u636e\u5e93\u540d\u5b57&#xff0c; redis db index<\/p>\n<p>    int cur_conn_cnt_;<br \/>\n    int max_conn_cnt_;<br \/>\n    list&lt;CacheConn *&gt; free_list_;<\/p>\n<p>    std::mutex m_mutex;<br \/>\n    std::condition_variable cond_var_;<br \/>\n    bool abort_request_ &#061; false;<br \/>\n};<\/p>\n<p>class CacheManager {<br \/>\n  public:<br \/>\n    virtual ~CacheManager();<br \/>\n    \/\/\/ &#064;brief<br \/>\n    \/\/\/ &#064;param conf_path<br \/>\n    static void SetConfPath(const char *conf_path);<br \/>\n    static CacheManager *getInstance();<\/p>\n<p>    int Init();<br \/>\n    CacheConn *GetCacheConn(const char *pool_name);<br \/>\n    void RelCacheConn(CacheConn *cache_conn);<\/p>\n<p>  private:<br \/>\n    CacheManager();<\/p>\n<p>  private:<br \/>\n    static CacheManager *s_cache_manager;<br \/>\n    map&lt;string, CachePool *&gt; m_cache_pool_map;<br \/>\n    static string conf_path_;<br \/>\n};<\/p>\n<p>class AutoRelCacheCon {<br \/>\n  public:<br \/>\n    AutoRelCacheCon(CacheManager *manger, CacheConn *conn)<br \/>\n        : manger_(manger), conn_(conn) {}<br \/>\n    ~AutoRelCacheCon() {<br \/>\n        if (manger_) {<br \/>\n            manger_-&gt;RelCacheConn(conn_);<br \/>\n        }<br \/>\n        \/\/ std::cout &lt;&lt; &#034;\u6d4b\u8bd5\u8fde\u63a5\u56de\u6536&#034; &lt;&lt; std::endl; \/\/ \u6d4b\u8bd5\u81ea\u52a8\u56de\u6536<br \/>\n    } \/\/\u5728\u6790\u6784\u51fd\u6570\u89c4\u5212<br \/>\n  private:<br \/>\n    CacheManager *manger_ &#061; NULL;<br \/>\n    CacheConn *conn_ &#061; NULL;<br \/>\n};<\/p>\n<p>#define AUTO_REL_CACHECONN(m, c) AutoRelCacheCon autorelcacheconn(m, c)<\/p>\n<p>#endif \/* CACHEPOOL_H_ *\/<\/p>\n<h2>cache_pool.cc<\/h2>\n<p>#include &#034;cache_pool.h&#034;<\/p>\n<p>#include &#034;.\/base\/util.h&#034;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#define log_error printf<br \/>\n#define log_info printf<br \/>\n#define log_warn printf<br \/>\n\/\/ #define log printf<br \/>\n#define MIN_CACHE_CONN_CNT 2<br \/>\n#define MAX_CACHE_CONN_FAIL_NUM 10<\/p>\n<p>#include &#034;.\/base\/config_file_reader.h&#034;<br \/>\n#include &lt;utility&gt;<\/p>\n<p>CacheManager *CacheManager::s_cache_manager &#061; NULL;<br \/>\nstring  CacheManager::conf_path_ &#061; &#034;&#034;;\/\/&#034;tc_http_server.conf&#034;; \/\/ \u9ed8\u8ba4<br \/>\nCacheConn::CacheConn(const char *server_ip, int server_port, int db_index,<br \/>\n                     const char *password, const char *pool_name) {<br \/>\n    server_ip_ &#061; server_ip;<br \/>\n    server_port_ &#061; server_port;<\/p>\n<p>    db_index_ &#061; db_index;<br \/>\n    password_ &#061; password;<br \/>\n    pool_name_ &#061; pool_name;<br \/>\n    context_ &#061; NULL;<br \/>\n    last_connect_time_ &#061; 0;<br \/>\n}<\/p>\n<p>CacheConn::CacheConn(CachePool *pCachePool) {<br \/>\n    cache_pool_ &#061; pCachePool;<br \/>\n    if (pCachePool) {<br \/>\n        server_ip_ &#061; pCachePool-&gt;GetServerIP();<br \/>\n        server_port_ &#061; pCachePool-&gt;GetServerPort();<br \/>\n        db_index_ &#061; pCachePool-&gt;GetDBIndex();<br \/>\n        password_ &#061; pCachePool-&gt;GetPassword();<br \/>\n        pool_name_ &#061; pCachePool-&gt;GetPoolName();<br \/>\n    } else {<br \/>\n        log_error(&#034;pCachePool is NULL\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    context_ &#061; NULL;<br \/>\n    last_connect_time_ &#061; 0;<br \/>\n}<\/p>\n<p>CacheConn::~CacheConn() {<br \/>\n    if (context_) {<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/*<br \/>\n * redis\u521d\u59cb\u5316\u8fde\u63a5\u548c\u91cd\u8fde\u64cd\u4f5c&#xff0c;\u7c7b\u4f3cmysql_ping()<br \/>\n *\/<br \/>\nint CacheConn::Init() {<br \/>\n    if (context_) \/\/ \u975e\u7a7a&#xff0c;\u8fde\u63a5\u662f\u6b63\u5e38\u7684<br \/>\n    {<br \/>\n        return 0;<br \/>\n    }<\/p>\n<p>    \/\/ 1s \u5c1d\u8bd5\u91cd\u8fde\u4e00\u6b21<br \/>\n    uint64_t cur_time &#061; (uint64_t)time(NULL);<br \/>\n    if (cur_time &lt; last_connect_time_ &#043; 1) \/\/ \u91cd\u8fde\u5c1d\u8bd5 \u95f4\u96941\u79d2<br \/>\n    {<br \/>\n        printf(&#034;cur_time:%lu, m_last_connect_time:%lu\\\\n&#034;, cur_time,<br \/>\n               last_connect_time_);<br \/>\n        return 1;<br \/>\n    }<br \/>\n    \/\/ printf(&#034;m_last_connect_time &#061; cur_time\\\\n&#034;);<br \/>\n    last_connect_time_ &#061; cur_time;<\/p>\n<p>    \/\/ 1000ms\u8d85\u65f6<br \/>\n    struct timeval timeout &#061; {0, 1000000};<br \/>\n    \/\/ \u5efa\u7acb\u8fde\u63a5\u540e\u4f7f\u7528 redisContext \u6765\u4fdd\u5b58\u8fde\u63a5\u72b6\u6001\u3002<br \/>\n    \/\/ redisContext \u5728\u6bcf\u6b21\u64cd\u4f5c\u540e\u4f1a\u4fee\u6539\u5176\u4e2d\u7684 err \u548c  errstr<br \/>\n    \/\/ \u5b57\u6bb5\u6765\u8868\u793a\u53d1\u751f\u7684\u9519\u8bef\u7801&#xff08;\u5927\u4e8e0&#xff09;\u548c\u5bf9\u5e94\u7684\u63cf\u8ff0\u3002<br \/>\n    context_ &#061;<br \/>\n        redisConnectWithTimeout(server_ip_.c_str(), server_port_, timeout);<\/p>\n<p>    if (!context_ || context_-&gt;err) {<br \/>\n        if (context_) {<br \/>\n            log_error(&#034;redisConnect failed: %s\\\\n&#034;, context_-&gt;errstr);<br \/>\n            redisFree(context_);<br \/>\n            context_ &#061; NULL;<br \/>\n        } else {<br \/>\n            log_error(&#034;redisConnect failed\\\\n&#034;);<br \/>\n        }<\/p>\n<p>        return 1;<br \/>\n    }<\/p>\n<p>    redisReply *reply;<br \/>\n    \/\/ \u9a8c\u8bc1<br \/>\n    if (!password_.empty()) {<br \/>\n        reply &#061;<br \/>\n            (redisReply *)redisCommand(context_, &#034;AUTH %s&#034;, password_.c_str());<\/p>\n<p>        if (!reply || reply-&gt;type &#061;&#061; REDIS_REPLY_ERROR) {<br \/>\n            log_error(&#034;Authentication failure:%p\\\\n&#034;, reply);<br \/>\n            if (reply)<br \/>\n                freeReplyObject(reply);<br \/>\n            return -1;<br \/>\n        } else {<br \/>\n            \/\/ log_info(&#034;Authentication success\\\\n&#034;);<br \/>\n        }<\/p>\n<p>        freeReplyObject(reply);<br \/>\n    }<\/p>\n<p>    reply &#061; (redisReply *)redisCommand(context_, &#034;SELECT %d&#034;, db_index_);<\/p>\n<p>    if (reply &amp;&amp; (reply-&gt;type &#061;&#061; REDIS_REPLY_STATUS) &amp;&amp;<br \/>\n        (strncmp(reply-&gt;str, &#034;OK&#034;, 2) &#061;&#061; 0)) {<br \/>\n        freeReplyObject(reply);<br \/>\n        return 0;<br \/>\n    } else {<br \/>\n        if (reply)<br \/>\n            log_error(&#034;select cache db failed:%s\\\\n&#034;, reply-&gt;str);<br \/>\n        return 2;<br \/>\n    }<br \/>\n}<\/p>\n<p>void CacheConn::DeInit() {<br \/>\n    if (context_) {<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n    }<br \/>\n}<\/p>\n<p>const char *CacheConn::GetPoolName() { return pool_name_.c_str(); }<\/p>\n<p>string CacheConn::Get(string key) {<br \/>\n    string value;<\/p>\n<p>    if (Init()) {<br \/>\n        return value;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;GET %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return value;<br \/>\n    }<\/p>\n<p>    if (reply-&gt;type &#061;&#061; REDIS_REPLY_STRING) {<br \/>\n        value.append(reply-&gt;str, reply-&gt;len);<br \/>\n    }<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return value;<br \/>\n}<\/p>\n<p>string CacheConn::Set(string key, string value) {<br \/>\n    string ret_value;<\/p>\n<p>    if (Init()) {<br \/>\n        return ret_value;<br \/>\n    }<br \/>\n    \/\/ \u8fd4\u56de\u7684\u7ed3\u679c\u5b58\u653e\u5728redisReply<br \/>\n    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;SET %s %s&#034;,<br \/>\n                                                   key.c_str(), value.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    ret_value.append(reply-&gt;str, reply-&gt;len);<br \/>\n    freeReplyObject(reply); \/\/ \u91ca\u653e\u8d44\u6e90<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>string CacheConn::SetEx(string key, int timeout, string value) {<br \/>\n    string ret_value;<\/p>\n<p>    if (Init()) {<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(<br \/>\n        context_, &#034;SETEX %s %d %s&#034;, key.c_str(), timeout, value.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    ret_value.append(reply-&gt;str, reply-&gt;len);<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>bool CacheConn::MGet(const vector&lt;string&gt; &amp;keys,<br \/>\n                     map&lt;string, string&gt; &amp;ret_value) {<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<br \/>\n    if (keys.empty()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    string strKey;<br \/>\n    bool bFirst &#061; true;<br \/>\n    for (vector&lt;string&gt;::const_iterator it &#061; keys.begin(); it !&#061; keys.end();<br \/>\n         &#043;&#043;it) {<br \/>\n        if (bFirst) {<br \/>\n            bFirst &#061; false;<br \/>\n            strKey &#061; *it;<br \/>\n        } else {<br \/>\n            strKey &#043;&#061; &#034; &#034; &#043; *it;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    if (strKey.empty()) {<br \/>\n        return false;<br \/>\n    }<br \/>\n    strKey &#061; &#034;MGET &#034; &#043; strKey;<br \/>\n    redisReply *reply &#061; (redisReply *)redisCommand(context_, strKey.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_info(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return false;<br \/>\n    }<br \/>\n    if (reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; &#043;&#043;i) {<br \/>\n            redisReply *child_reply &#061; reply-&gt;element[i];<br \/>\n            if (child_reply-&gt;type &#061;&#061; REDIS_REPLY_STRING) {<br \/>\n                ret_value[keys[i]] &#061; child_reply-&gt;str;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<\/p>\n<p>bool CacheConn::IsExists(string &amp;key) {<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;EXISTS %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return false;<br \/>\n    }<br \/>\n    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    if (0 &#061;&#061; ret_value) {<br \/>\n        return false;<br \/>\n    } else {<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p>long CacheConn::Del(string key) {<br \/>\n    if (Init()) {<br \/>\n        return 0;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;DEL %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return 0;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>long CacheConn::Hdel(string key, string field) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<br \/>\n    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;HDEL %s %s&#034;,<br \/>\n                                                   key.c_str(), field.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>string CacheConn::Hget(string key, string field) {<br \/>\n    string ret_value;<br \/>\n    if (Init()) {<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;HGET %s %s&#034;,<br \/>\n                                                   key.c_str(), field.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    if (reply-&gt;type &#061;&#061; REDIS_REPLY_STRING) {<br \/>\n        ret_value.append(reply-&gt;str, reply-&gt;len);<br \/>\n    }<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<br \/>\nint CacheConn::Hget(string key, char *field, char *value) {<br \/>\n    int retn &#061; 0;<br \/>\n    int len &#061; 0;<\/p>\n<p>    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;hget %s %s&#034;, key.c_str(), field);<br \/>\n    if (reply &#061;&#061; NULL || reply-&gt;type !&#061; REDIS_REPLY_STRING) {<br \/>\n        printf(&#034;hget %s %s  error %s\\\\n&#034;, key.c_str(), field, context_-&gt;errstr);<br \/>\n        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>    len &#061; reply-&gt;len &gt; VALUES_ID_SIZE ? VALUES_ID_SIZE : reply-&gt;len;<br \/>\n    strncpy(value, reply-&gt;str, len);<\/p>\n<p>    value[len] &#061; &#039;\\\\0&#039;;<\/p>\n<p>END:<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    return retn;<br \/>\n}<br \/>\nbool CacheConn::HgetAll(string key, map&lt;string, string&gt; &amp;ret_value) {<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;HGETALL %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    if ((reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) &amp;&amp; (reply-&gt;elements % 2 &#061;&#061; 0)) {<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; i &#043;&#061; 2) {<br \/>\n            redisReply *field_reply &#061; reply-&gt;element[i];<br \/>\n            redisReply *value_reply &#061; reply-&gt;element[i &#043; 1];<\/p>\n<p>            string field(field_reply-&gt;str, field_reply-&gt;len);<br \/>\n            string value(value_reply-&gt;str, value_reply-&gt;len);<br \/>\n            ret_value.insert(make_pair(field, value));<br \/>\n        }<br \/>\n    }<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<\/p>\n<p>long CacheConn::Hset(string key, string field, string value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(<br \/>\n        context_, &#034;HSET %s %s %s&#034;, key.c_str(), field.c_str(), value.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>long CacheConn::HincrBy(string key, string field, long value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(<br \/>\n        context_, &#034;HINCRBY %s %s %ld&#034;, key.c_str(), field.c_str(), value);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>long CacheConn::IncrBy(string key, long value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;INCRBY %s %ld&#034;,<br \/>\n                                                   key.c_str(), value);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redis Command failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<br \/>\n    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>string CacheConn::Hmset(string key, map&lt;string, string&gt; &amp;hash) {<br \/>\n    string ret_value;<\/p>\n<p>    if (Init()) {<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    int argc &#061; hash.size() * 2 &#043; 2;<br \/>\n    const char **argv &#061; new const char *[argc];<br \/>\n    if (!argv) {<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    argv[0] &#061; &#034;HMSET&#034;;<br \/>\n    argv[1] &#061; key.c_str();<br \/>\n    int i &#061; 2;<br \/>\n    for (map&lt;string, string&gt;::iterator it &#061; hash.begin(); it !&#061; hash.end();<br \/>\n         it&#043;&#043;) {<br \/>\n        argv[i&#043;&#043;] &#061; it-&gt;first.c_str();<br \/>\n        argv[i&#043;&#043;] &#061; it-&gt;second.c_str();<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommandArgv(context_, argc, argv, NULL);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        delete[] argv;<\/p>\n<p>        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return ret_value;<br \/>\n    }<\/p>\n<p>    ret_value.append(reply-&gt;str, reply-&gt;len);<\/p>\n<p>    delete[] argv;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>bool CacheConn::Hmget(string key, list&lt;string&gt; &amp;fields,<br \/>\n                      list&lt;string&gt; &amp;ret_value) {<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    int argc &#061; fields.size() &#043; 2;<br \/>\n    const char **argv &#061; new const char *[argc];<br \/>\n    if (!argv) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    argv[0] &#061; &#034;HMGET&#034;;<br \/>\n    argv[1] &#061; key.c_str();<br \/>\n    int i &#061; 2;<br \/>\n    for (list&lt;string&gt;::iterator it &#061; fields.begin(); it !&#061; fields.end(); it&#043;&#043;) {<br \/>\n        argv[i&#043;&#043;] &#061; it-&gt;c_str();<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommandArgv(<br \/>\n        context_, argc, (const char **)argv, NULL);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        delete[] argv;<\/p>\n<p>        redisFree(context_);<br \/>\n        context_ &#061; NULL;<\/p>\n<p>        return false;<br \/>\n    }<\/p>\n<p>    if (reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; i&#043;&#043;) {<br \/>\n            redisReply *value_reply &#061; reply-&gt;element[i];<br \/>\n            string value(value_reply-&gt;str, value_reply-&gt;len);<br \/>\n            ret_value.push_back(value);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    delete[] argv;<br \/>\n    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<\/p>\n<p>int CacheConn::Incr(string key, int64_t &amp;value) {<br \/>\n    value &#061; 0;<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;INCR %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redis Command failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<br \/>\n    value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>int CacheConn::Decr(string key, int64_t &amp;value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;DECR %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redis Command failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<br \/>\n    value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>long CacheConn::Lpush(string key, string value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;LPUSH %s %s&#034;,<br \/>\n                                                   key.c_str(), value.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>long CacheConn::Rpush(string key, string value) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;RPUSH %s %s&#034;,<br \/>\n                                                   key.c_str(), value.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>long CacheConn::Llen(string key) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;LLEN %s&#034;, key.c_str());<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    long ret_value &#061; reply-&gt;integer;<br \/>\n    freeReplyObject(reply);<br \/>\n    return ret_value;<br \/>\n}<\/p>\n<p>bool CacheConn::Lrange(string key, long start, long end,<br \/>\n                       list&lt;string&gt; &amp;ret_value) {<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;LRANGE %s %d %d&#034;,<br \/>\n                                                   key.c_str(), start, end);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    if (reply-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n        for (size_t i &#061; 0; i &lt; reply-&gt;elements; i&#043;&#043;) {<br \/>\n            redisReply *value_reply &#061; reply-&gt;element[i];<br \/>\n            string value(value_reply-&gt;str, value_reply-&gt;len);<br \/>\n            ret_value.push_back(value);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<\/p>\n<p>int CacheConn::ZsetExit(string key, string member) {<br \/>\n    int retn &#061; 0;<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/\u6267\u884c\u547d\u4ee4<br \/>\n    reply &#061;<br \/>\n        (redisReply *)redisCommand(context_, &#034;zlexcount %s [%s [%s&#034;,<br \/>\n                                   key.c_str(), member.c_str(), member.c_str());<\/p>\n<p>    if (reply-&gt;type !&#061; REDIS_REPLY_INTEGER) {<br \/>\n        log_error(&#034;zlexcount: %s,member: %s Error:%s,%s\\\\n&#034;, key.c_str(),<br \/>\n                  member.c_str(), reply-&gt;str, context_-&gt;errstr);<br \/>\n        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>    retn &#061; reply-&gt;integer;<\/p>\n<p>END:<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return retn;<br \/>\n}<\/p>\n<p>int CacheConn::ZsetAdd(string key, long score, string member) {<br \/>\n    int retn &#061; 0;<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        std::cout &lt;&lt; &#034;Init() -&gt; failed&#034;;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/\u6267\u884c\u547d\u4ee4, reply-&gt;integer\u6210\u529f\u8fd4\u56de1&#xff0c;reply-&gt;integer\u5931\u8d25\u8fd4\u56de0<br \/>\n    reply &#061; (redisReply *)redisCommand(context_, &#034;ZADD %s %ld %s&#034;, key.c_str(),<br \/>\n                                       score, member.c_str());<br \/>\n    \/\/ rop_test_reply_type(reply);<\/p>\n<p>    if (reply-&gt;type !&#061; REDIS_REPLY_INTEGER) {<br \/>\n        printf(&#034;ZADD: %s,member: %s Error:%s,%s, reply-&gt;integer:%lld, %d\\\\n&#034;,<br \/>\n               key.c_str(), member.c_str(), reply-&gt;str, context_-&gt;errstr,<br \/>\n               reply-&gt;integer, reply-&gt;type);<br \/>\n        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>END:<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return retn;<br \/>\n}<\/p>\n<p>int CacheConn::ZsetZrem(string key, string member) {<br \/>\n    int retn &#061; 0;<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        std::cout &lt;&lt; &#034;Init() -&gt; failed&#034;;<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/\u6267\u884c\u547d\u4ee4, reply-&gt;integer\u6210\u529f\u8fd4\u56de1&#xff0c;reply-&gt;integer\u5931\u8d25\u8fd4\u56de0<br \/>\n    reply &#061; (redisReply *)redisCommand(context_, &#034;ZREM %s %s&#034;, key.c_str(),<br \/>\n                                       member.c_str());<br \/>\n    if (reply-&gt;type !&#061; REDIS_REPLY_INTEGER) {<br \/>\n        printf(&#034;ZREM: %s,member: %s Error:%s,%s\\\\n&#034;, key.c_str(), member.c_str(),<br \/>\n               reply-&gt;str, context_-&gt;errstr);<br \/>\n        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<br \/>\nEND:<\/p>\n<p>    freeReplyObject(reply);<br \/>\n    return retn;<br \/>\n}<br \/>\nint CacheConn::ZsetIncr(string key, string member) {<br \/>\n    int retn &#061; 0;<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    reply &#061; (redisReply *)redisCommand(context_, &#034;ZINCRBY %s 1 %s&#034;, key.c_str(),<br \/>\n                                       member.c_str());<br \/>\n    \/\/ rop_test_reply_type(reply);<br \/>\n    if (strcmp(reply-&gt;str, &#034;OK&#034;) !&#061; 0) {<br \/>\n        printf(&#034;Add or increment table: %s,member: %s Error:%s,%s\\\\n&#034;,<br \/>\n               key.c_str(), member.c_str(), reply-&gt;str, context_-&gt;errstr);<\/p>\n<p>        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>END:<br \/>\n    freeReplyObject(reply);<br \/>\n    return retn;<br \/>\n}<\/p>\n<p>int CacheConn::ZsetZcard(string key) {<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    int cnt &#061; 0;<\/p>\n<p>    reply &#061; (redisReply *)redisCommand(context_, &#034;ZCARD %s&#034;, key.c_str());<br \/>\n    if (reply-&gt;type !&#061; REDIS_REPLY_INTEGER) {<br \/>\n        printf(&#034;ZCARD %s error %s\\\\n&#034;, key.c_str(), context_-&gt;errstr);<br \/>\n        cnt &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>    cnt &#061; reply-&gt;integer;<\/p>\n<p>END:<br \/>\n    freeReplyObject(reply);<br \/>\n    return cnt;<br \/>\n}<br \/>\nint CacheConn::ZsetZrevrange(string key, int from_pos, int end_pos,<br \/>\n                             RVALUES values, int &amp;get_num) {<br \/>\n    int retn &#061; 0;<br \/>\n    redisReply *reply &#061; NULL;<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<br \/>\n    int i &#061; 0;<br \/>\n    int max_count &#061; 0;<\/p>\n<p>    int count &#061; end_pos &#8211; from_pos &#043; 1; \/\/\u8bf7\u6c42\u5143\u7d20\u4e2a\u6570<\/p>\n<p>    \/\/\u964d\u5e8f\u83b7\u53d6\u6709\u5e8f\u96c6\u5408\u7684\u5143\u7d20<br \/>\n    reply &#061; (redisReply *)redisCommand(context_, &#034;ZREVRANGE %s %d %d&#034;,<br \/>\n                                       key.c_str(), from_pos, end_pos);<br \/>\n    if (reply-&gt;type !&#061; REDIS_REPLY_ARRAY) \/\/\u5982\u679c\u8fd4\u56de\u4e0d\u662f\u6570\u7ec4<br \/>\n    {<br \/>\n        printf(&#034;ZREVRANGE %s  error!%s\\\\n&#034;, key.c_str(), context_-&gt;errstr);<br \/>\n        retn &#061; -1;<br \/>\n        goto END;<br \/>\n    }<\/p>\n<p>    \/\/\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u67e5\u770belements\u7684\u503c(\u6570\u7ec4\u4e2a\u6570)<br \/>\n    \/\/\u901a\u8fc7element[index] \u7684\u65b9\u5f0f\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<br \/>\n    \/\/\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u662f\u4e00\u4e2aredisReply\u5bf9\u8c61\u7684\u6307\u9488<\/p>\n<p>    max_count &#061; (reply-&gt;elements &gt; count) ? count : reply-&gt;elements;<br \/>\n    get_num &#061; max_count; \/\/\u5f97\u5230\u7ed3\u679cvalue\u7684\u4e2a\u6570<\/p>\n<p>    for (i &#061; 0; i &lt; max_count; &#043;&#043;i) {<br \/>\n        strncpy(values[i], reply-&gt;element[i]-&gt;str, VALUES_ID_SIZE &#8211; 1);<br \/>\n        values[i][VALUES_ID_SIZE &#8211; 1] &#061; 0; \/\/\u7ed3\u675f\u7b26<br \/>\n    }<\/p>\n<p>END:<br \/>\n    if (reply !&#061; NULL) {<br \/>\n        freeReplyObject(reply);<br \/>\n    }<\/p>\n<p>    return retn;<br \/>\n}<\/p>\n<p>int CacheConn::ZsetGetScore(string key, string member) {<br \/>\n    if (Init()) {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    int score &#061; 0;<\/p>\n<p>    redisReply *reply &#061; NULL;<\/p>\n<p>    reply &#061; (redisReply *)redisCommand(context_, &#034;ZSCORE %s %s&#034;, key.c_str(),<br \/>\n                                       member.c_str());<\/p>\n<p>    if (reply-&gt;type !&#061; REDIS_REPLY_STRING) {<br \/>\n        printf(&#034;[-][GMS_REDIS]ZSCORE %s %s error %s\\\\n&#034;, key.c_str(),<br \/>\n               member.c_str(), context_-&gt;errstr);<br \/>\n        score &#061; -1;<br \/>\n        goto END;<br \/>\n    }<br \/>\n    score &#061; atoi(reply-&gt;str);<\/p>\n<p>END:<br \/>\n    freeReplyObject(reply);<\/p>\n<p>    return score;<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u6d88\u606f\u961f\u5217\u76f8\u5173\u547d\u4ee4<br \/>\n\/**<br \/>\n * key &#xff1a;\u961f\u5217\u540d<br \/>\n    end &#xff1a;\u7ed3\u675f\u503c&#xff0c; &#043; \u8868\u793a\u6700\u5927\u503c<br \/>\n    start &#xff1a;\u5f00\u59cb\u503c&#xff0c; &#8211; \u8868\u793a\u6700\u5c0f\u503c<br \/>\n    count &#xff1a;\u6570\u91cf<br \/>\n    *\/<br \/>\n\/\/    XREVRANGE mystream &#043; &#8211;<br \/>\n\/\/ XREVRANGE \u7a0b\u5e8f\u5458\u8001\u5ed62 &#043; &#8211;<br \/>\n\/\/ XREVRANGE \u7a0b\u5e8f\u5458\u8001\u5ed6 &#043; &#8211;<br \/>\nbool  CacheConn::GetXrevrange(const string &amp; key,<br \/>\n    const string start, const string end, int count, std::vector&lt;std::pair&lt;string, string&gt;&gt; &amp;msgs) {<br \/>\n    bool ret &#061; false;<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<br \/>\n    \/\/ \u6784\u5efa XREVRANGE \u547d\u4ee4<br \/>\n    string  command &#061; &#034;XREVRANGE &#034; &#043; key &#043; &#034; &#034; &#043; start  &#043; &#034; &#034; &#043; end;<br \/>\n    if (count &gt; 0) {<br \/>\n        command &#043;&#061; &#034; COUNT &#034; &#043; std::to_string(count);<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;command: &#034; &lt;&lt; command;<br \/>\n    \/\/ \u53d1\u9001\u547d\u4ee4<br \/>\n    redisReply* reply &#061; (redisReply*)redisCommand(context_, command.c_str());<br \/>\n    if (!reply) {<br \/>\n        std::cerr &lt;&lt; &#034;Failed to execute XREVRANGE command&#034; &lt;&lt; std::endl;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u67e5\u56de\u590d\u7c7b\u578b<br \/>\n    if (reply-&gt;type !&#061; REDIS_REPLY_ARRAY) {<br \/>\n        std::cerr &lt;&lt; &#034;Unexpected reply type: &#034; &lt;&lt; reply-&gt;type &lt;&lt; std::endl;<br \/>\n        freeReplyObject(reply);<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u89e3\u6790\u56de\u590d<br \/>\n    for (size_t i &#061; 0; i &lt; reply-&gt;elements; i&#043;&#043;) {<br \/>\n        redisReply* entry &#061; reply-&gt;element[i];<br \/>\n        if (entry-&gt;type &#061;&#061; REDIS_REPLY_ARRAY &amp;&amp; entry-&gt;elements &gt;&#061; 2) {<br \/>\n            \/\/ \u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6d88\u606f ID<br \/>\n            string  message_id &#061; entry-&gt;element[0]-&gt;str;  \/\/\u6d88\u606fid \u83b7\u53d6\u56de\u6765\u7684<\/p>\n<p>            \/\/ \u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u6d88\u606f\u5185\u5bb9&#xff08;\u5b57\u6bb5-\u503c\u5bf9&#xff09;<br \/>\n            redisReply* fields &#061; entry-&gt;element[1];<br \/>\n            if (fields-&gt;type &#061;&#061; REDIS_REPLY_ARRAY) {<br \/>\n                string  message_content;<br \/>\n                 string  value;<br \/>\n                for (size_t j &#061; 0; j &lt; fields-&gt;elements; j &#043;&#061; 2) {<br \/>\n                    string  field &#061; fields-&gt;element[j]-&gt;str;<br \/>\n                    value &#061; fields-&gt;element[j &#043; 1]-&gt;str;<br \/>\n                    message_content &#043;&#061; field &#043; &#034;: &#034; &#043; value &#043; &#034;, &#034;;<br \/>\n                }<br \/>\n                \/\/ std::cout &lt;&lt; string( &#034;key: &#034; &#043; key &#043; &#034;, ID: &#034; &#043; message_id &#043; &#034;, Content: &#034; &#043; message_content);<br \/>\n                msgs.push_back({message_id, value});<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u91ca\u653e\u56de\u590d\u5bf9\u8c61<br \/>\n    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<\/p>\n<p>bool CacheConn::Xadd(const string&amp; key,   string&amp; id, const std::vector&lt;std::pair&lt;string, string&gt;&gt;&amp; field_value_pairs)<br \/>\n{<br \/>\n    bool ret &#061; false;<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<br \/>\n    \/\/ \u6784\u5efa XADD \u547d\u4ee4<br \/>\n    string  command &#061; &#034;XADD &#034; &#043; key &#043; &#034; &#034; &#043; id;<\/p>\n<p>    \/\/ \u6dfb\u52a0\u5b57\u6bb5-\u503c\u5bf9<br \/>\n    for (const auto&amp; pair : field_value_pairs) {<br \/>\n        command &#043;&#061; &#034; &#034; &#043; pair.first &#043; &#034; &#034; &#043; pair.second;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; &#034;command: &#034; &lt;&lt; command;<br \/>\n    \/\/ \u53d1\u9001\u547d\u4ee4<br \/>\n    redisReply* reply &#061; (redisReply*)redisCommand(context_, command.c_str());<br \/>\n    if (!reply) {<br \/>\n        std::cerr &lt;&lt; &#034;Failed to execute XADD command&#034; &lt;&lt; std::endl;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u68c0\u67e5\u56de\u590d\u7c7b\u578b<br \/>\n    if (reply-&gt;type &#061;&#061; REDIS_REPLY_ERROR) {<br \/>\n        std::cerr &lt;&lt; &#034;Error: &#034; &lt;&lt; reply-&gt;str &lt;&lt; std::endl;<br \/>\n        freeReplyObject(reply);<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    \/\/ \u6253\u5370\u6210\u529f\u6d88\u606f<br \/>\n    \/\/ std::cout &lt;&lt; &#034;Message added with ID: &#034; &lt;&lt; reply-&gt;str &lt;&lt; std::endl;<br \/>\n    id &#061; reply-&gt;str;<br \/>\n    \/\/ \u91ca\u653e\u56de\u590d\u5bf9\u8c61<br \/>\n    freeReplyObject(reply);<br \/>\n    return true;<br \/>\n}<br \/>\nbool CacheConn::FlushDb() {<br \/>\n    bool ret &#061; false;<br \/>\n    if (Init()) {<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    redisReply *reply &#061; (redisReply *)redisCommand(context_, &#034;FLUSHDB&#034;);<br \/>\n    if (!reply) {<br \/>\n        log_error(&#034;redisCommand failed:%s\\\\n&#034;, context_-&gt;errstr);<br \/>\n        redisFree(context_);<br \/>\n        context_ &#061; NULL;<br \/>\n        return false;<br \/>\n    }<\/p>\n<p>    if (reply-&gt;type &#061;&#061; REDIS_REPLY_STRING &amp;&amp;<br \/>\n        strncmp(reply-&gt;str, &#034;OK&#034;, 2) &#061;&#061; 0) {<br \/>\n        ret &#061; true;<br \/>\n    }<\/p>\n<p>    freeReplyObject(reply);<\/p>\n<p>    return ret;<br \/>\n}<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nCachePool::CachePool(const char *pool_name, const char *server_ip,<br \/>\n                     int server_port, int db_index, const char *password,<br \/>\n                     int max_conn_cnt) {<br \/>\n    pool_name_ &#061; pool_name;<br \/>\n    server_ip_ &#061; server_ip;<br \/>\n    m_server_port &#061; server_port;<br \/>\n    db_index_ &#061; db_index;<br \/>\n    password_ &#061; password;<br \/>\n    max_conn_cnt_ &#061; max_conn_cnt;<br \/>\n    cur_conn_cnt_ &#061; MIN_CACHE_CONN_CNT;<br \/>\n}<\/p>\n<p>CachePool::~CachePool() {<br \/>\n    {<br \/>\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);<br \/>\n        abort_request_ &#061; true;<br \/>\n        cond_var_.notify_all(); \/\/ \u901a\u77e5\u6240\u6709\u5728\u7b49\u5f85\u7684<br \/>\n    }<br \/>\n    {<br \/>\n        std::lock_guard&lt;std::mutex&gt; lock(m_mutex);<br \/>\n        for (list&lt;CacheConn *&gt;::iterator it &#061; free_list_.begin();<br \/>\n             it !&#061; free_list_.end(); it&#043;&#043;) {<br \/>\n            CacheConn *pConn &#061; *it;<br \/>\n            delete pConn;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    free_list_.clear();<br \/>\n    cur_conn_cnt_ &#061; 0;<br \/>\n}<\/p>\n<p>int CachePool::Init() {<br \/>\n    for (int i &#061; 0; i &lt; cur_conn_cnt_; i&#043;&#043;) {<br \/>\n        CacheConn *pConn &#061;<br \/>\n            new CacheConn(server_ip_.c_str(), m_server_port, db_index_,<br \/>\n                          password_.c_str(), pool_name_.c_str());<br \/>\n        if (pConn-&gt;Init()) {<br \/>\n            delete pConn;<br \/>\n            return 1;<br \/>\n        }<\/p>\n<p>        free_list_.push_back(pConn);<br \/>\n    }<\/p>\n<p>    log_info(&#034;cache pool: %s, list size: %lu\\\\n&#034;, pool_name_.c_str(),<br \/>\n             free_list_.size());<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>CacheConn *CachePool::GetCacheConn(const int timeout_ms) {<br \/>\n    std::unique_lock&lt;std::mutex&gt; lock(m_mutex);<br \/>\n    if (abort_request_) {<br \/>\n        log_info(&#034;have aboort\\\\n&#034;);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    if (free_list_.empty()) \/\/ 2 \u5f53\u6ca1\u6709\u8fde\u63a5\u53ef\u4ee5\u7528\u65f6<br \/>\n    {<br \/>\n        \/\/ \u7b2c\u4e00\u6b65\u5148\u68c0\u6d4b \u5f53\u524d\u8fde\u63a5\u6570\u91cf\u662f\u5426\u8fbe\u5230\u6700\u5927\u7684\u8fde\u63a5\u6570\u91cf<br \/>\n        if (cur_conn_cnt_ &gt;&#061; max_conn_cnt_) \/\/ \u7b49\u5f85\u7684\u903b\u8f91<br \/>\n        {<br \/>\n            \/\/ \u5982\u679c\u5df2\u7ecf\u5230\u8fbe\u4e86&#xff0c;\u770b\u770b\u662f\u5426\u9700\u8981\u8d85\u65f6\u7b49\u5f85<br \/>\n            if (timeout_ms &lt;&#061; 0) \/\/ \u6b7b\u7b49&#xff0c;\u76f4\u5230\u6709\u8fde\u63a5\u53ef\u4ee5\u7528 \u6216\u8005 \u8fde\u63a5\u6c60\u8981\u9000\u51fa<br \/>\n            {<br \/>\n                log_info(&#034;wait ms:%d\\\\n&#034;, timeout_ms);<br \/>\n                cond_var_.wait(lock, [this] {<br \/>\n                    \/\/ \u5f53\u524d\u8fde\u63a5\u6570\u91cf\u5c0f\u4e8e\u6700\u5927\u8fde\u63a5\u6570\u91cf \u6216\u8005\u8bf7\u6c42\u91ca\u653e\u8fde\u63a5\u6c60\u65f6\u9000\u51fa<br \/>\n                    return (!free_list_.empty()) | abort_request_;<br \/>\n                });<br \/>\n            } else {<br \/>\n                \/\/ return\u5982\u679c\u8fd4\u56de false&#xff0c;\u7ee7\u7eedwait(\u6216\u8005\u8d85\u65f6),<br \/>\n                \/\/ \u5982\u679c\u8fd4\u56detrue\u9000\u51fawait 1.m_free_list\u4e0d\u4e3a\u7a7a 2.\u8d85\u65f6\u9000\u51fa<br \/>\n                \/\/ 3. m_abort_request\u88ab\u7f6e\u4e3atrue&#xff0c;\u8981\u91ca\u653e\u6574\u4e2a\u8fde\u63a5\u6c60<br \/>\n                cond_var_.wait_for(<br \/>\n                    lock, std::chrono::milliseconds(timeout_ms),<br \/>\n                    [this] { return (!free_list_.empty()) | abort_request_; });<br \/>\n                \/\/ \u5e26\u8d85\u65f6\u529f\u80fd\u65f6\u8fd8\u8981\u5224\u65ad\u662f\u5426\u4e3a\u7a7a<br \/>\n                if (free_list_.empty()) \/\/ \u5982\u679c\u8fde\u63a5\u6c60\u8fd8\u662f\u6ca1\u6709\u7a7a\u95f2\u5219\u9000\u51fa<br \/>\n                {<br \/>\n                    return NULL;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            if (abort_request_) {<br \/>\n                log_warn(&#034;have aboort\\\\n&#034;);<br \/>\n                return NULL;<br \/>\n            }<br \/>\n        } else \/\/ \u8fd8\u6ca1\u6709\u5230\u6700\u5927\u8fde\u63a5\u5219\u521b\u5efa\u8fde\u63a5<br \/>\n        {<br \/>\n            CacheConn *db_conn &#061;<br \/>\n                new CacheConn(server_ip_.c_str(), m_server_port, db_index_,<br \/>\n                              password_.c_str(), pool_name_.c_str()); \/\/\u65b0\u5efa\u8fde\u63a5<br \/>\n            int ret &#061; db_conn-&gt;Init();<br \/>\n            if (ret) {<br \/>\n                log_error(&#034;Init DBConnecton failed\\\\n\\\\n&#034;);<br \/>\n                delete db_conn;<br \/>\n                return NULL;<br \/>\n            } else {<br \/>\n                free_list_.push_back(db_conn);<br \/>\n                cur_conn_cnt_&#043;&#043;;<br \/>\n                \/\/ log_info(&#034;new db connection: %s, conn_cnt: %d\\\\n&#034;,<br \/>\n                \/\/ m_pool_name.c_str(), m_cur_conn_cnt);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    CacheConn *pConn &#061; free_list_.front();<br \/>\n    free_list_.pop_front();<\/p>\n<p>    return pConn;<br \/>\n}<\/p>\n<p>void CachePool::RelCacheConn(CacheConn *p_cache_conn) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(m_mutex);<\/p>\n<p>    list&lt;CacheConn *&gt;::iterator it &#061; free_list_.begin();<br \/>\n    for (; it !&#061; free_list_.end(); it&#043;&#043;) {<br \/>\n        if (*it &#061;&#061; p_cache_conn) {<br \/>\n            break;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    if (it &#061;&#061; free_list_.end()) {<br \/>\n        \/\/ m_used_list.remove(pConn);<br \/>\n        free_list_.push_back(p_cache_conn);<br \/>\n        cond_var_.notify_one(); \/\/ \u901a\u77e5\u53d6\u961f\u5217<br \/>\n    } else {<br \/>\n        log_error(&#034;RelDBConn failed\\\\n&#034;); \/\/ \u4e0d\u518d\u6b21\u56de\u6536\u8fde\u63a5<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/<br \/>\nCacheManager::CacheManager() {}<\/p>\n<p>CacheManager::~CacheManager() {}<\/p>\n<p>void CacheManager::SetConfPath(const char *conf_path) {<br \/>\n    conf_path_ &#061; conf_path;<br \/>\n}<\/p>\n<p>CacheManager *CacheManager::getInstance() {<br \/>\n    if (!s_cache_manager) {<br \/>\n        s_cache_manager &#061; new CacheManager();<br \/>\n        if (s_cache_manager-&gt;Init()) {<br \/>\n            delete s_cache_manager;<br \/>\n            s_cache_manager &#061; NULL;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return s_cache_manager;<br \/>\n}<\/p>\n<p>int CacheManager::Init() {<br \/>\n    std::cout &lt;&lt; &#034;Init&#034;;<br \/>\n    CConfigFileReader config_file(conf_path_.c_str());<\/p>\n<p>    char *cache_instances &#061; config_file.GetConfigName(&#034;CacheInstances&#034;);<br \/>\n    if (!cache_instances) {<br \/>\n        std::cout &lt;&lt; &#034;not configure CacheIntance&#034;;<br \/>\n        return 1;<br \/>\n    }<\/p>\n<p>    char host[64];<br \/>\n    char port[64];<br \/>\n    char db[64];<br \/>\n    char maxconncnt[64];<br \/>\n    CStrExplode instances_name(cache_instances, &#039;,&#039;);<br \/>\n    for (uint32_t i &#061; 0; i &lt; instances_name.GetItemCnt(); i&#043;&#043;) {<br \/>\n        char *pool_name &#061; instances_name.GetItem(i);<br \/>\n        \/\/ printf(&#034;%s&#034;, pool_name);<br \/>\n        snprintf(host, 64, &#034;%s_host&#034;, pool_name);<br \/>\n        snprintf(port, 64, &#034;%s_port&#034;, pool_name);<br \/>\n        snprintf(db, 64, &#034;%s_db&#034;, pool_name);<br \/>\n        snprintf(maxconncnt, 64, &#034;%s_maxconncnt&#034;, pool_name);<\/p>\n<p>        char *cache_host &#061; config_file.GetConfigName(host);<br \/>\n        char *str_cache_port &#061; config_file.GetConfigName(port);<br \/>\n        char *str_cache_db &#061; config_file.GetConfigName(db);<br \/>\n        char *str_max_conn_cnt &#061; config_file.GetConfigName(maxconncnt);<br \/>\n        if (!cache_host || !str_cache_port || !str_cache_db ||<br \/>\n            !str_max_conn_cnt) {<br \/>\n            if(!cache_host)<br \/>\n                std::cout &lt;&lt; &#034;not configure cache instance: &#034; &lt;&lt;  pool_name &lt;&lt; &#034;, cache_host is null&#034;;<br \/>\n            if(!str_cache_port)<br \/>\n                std::cout &lt;&lt; &#034;not configure cache instance: &#034; &lt;&lt; pool_name &lt;&lt; &#034;, str_cache_port is null&#034;;<br \/>\n            if(!str_cache_db)<br \/>\n                std::cout &lt;&lt; &#034;not configure cache instance: &#034; &lt;&lt; pool_name &lt;&lt; &#034;, str_cache_db is null&#034;;<br \/>\n            if(!str_max_conn_cnt)<br \/>\n                std::cout &lt;&lt; &#034;not configure cache instance: &#034; &lt;&lt; pool_name &lt;&lt; &#034;, str_max_conn_cnt is null&#034;;<br \/>\n            return 2;<br \/>\n        }<\/p>\n<p>        CachePool *pCachePool &#061;<br \/>\n            new CachePool(pool_name, cache_host, atoi(str_cache_port),<br \/>\n                          atoi(str_cache_db), &#034;&#034;, atoi(str_max_conn_cnt));<br \/>\n        if (pCachePool-&gt;Init()) {<br \/>\n            std::cout &lt;&lt; &#034;Init cache pool failed&#034;;<br \/>\n            return 3;<br \/>\n        }<\/p>\n<p>        m_cache_pool_map.insert(make_pair(pool_name, pCachePool));<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>CacheConn *CacheManager::GetCacheConn(const char *pool_name) {<br \/>\n    map&lt;string, CachePool *&gt;::iterator it &#061; m_cache_pool_map.find(pool_name);<br \/>\n    if (it !&#061; m_cache_pool_map.end()) {<br \/>\n        return it-&gt;second-&gt;GetCacheConn();<br \/>\n    } else {<br \/>\n        return NULL;<br \/>\n    }<br \/>\n}<\/p>\n<p>void CacheManager::RelCacheConn(CacheConn *cache_conn) {<br \/>\n    if (!cache_conn) {<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    map&lt;string, CachePool *&gt;::iterator it &#061;<br \/>\n        m_cache_pool_map.find(cache_conn-&gt;GetPoolName());<br \/>\n    if (it !&#061; m_cache_pool_map.end()) {<br \/>\n        return it-&gt;second-&gt;RelCacheConn(cache_conn);<br \/>\n    }<br \/>\n}<\/p>\n<h2 style=\"background-color:transparent\">config<\/h2>\n<p>CacheInstances&#061;master<br \/>\nmaster_host&#061;127.0.0.1<br \/>\nmaster_port&#061;6379<br \/>\nmaster_db&#061;0 #\u521d\u59cb\u5316conn\u65f6\u9ed8\u8ba4\u6267\u884c Select db<br \/>\nmaster_maxconncnt&#061;128<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b8c\u6574\u4ee3\u7801\u89c1&#xff1a;deng-c-f\/redis_pool (gitee.com) cache_pool.h #ifndef CACHEPOOL_H_<br \/>\n#define CACHEPOOL_H_#include<br \/>\n#include<br \/>\n#include<br \/>\n#include <\/p>\n<map>\n#include<br \/>\n#include #include &lt;hiredis\/hiredis.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,174,58],"topic":[],"class_list":{"0":"post-80693","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-c","9":"tag-linux"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/80693.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5b8c\u6574\u4ee3\u7801\u89c1&#xff1a;deng-c-f\/redis_pool (gitee.com) cache_pool.h #ifndef CACHEPOOL_H_ #define CACHEPOOL_H_#include #include #include #include #include #include #include &lt;hiredis\/hiredis.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/80693.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-05T12:07:48+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80693.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/80693.html\",\"name\":\"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-05T12:07:48+00:00\",\"dateModified\":\"2026-03-05T12:07:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/80693.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/80693.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/80693.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/80693.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5b8c\u6574\u4ee3\u7801\u89c1&#xff1a;deng-c-f\/redis_pool (gitee.com) cache_pool.h #ifndef CACHEPOOL_H_ #define CACHEPOOL_H_#include #include #include #include #include #include #include &lt;hiredis\/hiredis.","og_url":"https:\/\/www.wsisp.com\/helps\/80693.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-05T12:07:48+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"22 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/80693.html","url":"https:\/\/www.wsisp.com\/helps\/80693.html","name":"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-05T12:07:48+00:00","dateModified":"2026-03-05T12:07:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/80693.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/80693.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/80693.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux C\/C++ \u5b66\u4e60\u65e5\u8bb0\uff0867\uff09\uff1aRedis\uff08\u516b\uff09\uff1a\u5b9e\u73b0redis\u8fde\u63a5\u6c60\uff0c\u5e94\u7528\u4e8e\u9879\u76ee\u5f53\u4e2d"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=80693"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/80693\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=80693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=80693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=80693"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=80693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}