{"id":3632,"date":"2018-12-26T18:43:34","date_gmt":"2018-12-26T10:43:34","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3632"},"modified":"2018-12-26T18:46:08","modified_gmt":"2018-12-26T10:46:08","slug":"%e5%88%a0%e9%99%a4-redis-%e5%a4%a7-key-%e8%ae%a9%e7%a8%8b%e5%ba%8f%e5%87%ba%e7%8e%b0%e9%9b%aa%e5%b4%a9%e5%af%bc%e8%87%b4%e7%a8%8b%e5%ba%8f%e5%91%98%e8%a2%ab%e5%bc%80%e9%99%a4%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3632","title":{"rendered":"\u5220\u9664 Redis \u5927 Key \u8ba9\u7a0b\u5e8f\u51fa\u73b0\u96ea\u5d29\u5bfc\u81f4\u7a0b\u5e8f\u5458\u88ab\u5f00\u9664\uff01"},"content":{"rendered":"<p>\n\t\u5bd2\u51ac\u4e86\uff0c\u4e5f\u5230\u5e74\u5e95\u4e86\uff0c\u5927\u5bb6\u90fd\u4e0d\u8981\u62a5\u4efb\u4f55\u4fa5\u5e78\u5fc3\u7406\u3002\u6700\u8fd1\u7684\u5bd2\u51ac\uff0c\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u5404\u4e2a\u516c\u53f8\u90fd\u5728\u60f3\u529e\u6cd5\u8fdb\u884c\u4eba\u5458\u4f18\u5316\u3002\u5404\u4f4d\u5343\u4e07\u522b\u51fa\u5dee\u9519\uff0c\u522b\u7ed9\u516c\u53f8\u627e\u9ebb\u70e6\uff0c\u7ed9\u60f3\u5f00\u9664\u4f60\u7684\u4eba\u627e\u501f\u53e3\uff01\u6700\u8fd1\u6211\u4eec\u516c\u53f8\u5c31\u53d1\u751f\u4e86\u4e00\u4ef6\u4e8b\uff0c\u4e00\u4e2a\u8bef\u64cd\u4f5c\uff0c\u5bfc\u81f4\u751f\u4ea7\u4e0a\u7684\u5e94\u7528\u53d1\u751f\u4e86\u96ea\u5d29\uff0c\u5f00\u53d1\u4eba\u5458\u88ab\u8feb\u79bb\u804c\u3002\n<\/p>\n<p>\n\t\u4eca\u5929\u6211\u4eec\u5c31\u6765\u590d\u76d8\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u8bef\u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u5927\u516c\u53f8\u7684\u6743\u9650\u7ba1\u7406\u6bd4\u8f83\u96c6\u4e2d\uff0c\u800c\u5c0f\u516c\u53f8\u5462\uff1f\u6743\u9650\u7ba1\u7406\u5c31\u6bd4\u8f83\u5206\u6563\u3002\u5bfc\u81f4\u5f00\u53d1\u4eba\u5458\u548c\u8fd0\u7ef4\u4eba\u5458\u7684\u6743\u9650\u8f83\u5927\uff0c\u4e00\u4e0d\u5c0f\u5fc3\u5c31\u51fa\u6545\u969c\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u7684\u8fd9\u4f4d\u5f00\u53d1\u4eba\u5458\uff0c\u6839\u636e\u9886\u5bfc\u7684\u8981\u6c42\uff0c\u60f3\u5728\u751f\u4ea7\u4e0a Redis \u7cfb\u7edf\u5220\u9664\u4e00\u4e2a Key\uff0c\u4f46\u662f\u8fd9\u4e2a Key \u592a\u5927\u4e86\uff0c\u5b58\u5728\u8bbe\u8ba1\u4e0d\u5408\u7406\u7684\u73b0\u8c61\u3002\u8fd9\u4f4d\u5f00\u53d1\u4eba\u5458\u63a5\u5230\u547d\u4ee4\u540e\uff0c\u76f4\u63a5\u5728\u751f\u4ea7\u73af\u5883\u4e0a\u8fdb\u884c\u4e86\u5927 Key \u7684\u5220\u9664\uff0c\u5bfc\u81f4 Redis \u8fdb\u7a0b\u88ab\u963b\u585e\u4e86\u5341\u51e0\u79d2\uff0c\u6700\u7ec8\u5bfc\u81f4\u96c6\u7fa4\u7684\u5bb9\u91cf\u548c\u8bf7\u6c42\u51fa\u73b0&rdquo;\u503e\u659c\u95ee\u9898&ldquo;\u3002\u5728\u52a0\u4e0a\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u67b6\u6784\u7684\u5c31\u7f3a\u4e4f\u5408\u7406\u6027\uff0c\u5f15\u8d77\u4e86\u96ea\u5d29\u3002\u4e00\u65f6\u95f4\u5ba2\u670d\u7535\u8bdd\u88ab\u6253\u7206\u4e86\u3002\n<\/p>\n<h2>\n\t\u4ec0\u4e48\u662f Redis \u7684\u5927\u5065\uff1f<br \/>\n<\/h2>\n<p>\n\t\u6bcf\u4e2a\u4eba\u53ef\u80fd\u90fd\u6709\u4e0d\u540c\u7684\u7406\u89e3\uff0c\u4f46\u6211\u8ba4\u4e3a\u53ef\u4ee5\u4ece\u7a7a\u95f4\u590d\u6742\u6027\u548c\u65f6\u95f4\u590d\u6742\u5ea6\u4e24\u4e2a\u65b9\u9762\u5b9a\u4e49\u5927 Key\u3002\n<\/p>\n<p>\n\t\u7a7a\u95f4\u590d\u6742\u6027\uff0c\u6307\u7684\u5c31\u662f\u5360\u7528\u7684\u5185\u5b58\u5927\u5c0f\u3002\u4f8b\u5982\u4e00\u4e2a String \u952e\u6700\u5927\u4e3a 512MB\u3002\u4f60\u7684 String \u952e\u6709 300MB\uff0c\u8fd9\u5c31\u7b97\u4e00\u4e2a\u5927 Key\u3002\n<\/p>\n<p>\n\t\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6307\u7684\u5c31\u662f\u4e00\u4e2a\u952e\uff0c\u5b58\u50a8\u7684\u5b57\u6bb5\u592a\u591a\u3002\u6bd4\u5982\u4e00\u4e2a Hash \u952e\uff0c\u4f60\u5b58\u50a8\u4e86 100000000 \u4e2a\u5b57\u6bb5\uff0c\u8fd9\u4e5f\u53ef\u4ee5\u7b97\u4e00\u4e2a\u5927 Key\u3002\u56e0\u4e3a\u5b83\u7684\u5bf9\u5e94\u8bbf\u95ee\u6a21\u5f0f(\u5982 hgetall )\u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\u3002\n<\/p>\n<p>\n\t\u4f46\u662f String \u952e\u867d\u7136\u7b97\u4e00\u4e2a\u5927 Key\uff0c\u6211\u4eec\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u7684\u8bdd\uff0cdel 200MB String \u952e\u8017\u65f6\u7ea6 1 \u6beb\u79d2\uff0c\u8fd8\u7b97\u6bd4\u8f83\u5feb\u7684\uff0c\u5f71\u54cd\u4e0d\u5927\u3002\n<\/p>\n<p>\n\t\u4f46\u662f\u4f60\u8981\u5220\u9664 1kw \u4e2a\u5b57\u6bb5\u7684 Hash \u952e\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u7279\u522b\u5c0f\u5fc3\u4e86\uff0c\u76f4\u63a5\u5220\u9664\u5c31\u4f1a\u963b\u585e Redis \u8fdb\u7a0b\u6570\u5341\u79d2\u5de6\u53f3\u7684\u65f6\u95f4\u3002\n<\/p>\n<p>\n\tRedis \u5b98\u7f51\u4e0a\u9488\u5bf9\u5220\u9664 DEL key \u547d\u4ee4\uff0c\u6709\u8fd9\u6837\u4e00\u53e5\u8bdd\uff1a\n<\/p>\n<blockquote>\n<p>\n\t\tTime complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).\n\t<\/p>\n<\/blockquote>\n<p>\n\t\u5927\u81f4\u7684\u610f\u601d\u5982\u4e0b\uff1a\n<\/p>\n<blockquote>\n<p>\n\t\t\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N)\uff0c\u5176\u4e2dN\u662f\u5c06\u88ab\u5220\u9664\u7684\u5bc6\u94a5\u6570\u3002\u5f53\u8981\u5220\u9664\u7684\u952e\u6301\u6709\u5b57\u7b26\u4e32\u4ee5\u5916\u7684\u503c\u65f6\uff0c\u6b64\u952e\u7684\u5355\u72ec\u590d\u6742\u5ea6\u662fO(M)\uff0c\u5176\u4e2dM\u662f\u5217\u8868\u3001\u96c6\u5408\u3001\u6392\u5e8f\u96c6\u6216\u6563\u5217\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002\u79fb\u9664\u5305\u542b\u5b57\u7b26\u4e32\u503c\u7684\u5355\u4e2a\u952e\u662fO(1)\u3002\n\t<\/p>\n<\/blockquote>\n<p>\n\t\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u90a3\u4e48\u8be5\u5982\u4f55\u6b63\u786e\u7684\u5220\u9664\u5927 Key \u5462\uff1f\n<\/p>\n<p>\n\t\u8fd9\u4e2a\u95ee\u9898\u6211\u8bb0\u5f97 3 \u7fa4\u91cc\u6709\u4eba\u66fe\u7ecf\u95ee\u8fc7\u6211\u3002\u4eca\u5929\u6211\u518d\u6765\u56de\u7b54\u4e00\u4e0b\uff01\n<\/p>\n<h2>\n\t\u5904\u7406 Redis \u5927 Key<br \/>\n<\/h2>\n<p>\n\t\u5408\u7406\u7684\u4f7f\u7528 Redis \u7684\u8fc7\u671f\u7b56\u7565\uff0credis \u4e2d\u5bf9\u4e8e\u6709\u8bbe\u7f6e\u8fc7\u671f\u7684 key \u6709\u4e09\u79cd\u5904\u7406\u65b9\u5f0f\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u88ab\u52a8\u5220\u9664\uff1a\u8fd9\u4e2a key \u4e0b\u4e00\u6b21\u88ab\u8bbf\u95ee\u5230\u7684\u65f6\u5019\u624d\u4f1a\u5220\u9664\u3002\n\t<\/li>\n<li>\n\t\t\u4e3b\u52a8\u5220\u9664\uff1aRedis \u4f1a\u5b9a\u671f\u4e3b\u52a8\u6dd8\u6c70\u4e00\u6279\u5df2\u8fc7\u671f\u7684 key\n\t<\/li>\n<li>\n\t\t\u5f53\u524d\u5df2\u7528\u5185\u5b58\u8d85\u8fc7 maxmemory \u9650\u5b9a\u65f6\uff0c\u89e6\u53d1\u4e3b\u52a8\u6e05\u7406\u7b56\u7565\n\t<\/li>\n<\/ul>\n<p>\n\t\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u8bbe\u8ba1\u4e0d\u5408\u7406\uff0ckey \u8d8a\u6765\u8d8a\u5927\uff0c\u60f3\u8981\u5408\u7406\u7684\u5220\u9664 key\uff0c\u5c31\u9700\u8981\u5faa\u73af\u5404\u4e2a key\uff0c\u4e00\u4e2a\u4e00\u4e2a\u7684\u5220\u3002\u8fd9\u6837 Redis \u5c31\u4e0d\u4f1a\u88ab\u957f\u671f\u7684\u963b\u585e\uff0c\u5bfc\u81f4\u610f\u5916\u53d1\u751f\u3002\n<\/p>\n<p>\n\tLarge Hash Key \u53ef\u4f7f\u7528 hscan \u547d\u4ee4\uff0c\u6bcf\u6b21\u83b7\u53d6 500 \u4e2a\u5b57\u6bb5\uff0c\u518d\u7528 hdel \u547d\u4ee4\uff0c\u6bcf\u6b21\u5220\u9664 1 \u4e2a\u5b57\u6bb5\u3002\n<\/p>\n<p>\n\tLarge Set Key \u53ef\u4f7f\u7528 sscan \u547d\u4ee4\uff0c\u6bcf\u6b21\u626b\u63cf\u96c6\u5408\u4e2d 500 \u4e2a\u5143\u7d20\uff0c\u518d\u7528 srem \u547d\u4ee4\u6bcf\u6b21\u5220\u9664\u4e00\u4e2a\u952e\u3002\n<\/p>\n<p>\n\tLarge List Key \u53ef\u901a\u8fc7 ltrim \u547d\u4ee4\u6bcf\u6b21\u5220\u9664\u5c11\u91cf\u5143\u7d20\u3002\n<\/p>\n<p>\n\tLarge Sorted Set Key \u4f7f\u7528 sortedset \u81ea\u5e26\u7684 zremrangebyrank \u547d\u4ee4\uff0c\u6bcf\u6b21\u5220\u9664 top 100 \u4e2a\u5143\u7d20\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4ee5 hscan \u4e3a\u4f8b\uff0c\u7ed9\u5927\u5bb6\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic static void delLargeHashKey(Jedis jedis){\r\n    \/\/ \u6e38\u6807\u521d\u59cb\u503c\u4e3a0\r\n    String cursor = ScanParams.SCAN_POINTER_START;\r\n    ScanParams scanParams = new ScanParams();\r\n    scanParams.count(1000);\r\n    String key = &quot;test:xttblog&quot;;\r\n    while (true){\r\n        \/\/\u4f7f\u7528hscan\u547d\u4ee4\u83b7\u53d6500\u6761\u6570\u636e\uff0c\u4f7f\u7528cursor\u6e38\u6807\u8bb0\u5f55\u4f4d\u7f6e\uff0c\u4e0b\u6b21\u5faa\u73af\u4f7f\u7528\r\n        ScanResult&amp;lt;Map.Entry&amp;lt;String, String&amp;gt;&amp;gt; hscanResult =\r\n            jedis.hscan(key, cursor, scanParams);\r\n        cursor = hscanResult.getStringCursor();\/\/ \u8fd4\u56de0 \u8bf4\u660e\u904d\u5386\u5b8c\u6210\r\n        List&amp;lt;Map.Entry&amp;lt;String, String&amp;gt;&amp;gt; scanResult =\r\n            hscanResult.getResult();\r\n        long t1 = System.currentTimeMillis();\r\n        for(int m = 0;m &amp;lt; scanResult.size();m++){\r\n            Map.Entry&amp;lt;String, String&amp;gt; mapentry = scanResult.get(m);\r\n            jedis.hdel(key, mapentry.getKey());\r\n        }\r\n        long t2 = System.currentTimeMillis();\r\n        System.out.println(&quot;\u5220\u9664&quot; + scanResult.size()\r\n            + &quot;\u6761\u6570\u636e\uff0c\u8017\u65f6: &quot; + (t2-t1) + &quot;\u6beb\u79d2,cursor:&quot; + cursor);\r\n        if (&quot;0&quot;.equals(cursor)){\r\n            break;\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u5176\u4ed6\u51e0\u4e2a\u7528\u6cd5\u90fd\u7c7b\u4f3c\uff0c\u6211\u5c31\u4e0d\u5728\u5217\u4e3e\u4e86\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Redis \u5220\u9664\u5927 Key\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/redis-del-large-key.png\" style=\"width: 829px;height: 199px\" \/>\n<\/p>\n<p>\n\t\u53c2\u8003\u4e0a\u9762\u7684\u56fe\uff0c\u5f53\u4f60\u7684 Key \u5728\u8fd9\u4e2a\u8303\u56f4\u7684\uff0c\u5220\u9664 Key \u5c31\u8981\u5c0f\u5fc3\u4e86\u3002\u5f53\u7136\u6211\u7684\u8fd9\u4e2a\u622a\u56fe\u548c\u5927\u5bb6\u7684\u786c\u4ef6\u73af\u5883\u4e4b\u7c7b\u7684\u4e0d\u4e00\u6837\uff0c\u53ef\u80fd\u7565\u6709\u5dee\u5f02\uff0c\u4ec5\u4f9b\u53c2\u8003\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3632\">\u5220\u9664 Redis \u5927 Key \u8ba9\u7a0b\u5e8f\u51fa\u73b0\u96ea\u5d29\u5bfc\u81f4\u7a0b\u5e8f\u5458\u88ab\u5f00\u9664\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5bd2\u51ac\u4e86\uff0c\u4e5f\u5230\u5e74\u5e95\u4e86\uff0c\u5927\u5bb6\u90fd\u4e0d\u8981\u62a5\u4efb\u4f55\u4fa5\u5e78\u5fc3\u7406\u3002\u6700\u8fd1\u7684\u5bd2\u51ac\uff0c\u5927\u5bb6\u90fd\u77e5\u9053\uff0c\u5404\u4e2a\u516c\u53f8\u90fd\u5728\u60f3\u529e\u6cd5\u8fdb\u884c\u4eba\u5458\u4f18\u5316\u3002\u5404\u4f4d\u5343\u4e07 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[1853,1843,669,1852,1851],"class_list":["post-3632","post","type-post","status-publish","format-standard","hentry","category-java","category-yyzt","tag-hscan","tag-jedis","tag-redis","tag-scanparams","tag-scanresult"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3632"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3632\/revisions"}],"predecessor-version":[{"id":3634,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3632\/revisions\/3634"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}