{"id":98204,"date":"2024-10-21T00:39:20","date_gmt":"2024-10-20T16:39:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/98204\/"},"modified":"2024-10-21T00:39:21","modified_gmt":"2024-10-20T16:39:21","slug":"redis-%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a-key-%e7%9a%84%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/98204\/","title":{"rendered":"Redis \u67e5\u627e\u6307\u5b9a Key \u7684\u65b9\u5f0f"},"content":{"rendered":"<h1 id=\"redis-%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a-key-%e7%9a%84%e6%96%b9%e5%bc%8f-rqOOtnfDne\">Redis \u67e5\u627e\u6307\u5b9a Key \u7684\u65b9\u5f0f<\/h1>\n<p>Redis \u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u5b58\u5132\u7b49\u5834\u666f\u3002\u7531\u65bc\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0cRedis \u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u5728\u4f7f\u7528 Redis \u6642\uff0c\u67e5\u627e\u6307\u5b9a\u7684 Key \u662f\u4e00\u500b\u57fa\u672c\u800c\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5e7e\u7a2e\u67e5\u627e\u6307\u5b9a Key \u7684\u65b9\u5f0f\uff0c\u5e6b\u52a9\u8b80\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Redis\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-get-%e5%91%bd%e4%bb%a4-rqOOtnfDne\">1. \u4f7f\u7528 GET \u547d\u4ee4<\/h2>\n<p>\u6700\u57fa\u672c\u7684\u67e5\u627e\u65b9\u5f0f\u662f\u4f7f\u7528 <code>GET<\/code> \u547d\u4ee4\u3002\u9019\u500b\u547d\u4ee4\u7528\u65bc\u7372\u53d6\u6307\u5b9a Key \u7684\u503c\u3002\u5982\u679c Key \u5b58\u5728\uff0c\u5247\u8fd4\u56de\u5176\u5c0d\u61c9\u7684\u503c\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de <code>nil<\/code>\u3002<\/p>\n<pre><code>GET mykey<\/code><\/pre>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u5df2\u7d93\u5728 Redis \u4e2d\u8a2d\u7f6e\u4e86\u4e00\u500b Key \u70ba <code>mykey<\/code> \u7684\u503c\uff1a<\/p>\n<pre><code>SET mykey \"Hello, Redis!\"<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>GET<\/code> \u547d\u4ee4\u4f86\u67e5\u627e\u9019\u500b Key\uff1a<\/p>\n<pre><code>GET mykey<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de <code>\"Hello, Redis!\"<\/code>\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-exists-%e5%91%bd%e4%bb%a4-rqOOtnfDne\">2. \u4f7f\u7528 EXISTS \u547d\u4ee4<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u53ef\u80fd\u53ea\u60f3\u77e5\u9053\u4e00\u500b Key \u662f\u5426\u5b58\u5728\uff0c\u800c\u4e0d\u9700\u8981\u7372\u53d6\u5176\u503c\u3002\u9019\u6642\u53ef\u4ee5\u4f7f\u7528 <code>EXISTS<\/code> \u547d\u4ee4\u3002\u9019\u500b\u547d\u4ee4\u6703\u8fd4\u56de 1\uff08\u5b58\u5728\uff09\u6216 0\uff08\u4e0d\u5b58\u5728\uff09\u3002<\/p>\n<pre><code>EXISTS mykey<\/code><\/pre>\n<p>\u5982\u679c <code>mykey<\/code> \u5b58\u5728\uff0c\u5247\u8fd4\u56de 1\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de 0\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8-keys-%e5%91%bd%e4%bb%a4-rqOOtnfDne\">3. \u4f7f\u7528 KEYS \u547d\u4ee4<\/h2>\n<p>\u5982\u679c\u9700\u8981\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684 Key\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>KEYS<\/code> \u547d\u4ee4\u3002\u9019\u500b\u547d\u4ee4\u652f\u6301\u901a\u914d\u7b26\uff0c\u53ef\u4ee5\u7528\u4f86\u67e5\u627e\u6240\u6709\u7b26\u5408\u689d\u4ef6\u7684 Key\u3002<\/p>\n<pre><code>KEYS my*<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6240\u6709\u4ee5 <code>my<\/code> \u958b\u982d\u7684 Key\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>KEYS<\/code> \u547d\u4ee4\u5728\u5927\u578b\u6578\u64da\u96c6\u4e0a\u6027\u80fd\u8f03\u5dee\uff0c\u56e0\u6b64\u5728\u751f\u7522\u74b0\u5883\u4e2d\u61c9\u8b39\u614e\u4f7f\u7528\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8-scan-%e5%91%bd%e4%bb%a4-rqOOtnfDne\">4. \u4f7f\u7528 SCAN \u547d\u4ee4<\/h2>\n<p>\u70ba\u4e86\u907f\u514d <code>KEYS<\/code> \u547d\u4ee4\u7684\u6027\u80fd\u554f\u984c\uff0cRedis \u63d0\u4f9b\u4e86 <code>SCAN<\/code> \u547d\u4ee4\u3002\u9019\u500b\u547d\u4ee4\u53ef\u4ee5\u9010\u6b65\u904d\u6b77\u6240\u6709\u7684 Key\uff0c\u4e26\u4e14\u4e0d\u6703\u963b\u585e\u670d\u52d9\u5668\u3002<\/p>\n<pre><code>SCAN 0 MATCH my*<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6240\u6709\u4ee5 <code>my<\/code> \u958b\u982d\u7684 Key\uff0c\u4e26\u4e14\u53ef\u4ee5\u901a\u904e\u591a\u6b21\u8abf\u7528\u4f86\u7372\u53d6\u6240\u6709\u7684 Key\u3002<\/p>\n<h2 id=\"5-%e4%bd%bf%e7%94%a8-hget-%e5%91%bd%e4%bb%a4%ef%bc%88%e5%b0%8d%e6%96%bc%e5%93%88%e5%b8%8c%e9%a1%9e%e5%9e%8b%ef%bc%89-rqOOtnfDne\">5. \u4f7f\u7528 HGET \u547d\u4ee4\uff08\u5c0d\u65bc\u54c8\u5e0c\u985e\u578b\uff09<\/h2>\n<p>\u5982\u679c Key \u5c0d\u61c9\u7684\u503c\u662f\u54c8\u5e0c\u985e\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>HGET<\/code> \u547d\u4ee4\u4f86\u67e5\u627e\u7279\u5b9a\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<pre><code>HSET myhash field1 \"value1\"<\/code><\/pre>\n<pre><code>HGET myhash field1<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de <code>\"value1\"<\/code>\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rqOOtnfDne\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Redis \u4e2d\u67e5\u627e\u6307\u5b9a Key \u7684\u65b9\u5f0f\u6709\u591a\u7a2e\uff0c\u5305\u62ec\u4f7f\u7528 <code>GET<\/code>\u3001<code>EXISTS<\/code>\u3001<code>KEYS<\/code>\u3001<code>SCAN<\/code> \u548c <code>HGET<\/code> \u7b49\u547d\u4ee4\u3002\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u547d\u4ee4\uff0c\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u548c\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u53d6\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u7279\u5225\u662f\u5728\u9999\u6e2f\u5730\u5340\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684\u6027\u80fd\u548c\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Redis \u4e2d\u67e5\u627e\u6307\u5b9a Key \u7684\u591a\u79cd\u65b9\u5f0f\uff0c\u63d0\u5347\u6570\u636e\u68c0\u7d22\u6548\u7387\uff0c\u4f18\u5316\u6570\u636e\u5e93\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-98204","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98204","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=98204"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98204\/revisions"}],"predecessor-version":[{"id":98205,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98204\/revisions\/98205"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=98204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=98204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=98204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}