{"id":387,"date":"2019-12-15T20:07:00","date_gmt":"2019-12-15T12:07:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=387"},"modified":"2021-05-08T20:09:24","modified_gmt":"2021-05-08T12:09:24","slug":"redis%e6%ba%90%e7%a0%81%e4%bb%8emain%e5%bc%80%e5%a7%8b6","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=387","title":{"rendered":"redis\u6e90\u7801\u4ecemain\u5f00\u59cb6"},"content":{"rendered":"\n<p>\u524d\u9762\u6211\u4eec\u770b\u5b8c\u4e8b\u4ef6\u5faa\u73af\u7684\u4e3b\u6d41\u7a0b\uff0c\u8fd9\u91cc\u6211\u4eec\u5f00\u59cb\u770b\u7b2c\u4e00\u4e2a\u547d\u4ee4set\u7684\u5728redis\u4e2d\u7684\u6267\u884c\u6d41\u7a0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4e00\u4e2aset\u547d\u4ee4\u7684\u6267\u884c\u6d41\u7a0b\n\n\/\/src\/networking.c\n\/\/\u5728\u4e0a\u6587\u521b\u5efa\u64cd\u4f5credis\u7684\u5ba2\u6237\u7aef\u65f6\u4f7f\u7528readQueryFromClient\u65b9\u6cd5\u63a5\u6536\u5904\u7406\u8fde\u63a5\u65b0\u5230\u8fbe\u7684\u547d\u4ee4\n\n\/*\n * \u5904\u7406\u8fde\u63a5\u8bf7\u6c42\u5185\u5bb9\n *\/\nvoid readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask)\n{\n    processInputBuffer(c);\n}\n\n\/\/ \u5728readQueryFromClient\u901a\u8fc7\u8be5\u65b9\u6cd5\u5904\u7406\u8bf7\u6c42\u5185\u5bb9\nvoid processInputBuffer(redisClient *c)\n{\n\n    processMultibulkBuffer(c)\n    processCommand(c)\n}\n\n\nint processMultibulkBuffer(redisClient *c)\n{\n    char *newline = NULL;\n    int pos = 0, ok;\n    long long ll;\n\n    \/\/ \u8bfb\u5165\u547d\u4ee4\u7684\u53c2\u6570\u4e2a\u6570\n    \/\/ \u6bd4\u5982\u6267\u884c\u547d\u4ee4\u662fset aa 23333, \u5219\u8fd9\u8fb9\u7684c->querybuf\u663e\u793a\u4e3a\n    \/\/ *3\\r\\n<span class=\"katex math inline\">3\\r\\nset\\r\\n<\/span>2\\r\\naa\\r\\n<span class=\"katex math inline\">5\\r\\n23333\n    \/\/ \u5ba2\u6237\u7aef\u53d1\u8fc7\u6765\u7684\u547d\u4ee4\u4ee5 * \u5f00\u5934\n    fprintf(stderr, \"set aa 23333 c->querybuf %s \\n\", c->querybuf);\n\n    if (c->multibulklen == 0)\n    {\n        \/* Multi bulk length cannot be read without a \\r\\n *\/\n        newline = strchr(c->querybuf, '\\r');\n\n        \/* We know for sure there is a whole line since newline != NULL,\n         * so go ahead and find out the multi bulk length. *\/\n        redisAssertWithInfo(c, NULL, c->querybuf&#91;0] == '*');\n        \/\/ \u83b7\u5f97\u53c2\u6570\u7684\u4e2a\u6570,\u4ece*\u4e4b\u540e\u5230\u7b2c\u4e00\u4e2a\\r\\n\u4e4b\u524d,\u5b58\u5230ll\u4e2d\n        ok = string2ll(c->querybuf + 1, newline - (c->querybuf + 1), &amp;ll);\n        pos = (newline - c->querybuf) + 2;\n        \/\/ \u8bbe\u7f6e\u53c2\u6570\u6570\u91cf\n        c->multibulklen = ll;\n        \/* Setup argv array on client structure *\/\n        if (c->argv)\n            zfree(c->argv);\n        c->argv = zmalloc(sizeof(robj *) * c->multibulklen);\n    }\n\n    \/\/ \u4ece c->querybuf \u4e2d\u8bfb\u5165\u53c2\u6570\uff0c\u5e76\u521b\u5efa\u5404\u4e2a\u53c2\u6570\u5bf9\u8c61\u5230 c->argv\n    while (c->multibulklen)\n    {\n\n        \/* Read bulk length if unknown *\/\n        if (c->bulklen == -1)\n        {\n\n            \/\/ \u786e\u4fdd \"\\r\\n\" \u5b58\u5728\n            newline = strchr(c->querybuf + pos, '\\r');\n            \/* Buffer should also contain \\n *\/\n            if (newline - (c->querybuf) > ((signed)sdslen(c->querybuf) - 2))\n                break;\n\n            \/\/ \u68c0\u67e5\u521a\u597d\u662f<\/span>\n            \/\/ *3\\r\\n<span class=\"katex math inline\">3\\r\\nset\\r\\n<\/span>2\\r\\naa\\r\\n<span class=\"katex math inline\">5\\r\\n23333\n            if (c->querybuf&#91;pos] != '<\/span>')\n            {\n                addReplyErrorFormat(c,\n                                    \"Protocol error: expected '<span class=\"katex math inline\">', got '%c'\",\n                                    c->querybuf&#91;pos]);\n                setProtocolError(c, pos);\n                return REDIS_ERR;\n            }\n\n            \/\/ \u8bfb\u53d6\u957f\u5ea6\n            \/\/ \u6bd4\u5982 *3\\r\\n<\/span>3\\r\\nset\\r\\n<span class=\"katex math inline\">2\\r\\naa\\r\\n<\/span>5\\r\\n23333\n            ok = string2ll(c->querybuf + pos + 1, newline - (c->querybuf + pos + 1), &amp;ll);\n            if (!ok || ll &lt; 0 || ll > 512 * 1024 * 1024)\n            {\n                addReplyError(c, \"Protocol error: invalid bulk length\");\n                setProtocolError(c, pos);\n                return REDIS_ERR;\n            }\n\n            \/\/ \u5b9a\u4f4d\u5230\u53c2\u6570\u7684\u5f00\u5934\n            \/\/ \u6bd4\u5982\n            \/\/ *3\\r\\n<span class=\"katex math inline\">3\\r\\nset\\r\\n<\/span>2\\r\\naa\\r\\n<span class=\"katex math inline\">5\\r\\n23333\n            \/\/ pos = 4 \u503c\u4e3a<\/span>\n            \/\/ \u589e\u52a0\u540e pos = 8 \u503c\u4e3as\n            \/\/ pos = 13 \u503c\u4e3a<span class=\"katex math inline\">\/\/ \u589e\u52a0\u540e pos = 17 \u503c\u4e3aa\n            \/\/ pos = 22 \u503c\u4e3a<\/span>\n            \/\/ \u589e\u52a0\u540e pos = 26 \u503c\u4e3a2\n            pos += newline - (c->querybuf + pos) + 2;\n            \/\/ \u53c2\u6570\u7684\u957f\u5ea6\n            c->bulklen = ll;\n        }\n\n        \/* Read bulk argument *\/\n        \/\/ \u8bfb\u5165\u53c2\u6570\n        if (sdslen(c->querybuf) - pos &lt; (unsigned)(c->bulklen + 2))\n            \/* Not enough data (+2 == trailing \\r\\n) *\/\n            break;\n        }\n        else\n        {\n                c->argv&#91;c->argc++] =\n                    createStringObject(c->querybuf + pos, c->bulklen);\n                pos += c->bulklen + 2;\n\n            \/\/ \u8fd8\u9700\u8981\u8bfb\u53d6\u7684\u53c2\u6570\u4e2a\u6570\n            c->multibulklen--;\n        }\n    }\n\n    \/* Trim to pos *\/\n    if (pos)\n        sdsrange(c->querybuf, pos, -1);\n\n    \/* We're done when c->multibulk == 0 *\/\n    if (c->multibulklen == 0)\n        return REDIS_OK;\n\n    \/* Still not read to process the command *\/\n    \/\/ \u5982\u679c\u8fd8\u6709\u53c2\u6570\u672a\u8bfb\u53d6\u5b8c\uff0c\u90a3\u4e48\u5c31\u534f\u8bae\u5185\u5bb9\u6709\u9519\n    return REDIS_ERR;\n}\n\n\/* If this function gets called we already read a whole\n * command, arguments are in the client argv\/argc fields.\n * processCommand() execute the command or prepare the\n * server for a bulk read from the client.\n *\n * If 1 is returned the client is still alive and valid and\n * other operations can be performed by the caller. Otherwise\n * if 0 is returned the client was destroyed (i.e. after QUIT). \n *\n *\/\nint processCommand(redisClient *c)\n{\n\n    \/* Now lookup the command and check ASAP about trivial error conditions\n     * such as wrong arity, bad command name and so forth. *\/\n    \/\/ \u6839\u636e\u5b57\u7b26\u4e32\u67e5\u627e\u547d\u4ee4, \u68c0\u67e5\u53c2\u6570\u4e2a\u6570\n    c->cmd = c->lastcmd = lookupCommand(c->argv&#91;0]->ptr);\n    if (!c->cmd)\n    {\n        \/\/ \u6ca1\u6709\u547d\u4ee4\n    }\n    else if ((c->cmd->arity > 0 &amp;&amp; c->cmd->arity != c->argc) ||\n             (c->argc &lt; -c->cmd->arity))\n    {\n        \/\/\u53c2\u6570\u9519\u8bef\n    }\n    \/\/ \u6267\u884c\u547d\u4ee4\n    call(c, REDIS_CALL_FULL);\n}\n\n\/* Call() is the core of Redis execution of a command *\/\nvoid call(redisClient *c, int flags)\n{\n    \/\/ \u6267\u884c\u547d\u4ee4\u7684\u51fd\u6570.\u5728\u524d\u9762\u7684lookupCommand\u4f1a\u7ed9cmd\u8bbe\u7f6e\u503c\n    \/\/ \u5728\u4f8b\u5b50set aa 23333 \u4e2d\u76f8\u5f53\u4e8e\u6267\u884c setCommand\u65b9\u6cd5\n    c->cmd->proc(c);\n}\n\n\/* SET key value &#91;NX] &#91;XX] &#91;EX &lt;seconds>] &#91;PX &lt;milliseconds>] *\/\nvoid setCommand(redisClient *c)\n{\n    int j;\n    robj *expire = NULL;\n    int unit = UNIT_SECONDS;\n    int flags = REDIS_SET_NO_FLAGS;\n\n    \/\/ \u8bbe\u7f6e\u9009\u9879\u53c2\u6570\n    for (j = 3; j &lt; c->argc; j++)\n    {\n        char *a = c->argv&#91;j]->ptr;\n        robj *next = (j == c->argc - 1) ? NULL : c->argv&#91;j + 1];\n\n        if ((a&#91;0] == 'n' || a&#91;0] == 'N') &amp;&amp;\n            (a&#91;1] == 'x' || a&#91;1] == 'X') &amp;&amp; a&#91;2] == '\\0')\n        {\n            flags |= REDIS_SET_NX;\n        }\n        else if ((a&#91;0] == 'x' || a&#91;0] == 'X') &amp;&amp;\n                 (a&#91;1] == 'x' || a&#91;1] == 'X') &amp;&amp; a&#91;2] == '\\0')\n        {\n            flags |= REDIS_SET_XX;\n        }\n        else if ((a&#91;0] == 'e' || a&#91;0] == 'E') &amp;&amp;\n                 (a&#91;1] == 'x' || a&#91;1] == 'X') &amp;&amp; a&#91;2] == '\\0' &amp;&amp; next)\n        {\n            unit = UNIT_SECONDS;\n            expire = next;\n            j++;\n        }\n        else if ((a&#91;0] == 'p' || a&#91;0] == 'P') &amp;&amp;\n                 (a&#91;1] == 'x' || a&#91;1] == 'X') &amp;&amp; a&#91;2] == '\\0' &amp;&amp; next)\n        {\n            unit = UNIT_MILLISECONDS;\n            expire = next;\n            j++;\n        }\n        else\n        {\n            addReply(c, shared.syntaxerr);\n            return;\n        }\n    }\n\n    \/\/ \u5bf9\u53c2\u6570\u7f16\u7801\n    c->argv&#91;2] = tryObjectEncoding(c->argv&#91;2]);\n\n    setGenericCommand(c, flags, c->argv&#91;1], c->argv&#91;2], expire, unit, NULL, NULL);\n}\n\nvoid setGenericCommand(redisClient *c, int flags, robj *key, robj *val, robj *expire, int unit, robj *ok_reply, robj *abort_reply)\n{\n\n    long long milliseconds = 0; \/* initialized to avoid any harmness warning *\/\n\n    \/\/ \u53d6\u51fa\u8fc7\u671f\u65f6\u95f4\n    if (expire)\n    {\n\n        \/\/ \u53d6\u51fa expire \u53c2\u6570\u7684\u503c\n        \/\/ T = O(N)\n        if (getLongLongFromObjectOrReply(c, expire, &amp;milliseconds, NULL) != REDIS_OK)\n            return;\n\n        \/\/ expire \u53c2\u6570\u7684\u503c\u4e0d\u6b63\u786e\u65f6\u62a5\u9519\n        if (milliseconds &lt;= 0)\n        {\n            addReplyError(c, \"invalid expire time in SETEX\");\n            return;\n        }\n\n        \/\/ \u5c06\u79d2\u8f6c\u6210\u6beb\u79d2\n        if (unit == UNIT_SECONDS)\n            milliseconds *= 1000;\n    }\n\n    \/\/ \u5982\u679c\u8bbe\u7f6e\u4e86 NX \u6216\u8005 XX \u53c2\u6570\uff0c\u90a3\u4e48\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u4e0d\u7b26\u5408\u8fd9\u4e24\u4e2a\u8bbe\u7f6e\n    \/\/ \u5728\u6761\u4ef6\u4e0d\u7b26\u5408\u65f6\u62a5\u9519\uff0c\u62a5\u9519\u7684\u5185\u5bb9\u7531 abort_reply \u53c2\u6570\u51b3\u5b9a\n    if ((flags &amp; REDIS_SET_NX &amp;&amp; lookupKeyWrite(c->db, key) != NULL) ||\n        (flags &amp; REDIS_SET_XX &amp;&amp; lookupKeyWrite(c->db, key) == NULL))\n    {\n        addReply(c, abort_reply ? abort_reply : shared.nullbulk);\n        return;\n    }\n\n    \/\/ \u5c06\u952e\u503c\u5173\u8054\u5230\u6570\u636e\u5e93\n    setKey(c->db, key, val);\n    server.dirty++;\n\n    \/\/ \u4e3a\u952e\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\n    if (expire)\n        setExpire(c->db, key, mstime() + milliseconds);\n\n    \/\/ \u53d1\u9001\u901a\u77e5\n    notifyKeyspaceEvent(REDIS_NOTIFY_STRING, \"set\", key, c->db->id);\n\n    \/\/ \u53d1\u9001\u901a\u77e5\n    if (expire)\n        notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,\n                            \"expire\", key, c->db->id);\n\n    \/\/ \u8bbe\u7f6e\u6210\u529f\u56de\u590d\u5ba2\u6237\u7aef\n    addReply(c, ok_reply ? ok_reply : shared.ok);\n}\n\n\/* High level Set operation. This function can be used in order to set\n * a key, whatever it was existing or not, to a new object.\n *\n * 1) The ref count of the value object is incremented.\n *\n * 2) clients WATCHing for the destination key notified.\n *\n * 3) The expire time of the key is reset (the key is made persistent). \n *\/\nvoid setKey(redisDb *db, robj *key, robj *val)\n{\n\n    \/\/ \u6dfb\u52a0\u6216\u8986\u5199\u6570\u636e\u5e93\u4e2d\u7684\u952e\u503c\u5bf9\n    if (lookupKeyWrite(db, key) == NULL)\n    {\n        \/\/ \u6ca1\u6709\u503c\u5c31\u6dfb\u52a0\u65b0\u503c\n        dbAdd(db, key, val);\n    }\n    else\n    {\n        \/\/ \u6709\u503c\u5c31\u8986\u76d6\n        dbOverwrite(db, key, val);\n    }\n\n    incrRefCount(val);\n\n    \/\/ \u79fb\u9664\u952e\u7684\u8fc7\u671f\u65f6\u95f4\n    removeExpire(db, key);\n\n    \/\/ \u53d1\u9001\u952e\u4fee\u6539\u901a\u77e5\n    signalModifiedKey(db, key);\n}\n\n\/* Add the key to the DB. It's up to the caller to increment the reference\n * counter of the value if needed.\n * The program is aborted if the key already exists. \n *\n *\/\nvoid dbAdd(redisDb *db, robj *key, robj *val)\n{\n\n    \/\/ \u590d\u5236\u64cd\u4f5c\u7684\u952e\n    sds copy = sdsdup(key->ptr);\n\n    \/\/ \u5c1d\u8bd5\u6dfb\u52a0\u952e\u503c\u5bf9\n    int retval = dictAdd(db->dict, copy, val);\n\n    \/\/ \u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u90a3\u4e48\u505c\u6b62\n    redisAssertWithInfo(NULL, key, retval == REDIS_OK);\n\n    \/\/ \u5982\u679c\u5f00\u542f\u4e86\u96c6\u7fa4\u6a21\u5f0f\uff0c\u90a3\u4e48\u5c06\u952e\u4fdd\u5b58\u5230\u69fd\u91cc\u9762\n    if (server.cluster_enabled)\n        slotToKeyAdd(key);\n}\n\n\/* Overwrite an existing key with a new value. Incrementing the reference\n * count of the new value is up to the caller.\n *\n * This function does not modify the expire time of the existing key.\n *\n * The program is aborted if the key was not already present. \n *\/\nvoid dbOverwrite(redisDb *db, robj *key, robj *val)\n{\n    dictEntry *de = dictFind(db->dict, key->ptr);\n\n    \/\/ \u8282\u70b9\u5fc5\u987b\u5b58\u5728\uff0c\u5426\u5219\u4e2d\u6b62\n    redisAssertWithInfo(NULL, key, de != NULL);\n\n    \/\/ \u8986\u5199\u65e7\u503c\n    dictReplace(db->dict, key->ptr, val);\n}\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/download.redis.io\/releases\/redis-3.0.0.tar.gz\" target=\"_blank\" rel=\"noopener\">\u57fa\u4e8e\u7248\u672c3.0.0\u7248\u672c,<\/a>\u70b9\u51fb\u4e0b\u8f7dhttps:\/\/download.redis.io\/releases\/redis-3.0.0.tar.gz<\/p>\n\n\n\n<p><a href=\"https:\/\/www.ccagml.com\/?p=387\">\u672c\u6587\u5730\u5740<\/a>\uff0chttps:\/\/www.ccagml.com\/?p=387<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u6211\u4eec\u770b\u5b8c\u4e8b\u4ef6\u5faa\u73af\u7684\u4e3b\u6d41\u7a0b\uff0c\u8fd9\u91cc\u6211\u4eec\u5f00\u59cb\u770b\u7b2c\u4e00\u4e2a\u547d\u4ee4set\u7684\u5728redis\u4e2d\u7684\u6267\u884c\u6d41\u7a0b \u57fa\u4e8e\u7248\u672c3.0.0\u7248\u672c<a href=\"https:\/\/www.ccagml.com\/?p=387\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">redis\u6e90\u7801\u4ecemain\u5f00\u59cb6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,22],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":2,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":389,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions\/389"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}