{"id":153947,"date":"2024-11-02T11:41:20","date_gmt":"2024-11-02T03:41:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/153947\/"},"modified":"2024-11-02T11:41:21","modified_gmt":"2024-11-02T03:41:21","slug":"%e7%9a%84key%e4%bd%bf%e7%94%a8redis%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a%e5%89%8d%e7%b6%b4%e7%9a%84%e9%8d%b5%e5%80%bc%ef%bc%88redis%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a%e5%89%8d%e7%b6%b4%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/153947\/","title":{"rendered":"\u7684key\u4f7f\u7528Redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u503c\uff08redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a%e5%89%8d%e7%b6%b4%e7%9a%84%e9%8d%b5%e5%80%bc-heSLRwQfxg\">\u4f7f\u7528Redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u503c<\/h1>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\u4e2d\uff0c\u5982\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u5b58\u5132\u7b49\u3002\u7576\u6211\u5011\u9700\u8981\u67e5\u627e\u4ee5\u7279\u5b9a\u524d\u7db4\u958b\u982d\u7684\u9375\u6642\uff0cRedis\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u503c\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e9%8d%b5%e5%80%bc%e7%b5%90%e6%a7%8b-heSLRwQfxg\">Redis\u7684\u9375\u503c\u7d50\u69cb<\/h2>\n<p>\u5728Redis\u4e2d\uff0c\u6578\u64da\u4ee5\u9375\u503c\u5c0d\u7684\u5f62\u5f0f\u5b58\u5132\u3002\u6bcf\u500b\u9375\u90fd\u662f\u552f\u4e00\u7684\uff0c\u4e26\u4e14\u53ef\u4ee5\u5c0d\u61c9\u5230\u4e0d\u540c\u985e\u578b\u7684\u503c\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u9019\u7a2e\u7d50\u69cb\u4f7f\u5f97Redis\u5728\u67e5\u627e\u548c\u64cd\u4f5c\u6578\u64da\u6642\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<h2 id=\"%e6%9f%a5%e6%89%be%e6%8c%87%e5%ae%9a%e5%89%8d%e7%b6%b4%e7%9a%84%e9%8d%b5-heSLRwQfxg\">\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375<\/h2>\n<p>\u8981\u67e5\u627e\u4ee5\u7279\u5b9a\u524d\u7db4\u958b\u982d\u7684\u9375\uff0cRedis\u63d0\u4f9b\u4e86<code>SCAN<\/code>\u547d\u4ee4\u3002\u9019\u500b\u547d\u4ee4\u53ef\u4ee5\u7528\u4f86\u904d\u6b77\u6578\u64da\u5eab\u4e2d\u7684\u9375\uff0c\u4e26\u4e14\u652f\u6301\u6a21\u5f0f\u5339\u914d\u3002\u4f7f\u7528<code>SCAN<\/code>\u547d\u4ee4\u7684\u597d\u8655\u662f\u5b83\u4e0d\u6703\u963b\u585eRedis\u670d\u52d9\u5668\uff0c\u9069\u5408\u5728\u751f\u7522\u74b0\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n<h3 id=\"scan%e5%91%bd%e4%bb%a4%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95-heSLRwQfxg\">SCAN\u547d\u4ee4\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<p>\u4ee5\u4e0b\u662f<code>SCAN<\/code>\u547d\u4ee4\u7684\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>SCAN cursor [MATCH pattern] [COUNT count]<\/code><\/pre>\n<ul>\n<li><code>cursor<\/code>: \u6e38\u6a19\uff0c\u521d\u59cb\u503c\u70ba0\uff0c\u5f8c\u7e8c\u7684\u503c\u7531Redis\u8fd4\u56de\u3002<\/li>\n<li><code>MATCH pattern<\/code>: \u7528\u65bc\u5339\u914d\u9375\u7684\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26\uff0c\u5982<code>*<\/code>\u3002<\/li>\n<li><code>COUNT count<\/code>: \u6bcf\u6b21\u8fd4\u56de\u7684\u9375\u7684\u6578\u91cf\uff0c\u9019\u662f\u4e00\u500b\u63d0\u793a\u503c\uff0c\u5be6\u969b\u8fd4\u56de\u7684\u6578\u91cf\u53ef\u80fd\u6703\u5c11\u65bc\u9019\u500b\u503c\u3002<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e6%9f%a5%e6%89%be%e4%bb%a5%e7%89%b9%e5%ae%9a%e5%89%8d%e7%b6%b4%e9%96%8b%e9%a0%ad%e7%9a%84%e9%8d%b5-heSLRwQfxg\">\u793a\u4f8b\uff1a\u67e5\u627e\u4ee5\u7279\u5b9a\u524d\u7db4\u958b\u982d\u7684\u9375<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u4e9b\u9375\u4ee5<code>user:<\/code>\u70ba\u524d\u7db4\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u67e5\u627e\u9019\u4e9b\u9375\uff1a<\/p>\n<pre><code>SCAN 0 MATCH user:* COUNT 10<\/code><\/pre>\n<p>\u9019\u500b\u547d\u4ee4\u5c07\u8fd4\u56de\u4ee5<code>user:<\/code>\u958b\u982d\u7684\u6700\u591a10\u500b\u9375\u3002\u6ce8\u610f\uff0c<code>SCAN<\/code>\u547d\u4ee4\u662f\u5206\u9801\u7684\uff0c\u53ef\u80fd\u9700\u8981\u591a\u6b21\u8abf\u7528\u4f86\u7372\u53d6\u6240\u6709\u5339\u914d\u7684\u9375\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8redis%e7%9a%84lua%e8%85%b3%e6%9c%ac%e9%80%b2%e8%a1%8c%e6%9f%a5%e6%89%be-heSLRwQfxg\">\u4f7f\u7528Redis\u7684Lua\u8173\u672c\u9032\u884c\u67e5\u627e<\/h3>\n<p>\u9664\u4e86\u4f7f\u7528<code>SCAN<\/code>\u547d\u4ee4\u5916\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u4f7f\u7528Lua\u8173\u672c\u4f86\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u3002\u9019\u6a23\u53ef\u4ee5\u5728\u670d\u52d9\u5668\u7aef\u57f7\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\u3002<\/p>\n<pre><code>\nlocal keys = {}\nlocal cursor = \"0\"\nrepeat\n    local result = redis.call(\"SCAN\", cursor, \"MATCH\", ARGV[1] .. \"*\", \"COUNT\", 100)\n    cursor = result[1]\n    for i, key in ipairs(result[2]) do\n        table.insert(keys, key)\n    end\nuntil cursor == \"0\"\nreturn keys\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86<code>ARGV[1]<\/code>\u4f86\u63a5\u6536\u524d\u7db4\uff0c\u4e26\u4e14\u6bcf\u6b21\u8abf\u7528<code>SCAN<\/code>\u547d\u4ee4\u4f86\u7372\u53d6\u5339\u914d\u7684\u9375\uff0c\u76f4\u5230\u6e38\u6a19\u8fd4\u56de\u52300\u70ba\u6b62\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-heSLRwQfxg\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u503c\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u64cd\u4f5c\u3002\u901a\u904e<code>SCAN<\/code>\u547d\u4ee4\u548cLua\u8173\u672c\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u7372\u53d6\u6240\u9700\u7684\u9375\u3002\u9019\u4e9b\u6280\u8853\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u7279\u5225\u6709\u7528\uff0c\u80fd\u5920\u6709\u6548\u5730\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u904b\u884c\u60a8\u7684Redis\u5be6\u4f8b\uff0c\u4eab\u53d7\u66f4\u5feb\u7684\u6578\u64da\u8a2a\u554f\u901f\u5ea6\u548c\u66f4\u9ad8\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u67e5\u627e\u6307\u5b9a\u524d\u7db4\u7684\u9375\u503c\uff0c\u5feb\u901f\u7372\u53d6\u76f8\u95dc\u6578\u64da\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\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-153947","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153947","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=153947"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153947\/revisions"}],"predecessor-version":[{"id":153948,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153947\/revisions\/153948"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=153947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=153947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=153947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}