{"id":201209,"date":"2025-05-10T09:04:24","date_gmt":"2025-05-10T01:04:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201209\/"},"modified":"2025-05-10T09:04:24","modified_gmt":"2025-05-10T01:04:24","slug":"%e6%b5%85%e8%b0%88redis%e5%ad%98%e5%82%a8%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e5%8f%8a%e5%ad%98%e5%8f%96%e5%80%bc%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201209\/","title":{"rendered":"\u6d45\u8c08Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u53ca\u5b58\u53d6\u503c\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u6d45\u8c08Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u53ca\u5b58\u53d6\u503c\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230golang\u5b66\u4e60\u7f51\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6570\u636e\u5e93\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u6d45\u8c08Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u53ca\u5b58\u53d6\u503c\u65b9\u6cd5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u6570\u636e\u7c7b\u578b\u3001Redis\u5b58\u50a8\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>Redis\u652f\u6301\u4e94\u79cd\u6570\u636e\u7c7b\u578b\uff1astring\uff08\u5b57\u7b26\u4e32\uff09\uff0chash\uff08\u54c8\u5e0c\uff09\uff0clist\uff08\u5217\u8868\uff09\uff0cset\uff08\u96c6\u5408\uff09\u53cazset(sorted set\uff1a\u6709\u5e8f\u96c6\u5408)<\/p>\n<h3>String\u5b58\u53d6\u503c\uff1a<\/h3>\n<p>\u662f redis \u6700\u57fa\u672c\u7684\u7c7b\u578b&nbsp;\u4e00\u4e2a key \u5bf9\u5e94\u4e00\u4e2a value\u3002value\u5176\u5b9e\u4e0d\u4ec5\u662fString\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u5b57\u3002string \u7c7b\u578b\u662f\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\u3002\u610f\u601d\u662f redis \u7684 string \u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u6570\u636e\u3002\u6bd4\u5982jpg\u56fe\u7247\u6216\u8005\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u3002string \u7c7b\u578b\u662f Redis \u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\uff0cstring \u7c7b\u578b\u7684\u503c\u6700\u5927\u80fd\u5b58\u50a8 512MB\u3002\u3000\u4f7f\u7528\u573a\u666f\uff1a\u5e38\u89c4key-value\u7f13\u5b58\u5e94\u7528\u3002\u5e38\u89c4\u8ba1\u6570: \u5fae\u535a\u6570, \u7c89\u4e1d\u6570\u3002<\/p>\n<p>\u5b58\u503c\uff1aSET key&nbsp; value&nbsp; \u53d6\u503c\uff1aGET key<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247001963afde03e9dcb.png\" class=\"aligncenter\"><\/p>\n<h3>hash\u5b58\u53d6\u503c\uff1a<\/h3>\n<p>\u662f\u4e00\u4e2a\u952e\u503c(key =&gt; value)\u5bf9\u96c6\u5408\u3002Redis hash \u662f\u4e00\u4e2a string \u7c7b\u578b\u7684 field \u548c value \u7684\u6620\u5c04\u8868\uff0chash \u7279\u522b\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u5bf9\u8c61<\/p>\n<p>\u5b58\u503c&nbsp;HMSET key field value [field value &#8230;]&nbsp; &nbsp;\u53d6\u503c&nbsp;&nbsp;HGET key field&nbsp; \u6216\u8005&nbsp; SCAN cursor [MATCH pattern] [COUNT count]&nbsp;<\/p>\n<p>\u6216\u8005&nbsp;HGETALL key&nbsp; \u83b7\u53d6\u5168\u90e8<\/p>\n<p style=\"text-align: center\">&nbsp;<img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002063afde042cbf4.png\" class=\"aligncenter\"><\/p>\n<h3>list\u5b58\u53d6\u503c\uff1a&nbsp;<\/h3>\n<p>\u5217\u8868\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u6309\u7167\u63d2\u5165\u987a\u5e8f\u6392\u5e8f\u3002\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\u5230\u5217\u8868\u7684\u5934\u90e8\uff08\u5de6\u8fb9\uff09\u6216\u8005\u5c3e\u90e8\uff08\u53f3\u8fb9\uff09\u5e38\u7528\u547d\u4ee4\uff1alpush\uff08\u6dfb\u52a0\u5de6\u8fb9\u5143\u7d20\uff09,rpush,lpop\uff08\u79fb\u9664\u5de6\u8fb9\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09,rpop,lrange\uff08\u83b7\u53d6\u5217\u8868\u7247\u6bb5\uff0cLRANGE key start stop\uff09\u7b49<\/p>\n<p>\u5b58\u503c\uff1aLPUSH key value [value &#8230;]&nbsp; \u53d6\u503c\uff1aLRANGE key start stop&nbsp; &nbsp;<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002063afde0479789.png\" class=\"aligncenter\"><\/p>\n<h3>set\u5b58\u53d6\u503c\uff1a<\/h3>\n<p>\u662fstring\u7c7b\u578b\u7684\u65e0\u5e8f\u96c6\u5408\u3002\u96c6\u5408\u662f\u901a\u8fc7hashtable\u5b9e\u73b0\u7684\uff0c\u6982\u5ff5\u548c\u6570\u5b66\u4e2d\u4e2a\u7684\u96c6\u5408\u57fa\u672c\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4ea4\u96c6\uff0c\u5e76\u96c6\uff0c\u5dee\u96c6\u7b49\u7b49\uff0cset\u4e2d\u7684\u5143\u7d20\u662f\u6ca1\u6709\u987a\u5e8f\u7684\u3002set\u5bf9\u5916\u63d0\u4f9b\u7684\u529f\u80fd\u4e0elist\u7c7b\u4f3c\u662f\u4e00\u4e2a\u5217\u8868\u7684\u529f\u80fd\uff0c\u7279\u6b8a\u4e4b\u5904\u5728\u4e8eset\u662f\u53ef\u4ee5\u81ea\u52a8\u6392\u91cd\u7684\uff0c\u5f53\u4f60\u9700\u8981\u5b58\u50a8\u4e00\u4e2a\u5217\u8868\u6570\u636e\uff0c\u53c8\u4e0d\u5e0c\u671b\u51fa\u73b0\u91cd\u590d\u6570\u636e\u65f6\uff0cset\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u5e76\u4e14set\u63d0\u4f9b\u4e86\u5224\u65ad\u67d0\u4e2a\u6210\u5458\u662f\u5426\u5728\uff0c\u4e00\u4e2aset\u96c6\u5408\u5185\u7684\u91cd\u8981\u63a5\u53e3\uff0c\u8fd9\u4e2a\u4e5f\u662flist\u6240\u4e0d\u80fd\u63d0\u4f9b\u7684<\/p>\n<p>\u5b58\u503c\uff1asadd key member [member &#8230;]&nbsp; \u53d6\u503c\uff1a&nbsp;SMEMBERS key&nbsp; \u6216\u8005&nbsp;SSCAN key cursor [MATCH pattern] [COUNT count]&nbsp;<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002063afde04bd5b4.png\" class=\"aligncenter\"><\/p>\n<h3>zset\u5b58\u53d6\u503c\uff1a<\/h3>\n<p>Redis zset \u548c set \u4e00\u6837\u4e5f\u662fstring\u7c7b\u578b\u5143\u7d20\u7684\u96c6\u5408,\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u6210\u5458\u3002\u4e0d\u540c\u7684\u662f\u6bcf\u4e2a\u5143\u7d20\u90fd\u4f1a\u5173\u8054\u4e00\u4e2adouble\u7c7b\u578b\u7684\u5206\u6570\u3002redis\u6b63\u662f\u901a\u8fc7\u5206\u6570\u6765\u4e3a\u96c6\u5408\u4e2d\u7684\u6210\u5458\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u7684\u6392\u5e8f\u3002\u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u6709\u5e8f\u7684\u5e76\u4e14\u4e0d\u91cd\u590d\u7684\u96c6\u5408\u5217\u8868\uff0c\u90a3\u4e48\u53ef\u4ee5\u9009\u62e9sorted set\u6570\u636e\u7ed3\u6784\uff0czset\u7684\u6210\u5458\u662f\u552f\u4e00\u7684,\u4f46\u5206\u6570(score)\u5374\u53ef\u4ee5\u91cd\u590d\u3002<\/p>\n<p>\u5b58\u503c\uff1azadd key [NX|XX] [CH] [INCR] score member [score member &#8230;]&nbsp;&nbsp;&nbsp; \u53d6\u503c\uff1a&nbsp;&nbsp;ZRANGE key start stop [WITHSCORES]&nbsp;<\/p>\n<p>\u6216\u8005&nbsp;ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]&nbsp;<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002163afde05530aa.png\" class=\"aligncenter\"><\/p>\n<p>\u5982\u679c\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u5b58\u5728\u5219\u66f4\u65b0\u5bf9\u5e94score&nbsp; java\u4ece\u539f\u6765\u7b2c\u4e00\u4e2a\u53d8\u4e3a\u7b2c\u4e94\u4e2a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002163afde05b2cc7.png\" class=\"aligncenter\"><\/p>\n<p>\u5982\u679cscore\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u63d2\u5165\u65b0\u7684\u5143\u7d20\u4e3a\u5bf9\u5e94score&nbsp; redis\u63d2\u5165\u539f\u6709\u7684\u6709\u5e8f\u6392\u5e8f\u4e2d<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002163afde05f0fad.png\" class=\"aligncenter\"><\/p>\n<p>\u7ed3\u5408\u8f85\u52a9\u8f6f\u4ef6\u53ef\u4ee5\u770b\u5f97\u66f4\u52a0\u6e05\u6670<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" style=\"max-width:100%\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167247002263afde068b32b.png\" class=\"aligncenter\">\u3001<\/p>\n<p style=\"text-align: left\">\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8e\u6d45\u8c08Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u53ca\u5b58\u53d6\u503c\u65b9\u6cd5\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86,\u66f4\u591a\u76f8\u5173Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u5185\u5bb9\u8bf7\u641c\u7d22golang\u5b66\u4e60\u7f51\u4ee5\u524d\u7684\u6587\u7ae0\u6216\u7ee7\u7eed\u6d4f\u89c8\u4e0b\u9762\u7684\u76f8\u5173\u6587\u7ae0\u5e0c\u671b\u5927\u5bb6\u4ee5\u540e\u591a\u591a\u652f\u6301golang\u5b66\u4e60\u7f51\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d45\u8c08Redis\u5b58\u50a8\u6570\u636e\u7c7b\u578b\u53ca\u5b58\u53d6&#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-201209","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201209","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=201209"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201209\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}