{"id":201136,"date":"2025-05-10T09:46:19","date_gmt":"2025-05-10T01:46:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201136\/"},"modified":"2025-05-10T09:46:19","modified_gmt":"2025-05-10T01:46:19","slug":"redis%e4%b9%8bsds%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201136\/","title":{"rendered":"Redis\u4e4bSDS\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e4bSDS\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u4e4bSDS\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528\u300b\uff0c\u804a\u804a\u6570\u636e\u7ed3\u6784\u3001RedisSDS\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<h2>\u5e8f\u8a00<\/h2>\n<p>Redis\u7684\u51e0\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\u6709\u5b57\u7b26\u4e32\uff08String\uff09\u3001\u54c8\u5e0c\uff08Hash\uff09\u3001\u5217\u8868\uff08List\uff09\u3001\u96c6\u5408\uff08Set\uff09\u3001\u6709\u5e8f\u96c6\u5408\uff08Sorted Set\uff09\uff0c\u8fd9\u4e9b\u662f\u6700\u5e38\u89c1\u7684\uff0c\u4e5f\u80fd\u5728\u5b98\u7f51\u4e0a\u67e5\u770b\u5230\u3002<\/p>\n<p>\u5b98\u7f51\u94fe\u63a5\uff1a<\/p>\n<h2>\u5b57\u7b26\u4e32<\/h2>\n<p>\u524d\u9762\u4e5f\u63d0\u5230\u8fc7\u5b57\u7b26\u4e32\u662f\u8bbe\u8ba1\u4e86\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32SDS\uff08Simple Dynamic String\uff09\u7ed3\u6784\u6765\u8868\u793a\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u5347\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u6548\u7387\uff0c\u5e76\u53ef\u4ee5\u4fdd\u5b58\u4e8c\u8fdb\u5236\u6570\u636e\u3002<\/p>\n<blockquote>\n<p>\u5148\u601d\u8003\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>Redis\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u6ca1\u6709\u590d\u7528C\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u65b9\u6cd5\uff0c\u800c\u9009\u7528\u4e86SDS\u5462\uff1f<\/p>\n<\/blockquote>\n<h3>char*\u5b57\u7b26\u4e32\u6570\u7ec4<\/h3>\n<p>C\u8bed\u8a00\u5b9e\u73b0\u5b57\u7b26\u4e32\u4f7f\u7528\u7684\u662fchar*\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5b83\u662f\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u4e00\u6b21\u5b58\u653e\u4e86\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u4e14\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u662f\u201c\\0\u201d\uff0c\u7528\u6765\u6807\u8bc6\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u4f4d\u7f6e\uff0c\u5982\u4e0b\u56fe\uff0c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237464363ae6973749a1.png\" class=\"aligncenter\"><\/p>\n<p>\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u7684\u6240\u6709\u5b57\u7b26\u4e32\u6ca1\u6709\u5206\u9694\u7b26\u8ba1\u7b97\u673a\u5c31\u6ca1\u529e\u6cd5\u533a\u5206\u5b57\u7b26\u4e32\u4e0e\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u4f4d\u7f6e\u3002\u5728C\u8bed\u8a00\u6807\u51c6\u5e93\u4e2d\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u51fd\u6570\u5c31\u4f1a\u901a\u8fc7\u68c0\u67e5\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u662f\u5426\u6709\u201c\\0\u201d\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u7ed3\u675f\u3002\u4f8b\u5982\u5b57\u7b26\u4e32\u64cd\u4f5c\u51fd\u6570strlen\u51fd\u6570\uff0c\u5b83\u5c31\u662f\u5728\u904d\u5386\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u8fdb\u884c\u8ba1\u6570\uff0c\u76f4\u5230\u68c0\u67e5\u5230\u201c\\0\u201d\uff0c\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)\u3002\u6d41\u7a0b\u5982\u4e0b\uff0c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237464363ae6973f201b.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32SDS<\/h2>\n<p>SDS\u7684\u6570\u636e\u7ed3\u6784\u91cc\u5305\u542b\uff1a\u5b57\u7b26\u4e32\u5b9e\u9645\u957f\u5ea6\uff0c\u5b57\u7b26\u4e32\u5206\u914d\u7a7a\u95f4\u957f\u5ea6\uff0cSDS\u7c7b\u578b\uff0c\u5b57\u7b26\u6570\u7ec4\uff0c\u5176\u4e2d\u5b57\u7b26\u6570\u7ec4buf[]\u7528\u6765\u4fdd\u5b58\u5b9e\u9645\u6570\u636e\uff0c\u5982\u4e0b\u56fe\uff0c<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237464463ae69744c7e3.png\" class=\"aligncenter\"><\/p>\n<p>\u518d\u6765\u770b\u770b\u7c7b\u4f3c\u7684\u5b57\u7b26\u64cd\u4f5c\u51fd\u6570sdslen\u51fd\u6570\u7684\u6e90\u7801\uff08\u5728sds.h\u6587\u4ef6\u4e2d\uff09\uff0c\u76f4\u63a5\u6839\u636eSDS\u7c7b\u578b\u8fd4\u56de\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u73b0\u6709\u957f\u5ea6\uff0c\u907f\u514d\u4e86\u5bf9\u5b57\u7b26\u4e32\u7684\u904d\u5386\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u6210\u4e86O(1)\uff0c\u5f53\u7136\u4e5f\u4f1a\u4ed8\u51fa\u4e00\u70b9\u4ee3\u4ef7\u589e\u52a0\u4e86\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u90fd\u662f\u8bbe\u8ba1\u4eba\u5458\u8ba9\u6570\u636e\u64cd\u4f5c\u66f4\u52a0\u9ad8\u6548\u3002\u6e90\u7801\u5982\u4e0b\uff0c<\/p>\n<pre>static inline size_t sdslen(const sds s) {\n    unsigned char flags = s[-1];\n    switch(flags&amp;SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            return SDS_TYPE_5_LEN(flags);\n        case SDS_TYPE_8:\n            return SDS_HDR(8,s)-&gt;len;\n        case SDS_TYPE_16:\n            return SDS_HDR(16,s)-&gt;len;\n        case SDS_TYPE_32:\n            return SDS_HDR(32,s)-&gt;len;\n        case SDS_TYPE_64:\n            return SDS_HDR(64,s)-&gt;len;\n    }\n    return 0;\n}<\/pre>\n<p>\u518d\u6765\u770b\u4e00\u4e0b\u5b57\u7b26\u4e32\u7684\u62f7\u8d1d\u6e90\u7801\uff0c\u64cd\u4f5c\u90fd\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u7684\u73b0\u6709\u957f\u5ea6\uff0c\u62f7\u8d1d\u540e\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<pre>sds sdscpylen(sds s, const char *t, size_t len) {\n    \/\/ \u5224\u65ad\u5b57\u7b26\u4e32\u6570\u7ec4\u5206\u914d\u7684\u7a7a\u95f4\u957f\u5ea6\u662f\u4e0d\u662f\u5c0f\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4\u5f53\u524d\u957f\u5ea6\n    if (sdsalloc(s) \n<p>SDS\u628a\u76ee\u6807\u5b57\u7b26\u4e32\u7684\u7a7a\u95f4\u68c0\u67e5\u548c\u6269\u5bb9\u5c01\u88c5\u5728\u4e86sdsMakeRoomFor\u51fd\u6570\u4e2d\uff0c\u8ffd\u52a0\u3001\u6253\u5370\u3001\u590d\u5236\u7b49\u64cd\u4f5c\u90fd\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u3002\u53ef\u4ee5\u770b\u5230\u8be5\u51fd\u6570\u6839\u636esds\u7684\u4fe1\u606f\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\uff0c\u6e90\u7801\u5982\u4e0b\uff0c<\/p>\n<pre>sds sdsMakeRoomFor(sds s, size_t addlen) {\n    void *sh, *newsh;\n    \/\/ \u83b7\u53d6sds\u53ef\u7528\u7a7a\u95f4\n    size_t avail = sdsavail(s);\n    size_t len, newlen;\n    char type, oldtype = s[-1] &amp; SDS_TYPE_MASK;\n    int hdrlen;\n \n    \/\/ \u5982\u679c\u53ef\u7528\u7a7a\u95f4\u5927\u4e8e\u7b49\u4e8e\u8981\u589e\u52a0\u7684\u7a7a\u95f4\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n    if (avail &gt;= addlen) return s;\n    \/\/ sds\u957f\u5ea6\n    len = sdslen(s);\n    \/\/ sds\u6307\u9488\n    sh = (char*)s-sdsHdrSize(oldtype);\n    \/\/ \u65b0\u5b57\u7b26\u4e32\u957f\u5ea6\n    newlen = (len+addlen);\n    \/\/ \u5982\u679c\u65b0\u957f\u5ea6\u5c0f\u4e8e\u6700\u5927\u9884\u5206\u914d\u957f\u5ea6\uff0c\u5219\u8fdb\u884c\u4e24\u500d\u6269\u5bb9\n    if (newlen \n<p>&nbsp;\u53ef\u4ee5\u770b\u5230<strong>sdsMakeRoomFor<\/strong>\u51fd\u6570\u4e2d<strong>sdshdr5<\/strong>\u7c7b\u578b\u4e0d\u518d\u4f7f\u7528\u76f4\u63a5\u8f6c\u6362\u6210\u4e86<strong>sdshdr8<\/strong>\u7c7b\u578b\uff0c\u5b83\u4eec\u662fSDS\u8bbe\u8ba1\u76845\u79cd\u7c7b\u578b\uff0c\u5206\u522b\u8868\u793a<strong>sdshdr5<\/strong>\u3001<strong>sdshdr8<\/strong>\u3001<strong>sdshdr16<\/strong>\u3001<strong>sdshdr32<\/strong>\u548c<strong>sdshdr64<\/strong>\uff0c\u4e0b\u9762\u5c31\u770b\u4e00\u4e0b\u8fd9\u51e0\u79cd\u7c7b\u578b\u7684\u7ed3\u6784\u6e90\u7801\uff0c\u5982\u4e0b\u56fe\uff0c<\/p>\n<pre>struct __attribute__ ((__packed__)) sdshdr5 {\n    unsigned char flags; \/* 3 lsb of type, and 5 msb of string length *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr8 {\n    uint8_t len; \/* used *\/\n    uint8_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr16 {\n    uint16_t len; \/* used *\/\n    uint16_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr32 {\n    uint32_t len; \/* used *\/\n    uint32_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};\nstruct __attribute__ ((__packed__)) sdshdr64 {\n    uint64_t len; \/* used *\/\n    uint64_t alloc; \/* excluding the header and null terminator *\/\n    unsigned char flags; \/* 3 lsb of type, 5 unused bits *\/\n    char buf[];\n};<\/pre>\n<p><strong>sdshdr5<\/strong>\u5df2\u4e0d\u518d\u4f7f\u7528\uff0c\u6240\u4ee5\u5728\u51fd\u6570\u4e2d\u505a\u4e86\u5904\u7406\uff0c\u628a<strong>sdshdr5<\/strong>\u7c7b\u578b\u8f6c\u6362\u4e3a<strong>sdshdr8<\/strong>\u7c7b\u578b\u3002\u524d\u9762\u4e5f\u63d0\u5230\u8fc7SDS\u662f\u7d27\u51d1\u578b\u5b57\u7b26\u4e32\u6570\u636e\u7ed3\u6784\uff0c\u4ee5<strong>sdshdr8<\/strong>\u4e3a\u4f8b\uff0c\u5b83\u662f\u7528\u7684\u662f<strong>uint8_t<\/strong>\u53738\u4f4d\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u4f1a\u5360\u75281\u5b57\u8282\u7684\u5185\u5b58\u7a7a\u95f4\u3002SDS\u4e4b\u6240\u4ee5\u8bbe\u8ba1\u4e0d\u540c\u7684\u7ed3\u6784\u662f\u4e3a\u4e86\u80fd\u7075\u6d3b\u4fdd\u5b58\u4e0d\u540c\u5927\u5c0f\u7684\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u6709\u6548\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>\u53e6\u5916\uff0c<strong>__attribute__ ((__packed__))<\/strong>\u6807\u5fd7\u53ef\u4ee5\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u4ee5\u4e0a\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u4e0d\u5b9e\u7528\u5b57\u8282\u5bf9\u9f50\u7684\u65b9\u5f0f\uff08\u4e0d\u6ee18\u5b57\u8282\u7684\u6574\u6570\u500d\uff0c\u5219\u4f1a\u81ea\u52a8\u8865\u9f50\uff09\uff0c\u800c\u662f\u91c7\u7528\u7d27\u51d1\u7684\u65b9\u5f0f\u5206\u914d\u5185\u5b58\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e4bSDS\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201136","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201136"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201136\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}