{"id":414,"date":"2020-03-29T23:12:00","date_gmt":"2020-03-29T15:12:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=414"},"modified":"2021-06-06T23:14:40","modified_gmt":"2021-06-06T15:14:40","slug":"redis%e6%ba%90%e7%a0%81%e4%bb%8emain%e5%bc%80%e5%a7%8b13","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=414","title":{"rendered":"redis\u6e90\u7801\u4ecemain\u5f00\u59cb13"},"content":{"rendered":"\n<p>redis\u6e90\u7801\u4ecemain\u5f00\u59cb12\u4e2d\u6211\u4eec\u770b\u4e86list\u5bf9\u8c61\uff0c\u672c\u7ae0\u6765\u770bset\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>13 redis\u5bf9\u8c61\u4e4bset\n\n\u540c\u6837\u7684set\u7c7b\u578b\u5bf9\u8c61\u4e5f\u6709\u4e24\u79cd\u7f16\u7801\u65b9\u5f0f,\u4e00\u79cd\u662f\u54c8\u5e0c\u8868,\u4e00\u79cd\u662f\u6574\u6570\u96c6\u5408\n#define REDIS_ENCODING_HT 2      \/* Encoded as hash table *\/\n#define REDIS_ENCODING_INTSET 6  \/* Encoded as intset *\/\n\n\u4ee5\u4e0b\u4e3a\u4e24\u79cd\u7f16\u7801\u65b9\u5f0f\u7684set\u5bf9\u8c61\u7684\u521b\u5efa\u65b9\u5f0f\n\n\/\/\u54c8\u5e0c\u8868\u7f16\u7801\u7684\u96c6\u5408\nrobj *createSetObject(void) {\n    dict *d = dictCreate(&amp;setDictType,NULL);\n    robj *o = createObject(REDIS_SET,d);\n    o->encoding = REDIS_ENCODING_HT;\n    return o;\n}\n\n\/\/\u6574\u6570\u96c6\u5408\u7f16\u7801\u7684\u96c6\u5408\nrobj *createIntsetObject(void) {\n    intset *is = intsetNew();\n    robj *o = createObject(REDIS_SET,is);\n    o->encoding = REDIS_ENCODING_INTSET;\n    return o;\n}\n\n\/\/ \u4e0b\u9762\u5148\u6765\u770b\u7f16\u7801\u65b9\u5f0f\u4e3a\u54c8\u5e0c\u8868\u7684set\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b\n\n\/\/ \u7b2c\u4e00\u6b65,\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\ndict *dictCreate(dictType *type,void *privDataPtr)\n{\n    dict *d = zmalloc(sizeof(*d));\n    _dictInit(d,type,privDataPtr);\n    return d;\n}\n\n\/\/ redis\u4e2d\u5b57\u5178\u7684\u7ed3\u6784\ntypedef struct dict {\n    dictType *type; \/\/ \u4e00\u4e2a\u7c7b\u578b\u51fd\u6570\n    void *privdata; \/\/ \u79c1\u6709\u6570\u636e\n    dictht ht&#91;2];     \/\/ \u54c8\u5e0c\u8868,\u6709\u4e24\u4e2a,\u5728\u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c\u7684\u8fc7\u7a0b\u4f1a\u4f7f\u7528\u5230\n    int rehashidx; \/* \u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c rehashing not in progress if rehashidx == -1 *\/\n    int iterators; \/* \u5f53\u524d\u8fd0\u884c\u7684\u8fed\u4ee3\u5668\u6570\u91cf *\/\n} dict;\n\n\/* \u4e0d\u540c\u7c7b\u578b\u7684\u54c8\u5e0c\u8868 *\/\ndictType setDictType = {\n    dictEncObjHash,            \/* hash function *\/\n    NULL,                      \/* key dup *\/\n    NULL,                      \/* val dup *\/\n    dictEncObjKeyCompare,      \/* key compare *\/\n    dictRedisObjectDestructor, \/* key destructor *\/\n    NULL                       \/* val destructor *\/\n};\n\n\n\/\/\u521d\u59cb\u5316\u54c8\u5e0c\u8868\nint _dictInit(dict *d, dictType *type, void *privDataPtr)\n{\n    _dictReset(&amp;d->ht&#91;0]); \/\/ \u521d\u59cb\u5316\u5c5e\u6027\n    _dictReset(&amp;d->ht&#91;1]); \/\/ \u521d\u59cb\u5316\u5c5e\u6027\n    d->type = type;     \/\/ \u8bbe\u7f6e\u7c7b\u578bdictType,\u4e0d\u540c\u7c7b\u578b\u4f1a\u6709\u4e0d\u540c\u7684\u54c8\u5e0c\u51fd\u6570,\u952e\u503c\u7684\u590d\u5236\u6bd4\u8f83\u9500\u6bc1\u7684\u76f8\u5173\u51fd\u6570\n    d->privdata = privDataPtr;     \/\/ \u54c8\u5e0c\u8868\u79c1\u6709\u6570\u636e\n    d->rehashidx = -1; \/\/ \u8bbe\u7f6e\u4e0d\u5728\u91cd\u65b0\u8ba1\u7b97\u54c8\u5e0c\u4e2d\n    d->iterators = 0; \/\/ \u8bbe\u7f6e\u8fdb\u884c\u4e2d\u7684\u8fed\u4ee3\u5668\u6570\u91cf\n    return DICT_OK;\n}\n\n\/\/ \u7b2c\u4e8c\u6b65, \u521b\u5efa\u4e00\u4e2aset\u7c7b\u578b\u5bf9\u8c61, \u628a\u521a\u521a\u521b\u5efa\u7684\u54c8\u5e0c\u8868\u4f20\u5165\nrobj *o = createObject(REDIS_SET,d);\n\n\/\/ \u7b2c\u4e09\u6b65, \u8bbe\u7f6e\u7f16\u7801\u4e3a\u54c8\u5e0c\u8868\no->encoding = REDIS_ENCODING_HT;\n\n\n\n\/\/\u4e0b\u9762\u6211\u4eec\u770b\u6574\u6570\u96c6\u5408\u7684\u96c6\u5408\n\/\/ \u7b2c\u4e00\u6b65,\u521b\u5efa\u6574\u6570\u96c6\u5408\u7684\u7ed3\u6784\nintset *is = intsetNew();\n\n\/\/\u521b\u5efa\u7a7a\u7684\u6574\u6570\u96c6\u5408\nintset *intsetNew(void) {\n    intset *is = zmalloc(sizeof(intset));\n    is->encoding = intrev32ifbe(INTSET_ENC_INT16);     \/\/ \u8bbe\u7f6e\u521d\u59cb\u7f16\u7801\n    is->length = 0;     \/\/ \u521d\u59cb\u5316\u5143\u7d20\u6570\u91cf\n    return is;\n}\n\n\/\/ \u7b2c\u4e8c\u6b65,\u521b\u5efaset\u5bf9\u8c61\nrobj *o = createObject(REDIS_SET,is);\n\n\/\/ \u7b2c\u4e09\u6b65,\u8bbe\u7f6e\u7f16\u7801\u7c7b\u578b\no->encoding = REDIS_ENCODING_INTSET;\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=414\">\u672c\u6587\u5730\u5740<\/a>\uff0chttps:\/\/www.ccagml.com\/?p=414<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u6e90\u7801\u4ecemain\u5f00\u59cb12\u4e2d\u6211\u4eec\u770b\u4e86list\u5bf9\u8c61\uff0c\u672c\u7ae0\u6765\u770bset\u5bf9\u8c61 \u57fa\u4e8e\u7248\u672c3.0.0\u7248\u672c,\u70b9\u51fb\u4e0b\u8f7dh<a href=\"https:\/\/www.ccagml.com\/?p=414\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">redis\u6e90\u7801\u4ecemain\u5f00\u59cb13<\/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\/414"}],"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=414"}],"version-history":[{"count":3,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}