{"id":202346,"date":"2025-05-10T10:56:49","date_gmt":"2025-05-10T02:56:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202346\/"},"modified":"2025-05-10T10:56:49","modified_gmt":"2025-05-10T02:56:49","slug":"redis%e4%b8%ad%e9%94%ae%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202346\/","title":{"rendered":"Redis\u4e2d\u952e\u548c\u5b57\u7b26\u4e32\u5e38\u7528\u547d\u4ee4\u6709\u54ea\u4e9b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e2d\u952e\u548c\u5b57\u7b26\u4e32\u5e38\u7528\u547d\u4ee4\u6709\u54ea\u4e9b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u4e2d\u952e\u548c\u5b57\u7b26\u4e32\u5e38\u7528\u547d\u4ee4\u6709\u54ea\u4e9b\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<ul>\n<ul><\/ul>\n<\/ul>\n<h3>Redis \u76f8\u5173\u77e5\u8bc6<\/h3>\n<p>Redis\u7684\u9ed8\u8ba4\u7aef\u53e3\u53f7\u4e3a6379<\/p>\n<p>\u9ed8\u8ba416\u4e2a\u6570\u636e\u5e93\uff0c\u7c7b\u4f3c\u6570\u7ec4\u4e0b\u6807\u4ece0\u5f00\u59cb,\u521d\u59cb\u9ed8\u8ba4\u4f7f\u75280\u53f7\u5e93\u3002<br \/>\u4f7f\u7528\u547d\u4ee4<code>select &lt;dbid&gt;<\/code>\u6765\u5207\u6362\u6570\u636e\u5e93\u3002 \u5982: select 8\u3002<br \/>\u7edf\u4e00\u5bc6\u7801\u7ba1\u7406,\u6240\u6709\u5e93\u540c\u6837\u5bc6\u7801\u3002<\/p>\n<p><code>dbsize<\/code>\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684key\u7684\u6570\u91cf\u3002<br \/><code>flushdb<\/code>\u6e05\u7a7a\u5f53\u524d\u5e93\u3002<br \/><code>flushall<\/code>\u901a\u6740\u5168\u90e8\u5e93\u3002<\/p>\n<p>Redis\u662f\u5355\u7ebf\u7a0b+\u591a\u8defIO\u590d\u7528\u6280\u672f\u3002<br \/>\u591a\u8def\u590d\u7528\u662f\u6307\u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u6765\u68c0\u67e5\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26( Socket )\u7684\u5c31\u7eea\u72b6\u6001,\u6bd4\u5982\u8c03\u7528select\u548cpoll\u51fd\u6570,\u4f20\u5165\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u7eea,\u5219\u8fd4\u56de,\u5426\u5219\u963b\u585e\u76f4\u5230\u8d85\u65f6\u3002\u5f97\u5230\u5c31\u7eea\u72b6\u6001\u540e\u8fdb\u884c\u771f\u6b63\u7684\u64cd\u4f5c\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u6267\u884c,\u4e5f\u53ef\u4ee5\u542f\u52a8\u7ebf\u7a0b\u6267\u884c(\u6bd4\u5982\u4f7f\u7528\u7ebf\u7a0b\u6c60)\u3002<\/p>\n<p><strong>\u4e32\u884c<\/strong> VS <strong>\u591a\u7ebf\u7a0b+\u9501<\/strong> ( memcached) VS <strong>\u5355\u7ebf\u7a0b+ \u591a\u8defIO\u590d\u7528<\/strong>(Redis)<br \/>Redis\u4e0eMemcache\u4e09\u70b9\u4e0d\u540c\uff1a<\/p>\n<ul>\n<li>\n<p>\u652f\u6301\u591a\u6570\u636e\u7c7b\u578b<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u6301\u4e45\u5316<\/p>\n<\/li>\n<li>\n<p>\u5355\u7ebf\u7a0b+\u591a\u8defIO\u590d\u7528<\/p>\n<\/li>\n<\/ul>\n<h3>Redis\u4e2d\u7684\u6570\u636e\u7c7b\u578b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b2213275.png\" class=\"aligncenter\"><\/p>\n<h4>redis \u952e\uff08key\uff09<\/h4>\n<p><code>keys *<\/code>\uff1a\u67e5\u770b\u5f53\u524d\u5e93\u6240\u6709key (\u5339\u914d : keys *1)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b2219156.png\" class=\"aligncenter\"><\/p>\n<p><code>exists key<\/code>\uff1a\u5224\u65ad\u67d0\u4e2akey\u662f\u5426\u5b58\u5728\u3002<br \/><code>type key<\/code>\uff1a\u67e5\u770b\u4f60\u7684key\u662f\u4ec0\u4e48\u7c7b\u578b\u3002<br \/><code>del key<\/code>\uff1a\u5220\u9664\u6307\u5b9a\u7684key\u6570\u636e\uff0c<\/p>\n<p><code>unlink key<\/code>\uff1a\u6839\u636evalue\u9009\u62e9\u975e\u963b\u585e\u5220\u9664\u3002\u4ec5\u5c06keys\u4ecekeyspace\u5143\u6570\u636e\u4e2d\u5220\u9664,\u771f\u6b63\u7684\u5220\u9664\u4f1a\u5728\u540e\u7eed\u5f02\u6b65\u64cd\u4f5c\u3002\u3002<br \/><code>expire key 10<\/code>\uff1a10 \u79d2\u949f\uff0c\u4e3a\u7ed9\u5b9a\u7684key\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c<br \/><code>ttl key<\/code>\uff1a\u67e5\u770b\u8fd8\u6709\u591a\u5c11\u79d2\u8fc7\u671f, -1\u8868\u793a\u6c38\u4e0d\u8fc7\u671f, -2\u8868\u793a\u5df2\u8fc7\u671f\u3002<\/p>\n<p><code>select<\/code>\u547d\u4ee4\u5207\u6362\u6570\u636e\u5e93\uff0c<br \/><code>dbsize<\/code>\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684key\u7684\u6570\u91cf\u3002<br \/><code>flushdb<\/code>\u6e05\u7a7a\u5f53\u524d\u5e93\u3002<br \/><code>flushall<\/code>\u901a\u6740\u5168\u90e8\u5e93<\/p>\n<h4>Redis\u5b57\u7b26\u4e32(String)<\/h4>\n<p>String\u662fRedis \u6700\u57fa\u672c\u7684\u7c7b\u578b\uff0c\u4e00\u4e2akey\u5bf9\u5e94\u4e00\u4e2avalue\u3002<br \/>String\u7c7b\u578b\u662f\u4e8c\u8fdb\u5236\u5b89\u5168\u7684\u3002 \u610f\u5473\u7740Redis\u7684string\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u6570\u636e\u3002\u6bd4\u5982jpg\u56fe\u7247<br \/>\u6216\u8005\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u3002<br \/>String\u7c7b\u578b\u662fRedis\u6700\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b, \u4e00\u4e2aRedis\u4e2d\u5b57\u7b26\u4e32value\u6700\u591a\u53ef\u4ee5\u662f512M\u3002<\/p>\n<h3>\u5e38\u7528\u547d\u4ee4<\/h3>\n<p>set\u3001get\u7b49<\/p>\n<p><code>set &lt;key&gt; &lt;value&gt;<\/code>\uff1a\u6dfb\u52a0\u952e\u503c\u5bf9\u3002<br \/>\u5f53\u8bbe\u7f6e\u8fc7\u503c\u7684key\u518d\u88ab\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u503c\u65f6\uff0c\u65b0\u7684\u503c\u4f1a\u8986\u76d6\u65e7\u7684\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b221cc37.png\" class=\"aligncenter\"><\/p>\n<blockquote>\n<p>*NX:\u5f53\u6570\u636e\u5e93\u4e2dkey\u4e0d\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u5c06key-value\u6dfb\u52a0\u6570\u636e\u5e93\u3002<br \/>*XX:\u5f53\u6570\u636e\u5e93\u4e2dkey\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u5c06key-value\u6dfb\u52a0\u6570\u636e\u5e93\uff0c\u4e0eNX\u53c2\u6570\u4e92\u65a5\u3002<br \/>*EX: key\u7684\u8d85\u65f6\u79d2\u6570\u3002<br \/>*PX: key \u7684\u8d85\u65f6\u6beb\u79d2\u6570\uff0c\u4e0eEX\u4e92\u65a5\u3002<\/p>\n<\/blockquote>\n<p><code>get &lt;key&gt;<\/code> \u67e5\u8be2\u5bf9\u5e94\u952e\u503c.<br \/><code>append &lt;key&gt; &lt;value&gt;<\/code>\u5c06\u7ed9\u5b9a\u7684&lt; value &gt;\u8ffd\u52a0\u5230\u539f\u503c\u7684\u672b\u5c3e\uff0c<br \/><code>strlen &lt;key&gt;<\/code> \u83b7\u5f97\u503c\u7684\u957f\u5ea6\u3002<br \/><code>setnx &lt;key&gt; &lt;value&gt;<\/code>\u53ea\u6709\u5728key\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e key\u7684\u503c.<\/p>\n<p>\u589e\u51cf\u5efa\u7684\u503c<\/p>\n<blockquote>\n<p>incr &lt;key&gt;<\/p>\n<\/blockquote>\n<p>\u5c06key\u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u589e1\u3002<br \/>\u53ea\u80fd\u5bf9\u6570\u5b57\u503c\u64cd\u4f5c,\u5982\u679c\u4e3a\u7a7a,\u65b0\u589e\u503c\u4e3a10<\/p>\n<blockquote>\n<p>decr &lt;key&gt;<\/p>\n<\/blockquote>\n<p>\u5c06key\u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u51cf1\u3002<br \/>\u53ea\u80fd\u5bf9\u6570\u5b57\u503c\u64cd\u4f5c\u3002\u5982\u679c\u4e3a\u7a7a,\u65b0\u589e\u503c\u4e3a-1\u3002<\/p>\n<p><code>inrjy\/ decrby &lt;key&gt;<\/code>&lt;\u6b65\u957f&gt; \u5c06key\u4e2d\u50a8\u5b58\u7684\u6570\u5b57\u503c\u589e\u51cf\u3002\u81ea\u5b9a\u4e49\u6b65\u957f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<br \/>incr\u548cdecr\u662f\u539f\u5b50\u64cd\u4f5c<br \/>\u800cjava\u4e2d\u7684i++\u5219\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c<\/p>\n<p>mset\u3001mget\u548cmsetnx<\/p>\n<blockquote>\n<p>mset &lt;key1&gt; &lt;value1&gt; &lt;key2&gt; &lt;value2&gt; &#8230;.<\/p>\n<\/blockquote>\n<p>\u540c\u65f6\u8bbe\u7f6e\u4e00\u4e2a\u6216\u591a\u4e2akey-value\u5bf9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b222073a.png\" class=\"aligncenter\"><\/p>\n<blockquote>\n<p>mget &lt;key1&gt; &lt;key2&gt; &lt;key3&gt;<\/p>\n<\/blockquote>\n<p>\u540c\u65f6\u83b7\u53d6\u4e00\u4e2a\u6216\u591a\u4e2avalue\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b22237f8.png\" class=\"aligncenter\"><\/p>\n<blockquote>\n<p>msetnx &lt;key1&gt; &lt;value1&gt; &lt;key2&gt; &lt;value2&gt;<\/p>\n<\/blockquote>\n<p>\u5b83\u662f\u539f\u5b50\u6027\u7684<br \/>\u540c\u65f6\u8bbe\u7f6e\u4e00\u4e2a\u6216\u591a\u4e2a key-value\u5bf9,\u5f53\u4e14\u4ec5\u5f53\u6240\u6709\u7ed9\u5b9akey\u90fd\u4e0d\u5b58\u5728\u65f6\uff0c\u624d\u6210\u529f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b2227065.png\" class=\"aligncenter\"><\/p>\n<p>\u6709\u4e00\u4e2a\u662f\u4e4b\u524d\u5b58\u5728\u7684\u5219\u4e0d\u6210\u529f<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b222a532.png\" class=\"aligncenter\"><\/p>\n<p>getrange\u3001setrange<\/p>\n<blockquote>\n<p>getrange &lt;key&gt; &lt;\u8d77\u59cb\u4f4d\u7f6e&gt;&lt;\u7ed3\u675f\u4f4d\u7f6e&gt;<\/p>\n<\/blockquote>\n<p>\u83b7\u5f97\u503c\u7684\u8303\u56f4,\u7c7b\u4f3cjava\u4e2d\u7684substring ,\u524d\u5305\uff0c\u540e\u5305<\/p>\n<blockquote>\n<p>setrange &lt;key&gt; &lt;\u8d77\u59cb\u4f4d\u7f6e&gt; &lt;value&gt;<\/p>\n<\/blockquote>\n<p>\u7528\u8986\u5199\u6240\u50a8\u5b58\u7684\u5b57\u7b26\u4e32\u503c,\u4ece&lt;\u8d77\u59cb\u4f4d\u7f6e:\u5f00\u59cb(\u7d22\u5f15\u4ece0\u5f00\u59cb)\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b222bb95.png\" class=\"aligncenter\"><\/p>\n<p>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/p>\n<blockquote>\n<p>setex &lt;key&gt; &lt;\u8fc7\u671f\u65f6\u95f4&gt;&lt;value&gt;<\/p>\n<\/blockquote>\n<p>\u8bbe\u7f6e\u952e\u503c\u7684\u540c\u65f6\uff0c\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\u79d2\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b2231305.png\" class=\"aligncenter\"><\/p>\n<p>\u66ff\u6362\u65e7\u503c<\/p>\n<blockquote>\n<p>getset &lt;key&gt; &lt;value&gt;<\/p>\n<\/blockquote>\n<p>\u4ee5\u65b0\u6362\u65e7,\u8bbe\u7f6e\u4e86\u65b0\u503c\u540c\u65f6\u83b7\u5f97\u65e7\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b22327f5.png\" class=\"aligncenter\"><\/p>\n<h3>String\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<p>String\u7684\u6570\u636e\u7ed3\u6784\u4e3a\u7b80\u5355\u52a8\u6001\u5b57\u7b26\u4e32(Simple Dynamic String\uff0c\u7e2e\u5199SDS)\u3002\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u5b57\u7b26\u4e32\uff0c\u5185\u90e8\u7ed3\u6784\u5b9e\u73b0\u4e0a\u7c7b\u4f3c\u4e8eJava\u7684ArrayList \uff0c\u91c7\u7528\u9884\u5206\u914d\u5197\u4f59\u7a7a\u95f4\u7684\u65b9\u5f0f\u6765\u51cf\u5c11\u5185\u5b58\u7684\u9891\u7e41\u5206\u914d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240415\/1713183522661d1b2233e31.png\" class=\"aligncenter\"><\/p>\n<p>\u5982\u56fe\u4e2d\u6240\u793a\uff0c\u5185\u90e8\u4e3a\u5f53\u524d\u5b57\u7b26\u4e32\u5b9e\u9645\u5206\u914d\u7684\u7a7a\u95f4capacity\u4e00\u822c\u8981\u9ad8\u4e8e\u5b9e\u9645\u5b57\u7b26\u4e32\u957f\u5ea6len\u3002\u5f53\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e1M\u65f6,\u6269\u5bb9\u90fd\u662f\u52a0\u500d\u73b0\u6709\u7684\u7a7a\u95f4,\u5982\u679c\u8d85\u8fc71M,\u6269\u5bb9\u65f6\u4e00\u6b21\u53ea\u4f1a\u591a\u62691M\u7684\u7a7a\u95f4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5b57\u7b26\u4e32\u6700\u5927\u957f\u5ea6\u4e3a512M\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e2d\u952e\u548c\u5b57\u7b26\u4e32\u5e38\u7528\u547d\u4ee4\u6709&#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-202346","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202346","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=202346"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202346\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}