{"id":10008176,"date":"2023-08-28T15:00:40","date_gmt":"2023-08-28T07:00:40","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008176"},"modified":"2023-08-28T15:00:40","modified_gmt":"2023-08-28T07:00:40","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86-redis-%e4%b8%ad%e7%9a%84%e6%b5%b7%e9%87%8f%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008176","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u5982\u4f55\u904d\u5386 Redis \u4e2d\u7684\u6d77\u91cf\u6570\u636e\uff1f"},"content":{"rendered":"<p>\u6765\u6e90\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=BidShWfY3FalnNGlrZF1%2Fg%3D%3D.VEdVCp9ODTEIhfA8t%2BW6JuclDPEVDCA6QP3LhsvRU3wpiyjmmA%2BQaFh5utzF5%2Fb4ZHcWO%2B5NpB6FSoqVykaFtA%3D%3D\" rel=\"noopener noreferrer\">https:\/\/www.toutiao.com\/article\/6697540366528152077\/<\/a><\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u77e5\u9053\u7ebf\u4e0a\u7684<strong>redis\u7684\u4f7f\u7528\u60c5\u51b5<\/strong>\uff0c\u5c24\u5176\u9700\u8981\u77e5\u9053\u4e00\u4e9b<strong>\u524d\u7f00\u7684key\u503c<\/strong>\uff0c\u8ba9\u6211\u4eec\u600e\u4e48\u53bb\u67e5\u770b\u5462\uff1f\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e2a\u5c0f\u77e5\u8bc6\u70b9\uff01<\/p>\n<h2>\u4e8b\u6545\u4ea7\u751f<\/h2>\n<p>\u56e0\u4e3a\u6211\u4eec\u7684\u7528\u6237<strong>token\u7f13\u5b58\u662f\u91c7\u7528\u4e86\u3010user_token:userid\u3011\u683c\u5f0f\u7684key<\/strong>\uff0c\u4fdd\u5b58\u7528\u6237\u7684token\u7684\u503c\u3002\u6211\u4eec\u8fd0\u7ef4\u4e3a\u4e86\u5e2e\u52a9\u5f00\u53d1\u5c0f\u4f19\u4f34\u4eec\u67e5\u4e00\u4e0b\u7ebf\u4e0a\u73b0\u5728\u6709\u591a\u5c11\u767b\u5f55\u7528\u6237\u3002<\/p>\n<p>\u76f4\u63a5\u7528\u4e86<strong>keys user_token*\u65b9<\/strong>\u5f0f\u8fdb\u884c\u67e5\u8be2\uff0c\u4e8b\u6545\u5c31\u6b64\u53d1\u751f\u4e86\u3002<strong>\u5bfc\u81f4redis\u4e0d\u53ef\u7528\uff0c\u5047\u6b7b<\/strong>\u3002<\/p>\n<h2>\u5206\u6790\u539f\u56e0<\/h2>\n<p>\u6211\u4eec\u7ebf\u4e0a\u7684\u767b\u5f55\u7528\u6237\u6709\u51e0\u767e\u4e07\uff0c\u6570\u636e\u91cf\u6bd4\u8f83\u591a\uff1bkeys\u7b97\u6cd5\u662f\u904d\u5386\u7b97\u6cd5\uff0c\u590d\u6742\u5ea6\u662fO(n)\uff0c\u4e5f\u5c31\u662f\u6570\u636e\u8d8a\u591a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8a\u9ad8\u3002<\/p>\n<p>\u6570\u636e\u91cf\u8fbe\u5230\u51e0\u767e\u4e07\uff0c<strong>keys\u8fd9\u4e2a\u6307\u4ee4\u5c31\u4f1a\u5bfc\u81f4 Redis \u670d\u52a1\u5361\u987f<\/strong>\uff0c\u56e0\u4e3a <strong>Redis \u662f\u5355\u7ebf\u7a0b\u7a0b\u5e8f\uff0c\u987a\u5e8f\u6267\u884c\u6240\u6709\u6307\u4ee4\uff0c\u5176\u5b83\u6307\u4ee4\u5fc5\u987b\u7b49\u5230\u5f53\u524d\u7684 keys \u6307\u4ee4\u6267\u884c\u5b8c\u4e86\u624d\u53ef\u4ee5\u7ee7\u7eed<\/strong>\u3002<\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u90a3\u6211\u4eec\u5982\u4f55\u53bb<strong>\u904d\u5386\u5927\u6570\u636e\u91cf<\/strong>\u5462\uff1f\u8fd9\u4e2a\u4e5f\u662f\u9762\u8bd5\u7ecf\u5e38\u95ee\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u91c7\u7528redis\u7684\u53e6\u4e00\u4e2a<strong>\u547d\u4ee4scan\u3002<\/strong>\u6211\u4eec\u770b\u4e00\u4e0b<strong>scan\u7684\u7279\u70b9<\/strong><\/p>\n<blockquote>\n<p>1\u3001<strong>\u590d\u6742\u5ea6\u867d\u7136\u4e5f\u662f O(n)<\/strong>\uff0c\u4f46\u662f\u5b83\u662f\u901a\u8fc7\u6e38\u6807\u5206\u6b65\u8fdb\u884c\u7684\uff0c<strong>\u4e0d\u4f1a\u963b\u585e\u7ebf\u7a0b<\/strong><\/p>\n<p>2\u3001\u63d0\u4f9b count \u53c2\u6570\uff0c\u4e0d\u662f\u7ed3\u679c\u6570\u91cf\uff0c\u662fredis\u5355\u6b21\u904d\u5386\u5b57\u5178\u69fd\u4f4d\u6570\u91cf(\u7ea6\u7b49\u4e8e)<\/p>\n<p>3\u3001\u540c keys \u4e00\u6837\uff0c\u5b83\u4e5f\u63d0\u4f9b\u6a21\u5f0f\u5339\u914d\u529f\u80fd;<\/p>\n<p>4\u3001\u670d\u52a1\u5668\u4e0d\u9700\u8981\u4e3a\u6e38\u6807\u4fdd\u5b58\u72b6\u6001\uff0c\u6e38\u6807\u7684\u552f\u4e00\u72b6\u6001\u5c31\u662f <strong>scan \u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u6e38\u6807\u6574\u6570<\/strong>;<\/p>\n<p>5\u3001\u8fd4\u56de\u7684\u7ed3\u679c<strong>\u53ef\u80fd\u4f1a\u6709\u91cd\u590d\uff0c\u9700\u8981\u5ba2\u6237\u7aef\u53bb\u91cd\u590d<\/strong>\uff0c\u8fd9\u70b9\u975e\u5e38\u91cd\u8981;<\/p>\n<p>6\u3001\u5355\u6b21\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u7a7a\u7684\u5e76\u4e0d\u610f\u5473\u7740\u904d\u5386\u7ed3\u675f\uff0c\u800c\u8981\u770b<strong>\u8fd4\u56de\u7684\u6e38\u6807\u503c\u662f\u5426\u4e3a\u96f6<\/strong><\/p>\n<\/blockquote>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=JGn%2Bipm673tUhFBeXuwvKg%3D%3D.MkuGISUPm0Y%2FtfhU0H6Gxjraz5JjV5CI1f12J5%2ByyRA%2FxjcpUVUyYeucHyi%2BxINrcfS9ftY%2Bd6krQEHtasXnIQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<p><strong>\u4e00\u3001scan\u547d\u4ee4\u683c\u5f0f<\/strong><\/p>\n<pre><code>SCAN cursor [MATCH pattern] [COUNT count]<\/code><\/pre>\n<p><strong>\u4e8c\u3001\u547d\u4ee4\u89e3\u91ca\uff1a<\/strong>scan \u6e38\u6807 MATCH &lt;\u8fd4\u56de\u548c\u7ed9\u5b9a\u6a21\u5f0f\u76f8\u5339\u914d\u7684\u5143\u7d20&gt; count \u6bcf\u6b21\u8fed\u4ee3\u6240\u8fd4\u56de\u7684\u5143\u7d20\u6570\u91cf<\/p>\n<blockquote>\n<p>SCAN\u547d\u4ee4\u662f<strong>\u589e\u91cf\u7684\u5faa\u73af<\/strong>\uff0c\u6bcf\u6b21\u8c03\u7528\u53ea\u4f1a<strong>\u8fd4\u56de\u4e00\u5c0f\u90e8\u5206\u7684\u5143\u7d20<\/strong>\u3002\u6240\u4ee5\u4e0d\u4f1a\u8ba9redis\u5047\u6b7b<\/p>\n<p>SCAN\u547d\u4ee4\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6e38\u6807\uff0c<strong>\u4ece0\u5f00\u59cb\u904d\u5386\uff0c\u52300\u7ed3\u675f\u904d\u5386<\/strong><\/p>\n<\/blockquote>\n<p><strong>\u4e09\u3001\u4e3e\u4f8b<\/strong><\/p>\n<pre><code>redis &gt; scan 0 match user_token* count 5 \n 1) &quot;6&quot;\n 2) 1) &quot;user_token:1000&quot;\n 2) &quot;user_token:1001&quot;\n 3) &quot;user_token:1010&quot;\n 4) &quot;user_token:2300&quot;\n 5) &quot;user_token:1389&quot;<\/code><\/pre>\n<p>\u4ece0\u5f00\u59cb\u904d\u5386\uff0c\u8fd4\u56de\u4e86\u6e38\u68076\uff0c\u53c8\u8fd4\u56de\u4e86\u6570\u636e\uff0c\u7ee7\u7eedscan\u904d\u5386\uff0c\u5c31\u8981\u4ece6\u5f00\u59cb<\/p>\n<pre><code>redis &gt; scan 6 match user_token* count 5 \n 1) &quot;10&quot;\n 2) 1) &quot;user_token:3100&quot;\n 2) &quot;user_token:1201&quot;\n 3) &quot;user_token:1410&quot;\n 4) &quot;user_token:5300&quot;\n 5) &quot;user_token:3389&quot;<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd9\u4e2a\u662f<strong>\u9762\u8bd5\u7ecf\u5e38\u4f1a\u95ee\u5230\u7684<\/strong>\uff0c\u4e5f\u662f\u6211\u4eec\u5c0f\u4f19\u4f34\u5728<strong>\u5de5\u4f5c\u7684\u8fc7\u7a0b\u7ecf\u5e38\u7528\u7684<\/strong>\uff0c\u4e00\u822c\u5c0f\u516c\u53f8\uff0c\u4e0d\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46<strong>\u6570\u636e\u91cf\u591a\u7684\u65f6\u5019\uff0c\u4f60\u7684\u64cd\u4f5c\u65b9\u5f0f\u4e0d\u5bf9<\/strong>\uff0c\u4f60\u7684\u7ee9\u6548\u5c31\u4f1a\u88ab\u6263\u54e6\uff0c\u54c8\u54c8\u3002\u8c22\u8c22\uff01\uff01\uff01<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=Xx7ynNeztBcnXXmpm2Ooeg%3D%3D.YVjer5WlJUiRA1fyiGuD9ZNUWs%2FcFHw2uSP%2Fris1vUE%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=mYuUjwepkGONfZQuogmSBQ%3D%3D.RLVvWRUyZ7qo1IbTEeAchNXWXLU%2F0SW3KG6pzi%2BnbwAeZGqIgK8Oywn%2FDARu2ElHjhaFNTMX%2BOds3A7nFCJEIg%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=%2BC6b2ZA%2BBWCF4QDx4q8Erw%3D%3D.0jSH%2FuraviXGk1jCn7m9jlMzrjlVA3lhPDUAmwobXUmG9qVPmr53el2tLJB0R0XC\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=xjITVOLmWlsSYG%2Fe30Kekw%3D%3D.hSp9%2Bwen4s%2B2HkfmeVudt5qaG8Khc1Rqa20GHN8KhwNxJcsKSffkWTo1rBaQuRirvCtbmqfdY1DloBNY7YfjKw%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=8DCHMORsOmn%2F3UDwFcgBxg%3D%3D.KqhEfiQFRPzYK1Dak%2FCXwZI8z2Ng18Uu99qy%2BzkzsqeZFg3Wz7yyOVXrn9p86gVQL3TPLSMrhW5OcgCLt%2BVyo8VJQ5rsiMDVopM%2BmLqAGFQ%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044155698\">https:\/\/segmentfault.com\/a\/1190000044155698<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ahttps:\/\/www.toutiao.com\/article\/6697540366528152077\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008176","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008176"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008176\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}