{"id":424,"date":"2020-04-25T21:07:00","date_gmt":"2020-04-25T13:07:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=424"},"modified":"2021-06-21T21:20:59","modified_gmt":"2021-06-21T13:20:59","slug":"redis%e6%ba%90%e7%a0%81%e4%bb%8emain%e5%bc%80%e5%a7%8b15","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=424","title":{"rendered":"redis\u6e90\u7801\u4ecemain\u5f00\u59cb15"},"content":{"rendered":"\n<p>\u540c\u6837\u7684hash\u7c7b\u578b\u5bf9\u8c61\u4e5f\u6709\u4e24\u79cd\u7f16\u7801\u65b9\u5f0f,\u4e00\u79cd\u662f\u538b\u7f29\u5217\u8868,\u4e00\u79cd\u662f\u54c8\u5e0c\u8868\u54c8\u5e0c\u5bf9\u8c61\u9ed8\u8ba4\u521b\u5efa\u7684\u662f\u538b\u7f29\u5217\u8868\u7f16\u7801\u65b9\u5f0f,\u5f53\u8fbe\u5230\u4e00\u5b9a\u6761\u4ef6\u540e\u4f1a\u8f6c\u6362\u6210\u54c8\u5e0c\u7f16\u7801\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define REDIS_ENCODING_ZIPLIST 5 \/* Encoded as ziplist *\/\n#define REDIS_ENCODING_HT 2      \/* Encoded as hash table *\/\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u5bf9\u8c61\nrobj *createHashObject(void) {\n    unsigned char *zl = ziplistNew();\n    robj *o = createObject(REDIS_HASH, zl);\n    o-&gt;encoding = REDIS_ENCODING_ZIPLIST;\n    return o;\n}\n\n\/\/\u5e38\u89c1\u7684\u6761\u4ef6\u6709\u4ee5\u4e0b\u8fd9\u4e9b\n#define REDIS_HASH_MAX_ZIPLIST_ENTRIES 512\n#define REDIS_HASH_MAX_ZIPLIST_VALUE 64\n\nserver.hash_max_ziplist_entries \u4fdd\u5b58\u7684\u952e\u503c\u6570\u91cf\u5c0f\u4e8e\u9ed8\u8ba4\u503c512\nserver.hash_max_ziplist_value  \u952e\u503c\u7684\u957f\u5ea6\u90fd\u5c0f\u4e8e\u9ed8\u8ba4\u503c64\u5b57\u8282\n\n\n\/\/ \u5177\u4f53\u7684\u8f6c\u6362\u51fd\u6570\u4e3ahashTypeConvert\nvoid hashTypeConvert(robj *o, int enc) {\n    if (o-&gt;encoding == REDIS_ENCODING_ZIPLIST) {\n        hashTypeConvertZiplist(o, enc);\n    } else if (o-&gt;encoding == REDIS_ENCODING_HT) {\n        redisPanic(\"Not implemented\");\n    } else {\n        redisPanic(\"Unknown hash encoding\");\n    }\n}\n\n\/\/ \u5c06REDIS_ENCODING_ZIPLIST\u7f16\u7801\u5bf9\u8c61\u8f6c\u6210REDIS_ENCODING_HT\nvoid hashTypeConvertZiplist(robj *o, int enc) {\n    redisAssert(o-&gt;encoding == REDIS_ENCODING_ZIPLIST);\n    if (enc == REDIS_ENCODING_ZIPLIST) {\n    } else if (enc == REDIS_ENCODING_HT) {\n        hashTypeIterator *hi;\n        dict *dict;\n        int ret;\n        hi = hashTypeInitIterator(o);\n        dict = dictCreate(&amp;hashDictType, NULL);\n        while (hashTypeNext(hi) != REDIS_ERR) {\n            robj *field, *value;\n            field = hashTypeCurrentObject(hi, REDIS_HASH_KEY);\n            field = tryObjectEncoding(field);\n\n\n            value = hashTypeCurrentObject(hi, REDIS_HASH_VALUE);\n            value = tryObjectEncoding(value);\n\n\n            ret = dictAdd(dict, field, value);\n            if (ret != DICT_OK) {\n                redisLogHexDump(REDIS_WARNING,\"ziplist with dup elements dump\",\n                    o-&gt;ptr,ziplistBlobLen(o-&gt;ptr));\n                redisAssert(ret == DICT_OK);\n            }\n        }\n\n        hashTypeReleaseIterator(hi);\n        zfree(o-&gt;ptr);\n        \/\/ \u8f6c\u6362\u7f16\u7801\u65b9\u5f0f\u548c\u65b0\u7684\u5b57\u5178\n        o-&gt;encoding = REDIS_ENCODING_HT;\n        o-&gt;ptr = dict;\n\n    } else {\n        redisPanic(\"Unknown hash encoding\");\n    }\n}\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=424\">\u672c\u6587\u5730\u5740<\/a>\uff0chttps:\/\/www.ccagml.com\/?p=424<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u6837\u7684hash\u7c7b\u578b\u5bf9\u8c61\u4e5f\u6709\u4e24\u79cd\u7f16\u7801\u65b9\u5f0f,\u4e00\u79cd\u662f\u538b\u7f29\u5217\u8868,\u4e00\u79cd\u662f\u54c8\u5e0c\u8868\u54c8\u5e0c\u5bf9\u8c61\u9ed8\u8ba4\u521b\u5efa\u7684\u662f\u538b\u7f29\u5217\u8868\u7f16\u7801\u65b9\u5f0f,\u5f53\u8fbe<a href=\"https:\/\/www.ccagml.com\/?p=424\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">redis\u6e90\u7801\u4ecemain\u5f00\u59cb15<\/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\/424"}],"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=424"}],"version-history":[{"count":4,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/424\/revisions\/434"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}