{"id":201330,"date":"2025-05-10T10:35:37","date_gmt":"2025-05-10T02:35:37","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201330\/"},"modified":"2025-05-10T10:35:37","modified_gmt":"2025-05-10T02:35:37","slug":"%e8%a7%a3%e6%9e%90redis%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e4%b9%8b%e7%ae%80%e5%8d%95%e5%8a%a8%e6%80%81%e5%ad%97%e7%ac%a6%e4%b8%b2sds","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201330\/","title":{"rendered":"\u89e3\u6790Redis\u6570\u636e\u7ed3\u6784\u4e4b\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32sds"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u89e3\u6790Redis\u6570\u636e\u7ed3\u6784\u4e4b\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32sds<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u89e3\u6790Redis\u6570\u636e\u7ed3\u6784\u4e4b\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32sds\u300b\uff0c\u804a\u804aRedis\u6570\u636e\u7ed3\u6784\u3001\u52a8\u6001\u5b57\u7b26\u4e32\u3001sds\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>Redis\u662f\u7528ANSI C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5b83\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684key-value\u6570\u636e\u5e93\uff0c\u5b83\u53ef\u4ee5\u4f5c\u7528\u5728\u6570\u636e\u5e93\u3001\u7f13\u5b58\u548c\u6d88\u606f\u4e2d\u95f4\u4ef6\u3002\u5176\u4e2d Redis \u952e\u503c\u5bf9\u4e2d\u7684\u952e\u90fd\u662f string \u7c7b\u578b\uff0c\u800c\u952e\u503c\u5bf9\u4e2d\u7684\u503c\u4e5f\u662f\u6709 string \u7c7b\u578b\uff0c\u5728 Redis \u4e2d string \u7c7b\u578b\u8fd0\u7528\u8fd8\u662f\u5f88\u5e7f\u6cdb\u7684\u3002\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd string \u7684\u6570\u636e\u7ed3\u6784\u2014\u2014 \u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32\uff08Simple Dynamic String\uff09 \u7b80\u79f0sds\u3002<\/p>\n<h2>sds \u5b9e\u73b0<\/h2>\n<p>sds \u7684\u6570\u636e\u7ed3\u6784:<\/p>\n<pre>\nstruct sdshdr {\n\n     \/\/buf \u5df2\u5360\u7528\u7684\u957f\u5ea6\n     int len;\n\n     \/\/ buf \u5269\u4f59\u7684\u53ef\u7528\u7684\u957f\u5ea6\n     int free;\n   \n     \/\/ \u4fdd\u5b58\u5b57\u7b26\u4e32\u6570\u636e\u7684\u5730\u65b9\n     char buf[]; \n}<\/pre>\n<p>\u7ed3\u6784 sdshdr \u4fdd\u5b58\u4e86 len\u3001free \u548c buf \u4e09\u4e2a\u5c5e\u6027\uff0c\u5206\u522b\u8bb0\u5f55\u5b57\u7b26\u7684\u5df2\u4f7f\u7528\u7684\u957f\u5ea6\uff0c\u672a\u4f7f\u7528\u7684\u957f\u5ea6\uff0c\u4ee5\u53ca\u5b9e\u9645\u4fdd\u5b58\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002<br \/> \u4ee5\u4e0b\u662f\u4e00\u4e2a\u65b0\u5efa\u7684\uff0c\u4fdd\u5b58 hello world \u5b57\u7b26\u4e32\u7684 sdshdr \u7ed3\u6784\uff1a<\/p>\n<pre>\nstruct sdshdr {\n    len = 5;\n    free = 0;\n    buf = \"hello\\0\"; \n}<\/pre>\n<ul>\n<li><span style=\"background-color: initial\">free \u5c5e\u6027\u503c\u4e3a0\uff0c\u8868\u793a\u8fd9\u4e2asds\u6ca1\u6709\u5206\u914d\u672a\u4f7f\u7528\u7684\u7a7a\u95f4\u3002<\/span><\/li>\n<li><span style=\"background-color: initial\">len \u5c5e\u6027\u503c\u4e3a5\uff0c\u8868\u793a\u8fd9\u4e2asds\u4fdd\u5b58\u4e86\u4e00\u4e2a\u4e94\u5b57\u8282\u957f\u7684\u5b57\u7b26\u4e32\u3002<\/span><\/li>\n<li><span style=\"background-color: initial\">buf \u5c5e\u6027\u662f\u4e00\u4e2a char \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u524d\u4e94\u4e2a\u5b57\u8282\u5206\u522b\u4fdd\u5b58\u4e86 &#8216;h&#8217;\u3001&#8217;e&#8217;\u3001&#8217;l&#8217;\u3001&#8217;l&#8217;\u3001&#8217;o&#8217; \u4e94\u4e2a\u5b57\u7b26\uff0c\u800c\u6700\u540e\u4e00\u4e2a\u5b57\u8282\u4fdd\u5b58\u4e86\u7a7a\u5b57\u7b26&#8217;\\0&#8217;\u3002<\/span><\/li>\n<\/ul>\n<p>sds \u9075\u5b88 C \u5b57\u7b26\u4e32\u4ee5\u7a7a\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u60ef\u4f8b\uff0c\u4fdd\u5b58\u7684\u7a7a\u5b57\u7b26\u4e32\u4e00\u4e2a\u5b57\u8282\u7a7a\u95f4\u4e0d\u8ba1\u7b97\u5728 sds \u7684 len \u5c5e\u6027\u91cc\u9762\u3002\u6dfb\u52a0\u7a7a\u5b57\u7b26\u4e32\u5230\u5b57\u7b26\u4e32\u672b\u5c3e\u7b49\u64cd\u4f5c\uff0c\u90fd\u662f\u7531 sds \u51fd\u6570\u81ea\u52a8\u5b8c\u6210\u7684\uff0c\u6240\u4ee5\u8fd9\u4e2a\u7a7a\u5b57\u7b26\u5bf9\u4e8e\u4f7f\u7528\u8005\u6765\u8bf4\u5b8c\u5168\u662f\u900f\u660e\u7684\u3002<\/p>\n<p>\u901a\u8fc7 len \u5c5e\u6027\uff0c\u53ef\u4ee5\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6 O(1) \u7684\u957f\u5ea6\u8ba1\u7b97\u3002\u53e6\u5916\u901a\u8fc7\u5bf9 buf \u5206\u914d\u4e00\u4e9b\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u5e76\u4f7f\u7528 free \u8bb0\u5f55\u672a\u4f7f\u7528\u7a7a\u95f4\u7684\u957f\u5ea6\uff0csdshdr \u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u7684\u91cd\u65b0\u5206\u914d\u3002\u8fd9\u662f sds \u76f8\u5bf9 c \u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u4f18\u52bf\u3002<\/p>\n<h2>\u4e3a\u4f55 Redis \u4e0d\u7528 C \u8bed\u8a00\u8868\u793a\u5b57\u7b26\u4e32<\/h2>\n<p>Redis \u662f\u4f7f\u7528 C \u8bed\u8a00\u5f00\u53d1\u7684\uff0c\u800c\u5728\u4f7f\u7528\u6700\u591a\u7684\u5b57\u7b26\u4e32\u4e0a\uff0cRedis \u6ca1\u6709\u4f7f\u7528 C \u8bed\u8a00\u4f20\u7edf\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u800c\u4e14\u4f7f\u7528\u81ea\u5df1\u6784\u5efa\u7684\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32\uff08sds\uff09\u3002<br \/> \u5728 C \u8bed\u8a00\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u4e00\u4e2a \\0 \u7ed3\u5c3e\u7684 char \u6570\u7ec4\u8868\u793a\u3002\u6bd4\u5982 hello world \u5728 C \u8bed\u8a00\u4e2d\u5c31\u53ef\u4ee5\u8868\u793a\u4e3a&#8221;hello world\\0&#8243;\u3002\u6570\u7ec4\u4e00\u822c\u521d\u59cb\u5316\u4ee5\u540e\u957f\u5ea6\u5c31\u5df2\u7ecf\u56fa\u5b9a\u4e86\uff0c\u4e0d\u80fd\u652f\u6301\u5b57\u7b26\u4e32\u8ffd\u52a0append\u548c\u957f\u5ea6\u8ba1\u7b97\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u6bcf\u6b21\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u90fd\u8981\u904d\u5386\u4e00\u904d\u6570\u7ec4\uff0c\u6240\u4ee5\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(N)<\/li>\n<li>\u5bf9\u5b57\u7b26\u4e32\u6bcf\u6b21\u8fdb\u884c\u8ffd\u52a0\u64cd\u4f5c\uff0c\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u4e00\u6b21\u5185\u5b58\u5206\u914d<\/li>\n<\/ul>\n<h2>sds \u4f18\u5316\u8ffd\u52a0\u5b57\u7b26\u64cd\u4f5c<\/h2>\n<p>Redis \u4f5c\u4e3a\u6570\u636e\u5e93\uff0c\u5bf9\u4e8e\u67e5\u8be2\u901f\u5ea6\u8981\u6c42\u4e25\u683c\uff0c\u6570\u636e\u4fee\u6539\u4e5f\u6bd4\u8f83\u9891\u7e41\uff0c\u5982\u679c\u6bcf\u6b21\u4fee\u6539\u5b57\u7b26\u4e32\u90fd\u9700\u8981\u6267\u884c\u4e00\u6b21\u5185\u5b58\u5206\u914d\u7684\u8bdd\uff0c\u90fd\u4f1a\u5360\u7528\u5927\u91cf\u7684\u65f6\u95f4\u3002\u6240\u4ee5 Redis \u9009\u62e9\u4e86 sds \u800c\u4e0d\u662f C \u5b57\u7b26\u4e32\uff0csds \u53ef\u4ee5\u51cf\u5c11\u8ffd\u52a0\u5b57\u7b26\u7684\u5185\u5b58\u5206\u914d\u3002\u901a\u8fc7\u4e3e\u4f8b\u6765\u8bf4\u660e\uff0c\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u65f6\uff0csds \u5185\u90e8\u7684\u53d8\u5316\uff1a<\/p>\n<pre>\nredis&gt; set msg \"hello world\"\nOK\n\nredis&gt; append msg \" again\"\n(integer)18\n\nredis&gt; get msg\n\"hello world again\"<\/pre>\n<p>\u9996\u5148 set \u547d\u4ee4\u521b\u5efa\u5e76\u4fdd\u5b58hello world \u5230\u4e00\u4e2a sdshdr \u4e2d\uff0c\u8fd9\u4e2a sdshdr \u7684\u503c\u5982\u4e0b\uff1a<\/p>\n<pre>\nstruct sdshdr {\n     len = 11;\n     free = 0;\n     buf = \"hello world\\0\";\n}<\/pre>\n<p>\u5f53\u6267\u884c append \u547d\u4ee4\u65f6\uff0c\u76f8\u5bf9\u5e94\u7684 sdshdr \u88ab\u66f4\u65b0\uff0c\u5b57\u7b26\u4e32 &#8221; again&#8221; \u4f1a\u88ab\u8ffd\u52a0\u5230\u539f\u6765\u7684 &#8220;hello world&#8221; \u4e4b\u540e\uff1a<\/p>\n<pre>\nstruct sdshdr {\n     len = 17;\n     free = 17;\n     buf = \"hello world again\\0                \";\n}<\/pre>\n<p>\u5f53\u8c03\u7528 set \u547d\u4ee4\u521b\u5efa sdshdr \u65f6\uff0cRedis \u6ca1\u6709\u7ed9 sdshdr \u5206\u914d\u591a\u4f59\u7684\u7a7a\u95f4\uff0cfree \u5c5e\u6027\u4e3a0\u3002\u800c\u5728\u6267\u884c append \u64cd\u4f5c\u4e4b\u540e\uff0cRedis \u4e3a buf \u5206\u914d\u4e86\u591a\u4e8e\u6240\u9700\u7a7a\u95f4\u4e00\u500d\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5728\u6267\u884c append \u547d\u4ee4\u4e4b\u540e\uff0c\u4fdd\u5b58 &#8220;hello world again&#8221; \u5171\u9700\u898117 + 1 \u4e2a\u5b57\u8282\uff0c\u4f46\u662f\u7a0b\u5e8f\u4e3a sdshdr \u5206\u914d\u4e86 17 + 17 + 1 = 35 \u4e2a\u5b57\u8282\uff0c\u800c\u540e\u7eed\u5982\u679c\u5728\u5bf9 sdshdr \u8fdb\u884c\u8ffd\u52a0\u64cd\u4f5c\uff0c\u53ea\u8981\u8ffd\u52a0\u7684\u957f\u5ea6\u4e0d\u8d85\u8fc7 free \u5c5e\u6027\u503c\uff0c\u90a3\u4e48\u5c31\u4e0d\u9700\u8981\u5bf9 buf \u8fdb\u884c\u5185\u5b58\u91cd\u5206\u914d\u3002<\/p>\n<p>\u6bd4\u5982\u6267\u884c\u4ee5\u540e\u547d\u4ee4\u5e76\u4e0d\u4f1a\u5f15\u8d77 buf \u7684\u5185\u5b58\u91cd\u5206\u914d\uff0c\u56e0\u4e3a\u65b0\u8ffd\u52a0\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e17\uff1a<\/p>\n<pre>\nredis&gt; append msg  \" again\"\n(integer) 23<\/pre>\n<p>\u5bf9\u5e94\u7684 sdshdr \u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>\nstruct sdshdr {\n     len = 23;\n     free = 11;\n     buf = \"hello world again again\\0               \";\n}<\/pre>\n<p>redis \u5185\u5b58\u5206\u914d\u53ef\u4ee5\u67e5\u770b sds.s\/sdsMakeRoomFor\uff0csdsMakeRoomFor \u51fd\u6570\u63cf\u8ff0\u4e86\u5185\u5b58\u5206\u914d\u7684\u7b56\u7565\uff0c\u4e0b\u9762\u7684\u8be5\u51fd\u6570\u7684\u4f2a\u4ee3\u7801\uff1a<\/p>\n<pre>\n\/\/ sdshdr:\u8ffd\u52a0\u524d\u7684\u5b57\u7b26\n\/\/ addlen\uff1a\u8ffd\u52a0\u5b57\u7b26\u4e32\nsds sdsMakeRoomFor(sdshdr, addlen) {\n   \n    \/\/ \u591a\u4f59\u7a7a\u95f4\u5927\u4e8e\u8ffd\u52a0\u7a7a\u95f4\uff0c\u65e0\u5e8f\u518d\u5206\u914d\u5185\u5b58\uff0c\u76f4\u63a5\u8fd4\u56de\n    if (free &gt;= addlen) return s;\n    \/\/ \u8ba1\u7b97\u65b0\u5b57\u7b26\u7684\u957f\u5ea6\n    newlen = (len+addlen); \n   \/\/ \u5982\u679c\u65b0\u5b57\u7b26\u7684\u957f\u5ea6\u5c0f\u4e8e SDS_MAX_PREALLOC\uff0c\u5c31\u5206\u914d\u4e24\u500d\u65b0\u5b57\u7b26\u7a7a\u95f4\n  \/\/  \u5982\u679c\u65b0\u5b57\u7b26\u7684\u957f\u5ea6\u5927\u4e8e SDS_MAX_PREALLOC\uff0c\u5c31\u5206\u914d\u65b0\u5b57\u7b26\u7a7a\u95f4 + SDS_MAX_PREALLOC \u7a7a\u95f4\n    if (newlen \n\n<p>\u800c\u5bf9\u4e8e\u5b57\u7b26\u7684\u7f29\u77ed\u64cd\u4f5c\uff0cRedis \u4fdd\u5b58\u7f29\u77ed\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u6b64\u65f6\u5e76\u4e0d\u4f1a\u8fdb\u884c\u5185\u5b58\u91cd\u5206\u914d\uff0c\u800c\u662f\u4f7f\u7528 free \u5c5e\u6027\u8bb0\u5f55\u7f29\u77ed\u7684\u5b57\u7b26\u957f\u5ea6\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Redis \u7684 string \u7c7b\u578b\u4e3a\u4f55\u4f7f\u7528sds\u800c\u4e0d\u662f C \u5b57\u7b26\u4e32\uff0c\u56e0\u4e3asds\u6709\u4e24\u70b9\u4f18\u52bf\uff1a<\/p>\n<\/pre>\n<ul>\n<li>\u8ba1\u7b97\u5b57\u7b26\u957f\u5ea6\uff0cC \u5b57\u7b26\u4e32\u590d\u6742\u5ea6O\uff08n\uff09\uff0c\u800c sds \u590d\u6742\u5ea6\u4e3a O\uff081\uff09<\/li>\n<li>\u5b57\u7b26\u8ffd\u52a0\u64cd\u4f5c\uff0cC \u5b57\u7b26\u4e32\u6bcf\u6b21\u90fd\u9700\u8981\u5bf9\u5185\u5b58\u8fdb\u884c\u91cd\u5206\u914d\uff0c\u800c sds \u6bcf\u6b21\u4f1a\u8fdb\u884c\u52a8\u6001\u6269\u5bb9\uff0c\u5f53\u6dfb\u52a0\u5b57\u7b26\u5c0f\u4e8e\u7a7a\u95f2\u5b57\u7b26\u65f6\uff0c\u4e0d\u4f1a\u5bf9\u5185\u5bb9\u8fdb\u884c\u5206\u914d\uff0c\u51cf\u5c11\u7cfb\u7edf\u7b49\u5f85\u65f6\u95f4<\/li>\n<\/ul>\n<h2>\u53c2\u8003<\/h2><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6790Redis\u6570\u636e\u7ed3\u6784\u4e4b\u7b80\u5355\u52a8\u6001&#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-201330","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201330","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=201330"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201330\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}