{"id":1991,"date":"2023-04-01T10:55:33","date_gmt":"2023-04-01T02:55:33","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1991"},"modified":"2023-04-22T07:41:30","modified_gmt":"2023-04-21T23:41:30","slug":"playing-redis-kill-nail-houses-key-with-no-expiration-date-set","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/playing-redis-kill-nail-houses-key-with-no-expiration-date-set\/","title":{"rendered":"\u73a9\u8f6cRedis-\u5e72\u6389\u9489\u5b50\u6237-\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u524d\u6bb5\u65f6\u95f4\u516c\u53f8\u6709\u65b0\u4e1a\u52a1\u9700\u8981\u4f7f\u7528Redis\uff0c\u4e8e\u662f\u67e5\u770b\u4e86\u751f\u4ea7\u4e00Redis\u96c6\u7fa4\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u7528\u4e8e\u8bc4\u4f30\u662f\u5426\u80fd\u76f4\u63a5\u63a5\u5165\u65b0\u4e1a\u52a1\u3002\u6b64Redis\u96c6\u7fa4\u8d2d\u4e70\u7684\u963f\u91cc\u4e91\u96c6\u7fa4\u793e\u533a\u7248\uff0c8\u8282\u70b932G\uff1b<\/p>\n<p>\u4e0d\u770b\u4e0d\u77e5\u9053\uff0c\u4e00\u770b\u5413\u4e00\u8df3\u3002Redis\u5b9e\u4f8b\u5171\u8ba1 450W key\uff0c\u5176\u4e2d 230W \u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\uff0c\u4e5f\u5c31\u662f\u8bf4\u8db3\u8db3\u6709 220W key\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u3002 What !!! \u63a5\u8fd1 50% \u7684\u6570\u636e\u6ca1\u6709\u8fc7\u671f\u65f6\u95f4\uff0c\u5b8c\u5168\u4e0d\u7b26\u5408\u5e38\u7406\uff0c\u4e25\u91cd\u6d6a\u8d39\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4e0d\u628a\u8fd9\u4e9b\u9489\u5b50\u6237\u63ea\u51fa\u6765\uff0c\u201c\u8a93\u4e0d\u4e3a\u733f\u201d\u3002<\/p>\n<h2>\u5982\u4f55\u67e5\u8be2Redis\u4e2d\u672a\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key\u6570\u91cf<\/h2>\n<h3>\u4f7f\u7528\u963f\u91cc\u4e91\u3010\u4e91\u6570\u636e\u5e93Redis\u7248\u7ba1\u7406\u63a7\u5236\u53f0\u3011\u67e5\u770b<\/h3>\n<p>\u5982\u679cRedis\u96c6\u7fa4\u662f\u4e91\u5382\u5546\u63d0\u4f9b\u7684\uff0c\u5382\u5546\u4e00\u822c\u4f1a\u63d0\u4f9b\u7ba1\u7406\u63a7\u5236\u53f0\u7684\u3002\u963f\u91cc\u4e91\u63a7\u5236\u53f0\u641c\u7d22\u201c\u4e91\u6570\u636e\u5e93Redis\u7248\u201d\u5373\u53ef\u8fdb\u5165\u3010\u4e91\u6570\u636e\u5e93Redis\u7248\u7ba1\u7406\u63a7\u5236\u53f0\u3011\u3002<\/p>\n<h4>\u67e5\u770b\u96c6\u7fa4\u6216\u8282\u70b9\u7684Key\u53d8\u5316\u60c5\u51b5<\/h4>\n<p>\uff081\uff09\u8fdb\u5165\u3010\u6027\u80fd\u76d1\u63a7\u3011\uff0c\u9ed8\u8ba4\u9009\u62e9\u3010\u6570\u636e\u8282\u70b9\u805a\u5408\u6307\u6807\u3011\uff0c\u6b64\u5904\u662f\u533a\u95f4\u6298\u7ebf\u56fe<\/p>\n<ul>\n<li>\u805a\u5408\u6307\u6807\u53ef\u67e5\u770b\u6574\u4e2a\u96c6\u7fa4\u7684\u6570\u636e\u60c5\u51b5<\/li>\n<li>\u53ef\u901a\u8fc7\u8c03\u6574\u201c\u67e5\u8be2\u65f6\u95f4\u201d\u6269\u5927\u6570\u636e\u5206\u6790\u8303\u56f4<\/li>\n<li>\u4ea6\u53ef\u9009\u62e9\u3010\u6570\u636e\u8282\u70b9\u3011\u67e5\u770b\u6307\u5b9a\u8282\u70b9\u7684\u6570\u636e\u60c5\u51b5<\/li>\n<\/ul>\n<p>\u8fdb\u5165\u3010CloudDBA\u3011\uff0c\u8fdb\u5165\u3010\u6027\u80fd\u8d8b\u52bf\u3011\uff0c\u53ef\u67e5\u770b\u533a\u95f4\u6298\u7ebf\u56fe<\/p>\n<ul>\n<li>[Keys]\u4e00\u680f\uff0c\u53ef\u67e5\u770b key\u603b\u91cf\u3001\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4key\u6570\u91cf\u3001\u5df2\u8fc7\u671fkey\u6570\u91cf \u7684\u53d8\u5316\u4fe1\u606f<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6280\u672f\/Redis-\u963f\u91cc\u4e91CloudDBA\u6027\u80fd\u8d8b\u52bf.png\" alt=\"\u963f\u91cc\u4e91CloudDBA\u6027\u80fd\u8d8b\u52bf\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6280\u672f\/Redis-\u963f\u91cc\u4e91CloudDBA\u6027\u80fd\u8d8b\u52bfKeys.png\" alt=\"\u963f\u91cc\u4e91CloudDBA\u6027\u80fd\u8d8b\u52bfKeys\" \/><\/p>\n<h4>\u67e5\u770b\u5168\u5c40\u8282\u70b9\u7684\u5b9e\u65f6\u6027\u80fd<\/h4>\n<ul>\n<li>\u8fdb\u5165\u3010CloudDBA\u3011\uff0c\u8fdb\u5165\u3010\u5b9e\u65f6\u6027\u80fd\u3011\uff0c\u9009\u62e9\u3010\u5168\u5c40\u8282\u70b9\u5b9e\u65f6\u6027\u80fd\u3011<\/li>\n<li>\u9ed8\u8ba4\u52fe\u9009\u201c\u81ea\u52a8\u5237\u65b0(5\u79d2)\u201d\uff0c\u5373\u53ef\u67e5\u770b\u5404\u8282\u70b9\u7684\u5b9e\u65f6\u6027\u80fd<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6280\u672f\/Redis-\u963f\u91cc\u4e91CloudDBA\u5168\u5c40\u8282\u70b9\u5b9e\u65f6\u6027\u80fd.png\" alt=\"\u963f\u91cc\u4e91CloudDBA\u5168\u5c40\u8282\u70b9\u5b9e\u65f6\u6027\u80fd\" \/><\/p>\n<h4>\u4e91\u6570\u636e\u5e93Redis\u76d1\u63a7\u6307\u6807\u8bf4\u660e<\/h4>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u76d1\u63a7\u6307\u6807<\/th>\n<th style=\"text-align: left;\">\u5355\u4f4d<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">Keys<\/td>\n<td style=\"text-align: left;\">Counts<\/td>\n<td style=\"text-align: left;\">Key\u603b\u6570\u91cf\uff0c\u5b9e\u4f8b\u5b58\u50a8\u7684\u4e00\u7ea7Key\u603b\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Expires<\/td>\n<td style=\"text-align: left;\">Counts<\/td>\n<td style=\"text-align: left;\">\u5b9e\u4f8b\u4e2d\u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684\u952e\u503c\u5bf9\u6570\u91cf\uff0c\u8be5\u6307\u6807\u5c55\u793a\u7684\u662f\u91c7\u96c6\u6570\u636e\u65f6\u7684\u77ac\u65f6\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">ExpiredKeys<\/td>\n<td style=\"text-align: left;\">Counts<\/td>\n<td style=\"text-align: left;\">\u5386\u53f2\u7d2f\u8ba1\u6dd8\u6c70\u7684Key\u603b\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">EvictedKeys<\/td>\n<td style=\"text-align: left;\">Counts<\/td>\n<td style=\"text-align: left;\">\u5386\u53f2\u7d2f\u8ba1\u9a71\u9010\u7684Key\u603b\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">ExpiredKeysPerSecond<\/td>\n<td style=\"text-align: left;\">Counts\/s<\/td>\n<td style=\"text-align: left;\">\u6bcf\u79d2\u88ab\u6dd8\u6c70\u7684Key\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">EvictedKeysPerSecond<\/td>\n<td style=\"text-align: left;\">Counts\/s<\/td>\n<td style=\"text-align: left;\">\u6bcf\u79d2\u88ab\u9a71\u9010\u7684Key\u6570\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53c2\u8003\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/help.aliyun.com\/document_detail\/43887.html\">https:\/\/help.aliyun.com\/document_detail\/43887.html<\/a><\/p>\n<h3>\u4f7f\u7528Redis\u547d\u4ee4\u884c\u67e5\u770b<\/h3>\n<p>\u6267\u884c<code>info Keyspace<\/code>\u6307\u4ee4\u5373\u53ef\uff0c\u5176\u4e2d keys \u8868\u793a \u96c6\u7fa4\u4e2d key \u7684\u603b\u91cf\uff08\u5927\u7ea6 455W\uff09\uff0cexpires \u8868\u793a \u8bbe\u7f6e\u4e86\u8fc7\u671f\u65f6\u95f4\u7684key\u7684\u603b\u91cf\uff08\u5927\u7ea6 240W\uff09<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; info Keyspace\n# Keyspace\ndb0:keys=4551001,expires=2405155,avg_ttl=219009799007<\/code><\/pre>\n<p>\u4f7f\u7528\u547d\u4ee4\u884c\u67e5\u770b\u7684\u7f3a\u70b9\u662f\uff1a\u65e0\u6cd5\u67e5\u9605\u6570\u636e\u4e00\u6bb5\u65f6\u95f4\u7684\u53d8\u5316\u60c5\u51b5\uff08\u5f53\u7136\u8fd0\u7ef4\u53ef\u4ee5\u81ea\u884c\u642d\u5efa\u76d1\u63a7\u4f53\u7cfb\uff0c\u666e\u7f57\u7c73\u4fee\u65af\u3001ELK\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff09\uff0c\u6240\u4ee5\u4e91\u5382\u5546\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u65b9\u4fbf<\/p>\n<h2>\u5bfc\u51faRedis\u4e2d\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key<\/h2>\n<p>\u4e00\u60f3\u7740\u5feb 50% \u7684\u8d44\u6e90\u6d6a\u8d39\u4e86\u5c31\u65e0\u6bd4\u75db\u5fc3\uff0cSo \u5fc5\u987b\u5bfc\u51fa\u8fd9\u4e9b\u9489\u5b50\u6237\u5206\u6790\u7f6a\u9b41\u7978\u9996\uff0c\u7136\u540e\u5e72\u6389\u4ed6\u3002<\/p>\n<p>\u539f\u672c\u60f3\u7740\u963f\u91cc\u4e91\u5e94\u8be5\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u7ed3\u679c\u5728\u5b98\u7f51\u67e5\u770b\u4ee5\u53ca\u8be2\u95ee\u963f\u91cc\u4e91\u6280\u672f\u652f\u6301\uff0c\u53d1\u73b0\u963f\u91cc\u4e91\u5e76\u672a\u63d0\u4f9b\u6b64\u529f\u80fd\uff0c\u53ea\u80fd\u81ea\u5df1\u6363\u9f13\u4e86\u3002<\/p>\n<p>\u7531\u4e8e\u662f\u751f\u4ea7\u73af\u5883\uff0c\u6240\u4ee5<strong>\u3010\u5b89\u5168\u6027\u3001\u7a33\u5b9a\u6027\u3011<\/strong>\u5c24\u5176\u91cd\u8981\uff0c\u7edd\u4e0d\u80fd\u5f71\u54cd\u751f\u4ea7\u73af\u5883\u7684\u6b63\u5e38\u4f7f\u7528\u3002\u7f51\u4e0a\u67e5\u8be2\u4e86\u76f8\u5173\u8d44\u6599\uff0c\u6700\u7ec8\u5f62\u6210\u5982\u4e0b2\u79cd\u811a\u672c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528shell\u811a\u672c\u5bfc\u51fa\u6570\u636e<\/li>\n<li>\n<ul>\n<li>\u65e0\u957f\u8fde\u63a5\uff0c\u76f8\u5bf9\u8f83\u6162\uff0c\u5927\u91cf\u8fde\u63a5\u5bf9Redis\u6709\u4e00\u5b9a\u5f71\u54cd<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>\u6570\u636e\u91cf\u8f83\u5927\u65f6\u6ce8\u610f\u5408\u7406\u914d\u7f6e\u4f11\u7720\u65f6\u95f4<\/li>\n<\/ul>\n<\/li>\n<li>\u4f7f\u7528python\u811a\u672c\u5bfc\u51fa\u6570\u636e<\/li>\n<\/ul>\n<h3>\u4f7f\u7528shell\u811a\u672c\u5bfc\u51fa\u6570\u636e<\/h3>\n<p>\u811a\u672c\u90e8\u5206\u53c2\u6570\u53ef\u914d\u7f6e\uff0c\u53ef\u6309\u9700\u4fee\u6539\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n<ul>\n<li>db_ip\uff1aRedis\u8fde\u63a5\u5730\u5740<\/li>\n<li>db_port\uff1aRedis\u8fde\u63a5\u7aef\u53e3\uff0c\u53ef\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165<\/li>\n<li>password\uff1aRedis\u8fde\u63a5\u5bc6\u7801\uff0c\u53ef\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165<\/li>\n<li>cursor\uff1a\u7b2c\u4e00\u6b21 scan \u8fed\u4ee3\u6e38\u6807\uff0c\u9ed8\u8ba40\uff0c\u9996\u6b21\u4f7f\u7528\u4e0d\u5efa\u8bae\u4fee\u6539<\/li>\n<li>cnt\uff1a\u6bcf\u6b21 scan \u8fed\u4ee3\u7684\u6570\u91cf\uff0c\u9ed8\u8ba41000\uff0c\u53ef\u6839\u636e\u751f\u4ea7\u60c5\u51b5\u8c03\u6574\u4f18\u5316<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\u811a\u672c\u9ed8\u8ba4\u6bcfscan(ttl) 1000 \u4e2akey\u5373\u4f11\u77200.1\u79d2<\/li>\n<li>shell \u811a\u672c\u65e0\u6cd5\u7ef4\u62a4\u957f\u8fde\u63a5\uff0c\u6240\u4ee5\u6bcf\u6b21ttl\u90fd\u5c06\u521b\u5efa\u8fde\u63a5\uff0c\u6027\u80fd\u6709\u4e00\u5b9a\u5f71\u54cd<\/li>\n<li>\u6709\u60f3\u6cd5\u4f7f\u7528lua\u811a\u672c\u6279\u91cf ttl \u63d0\u5347\u6027\u80fd\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e0b\uff0c\u6b22\u8fce\u5206\u4eab\u53cd\u9988<\/li>\n<\/ul>\n<p>\u811a\u672c\u6267\u884c\uff1a<\/p>\n<p>\u6d4b\u8bd5 Redis \u5355\u673a\u7248 \u5171 30W key\uff0c\u6309\u7167\u9ed8\u8ba4\u7684\u4f11\u7720\u9891\u7387\uff08scan 1K \u4e2akey\u4f11\u77200.1\u79d2\uff09\u603b\u8ba1\u8017\u65f612\u5206\u949f\uff08\u53ef\u81ea\u884c\u4f18\u5316\u8c03\u6574\uff09\uff0c\u626b\u63cf\u7ed3\u679c\u7684 key \u4fdd\u5b58\u5728 \u5f53\u524d\u76ee\u5f55\u7684<code>no_ttlkey.log<\/code>\u4e2d\u3002<\/p>\n<pre><code class=\"language-bash\">[redis@xxx redis]$ time .\/checknottl.sh 6378 password\nWarning: Using a password with &#039;-a&#039; or &#039;-u&#039; option on the command line interface may not be safe.\nscan_num: 0\n\nreal    12m14.810s\nuser    4m14.343s\nsys    5m41.646s<\/code><\/pre>\n<p>\u811a\u672c\u5982\u4e0b\uff0c\u5c06\u4ee5\u4e0b\u811a\u672c\u5185\u5bb9\u4fdd\u5b58\u4e3a<code>checknottl.sh<\/code>\uff0c\u518d\u6267\u884c<code>chmod u+x *.sh<\/code>\u6388\u4e88\u6267\u884c\u6743\u9650\uff1a<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\n# \u67e5\u8be2\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key\n# \u811a\u672c\u5730\u5740\uff1ahttps:\/\/github.com\/zxiaofan\/OpenSource_Study\/tree\/master\/redis_scripts\uff1b\n# \u9700\u5728redis-cli \u76ee\u5f55\u6267\u884c\uff0c\u6216\u8005\u4fee\u6539\u811a\u672c\u4e2d\u7684\u8def\u5f84\n# checknottl.sh\uff0c\u9ed8\u8ba4\u6bcfscan(ttl) 1000 \u4e2akey\u5373\u4f11\u77200.1\u79d2\n# \u811a\u672c\u53c2\u8003\u7f51\u7edc\uff0c\u5df2\u505a\u4e00\u5b9a\u8c03\u6574\u4f18\u5316\uff0c\u66f4\u591aRedis\u7cfb\u5217\u6587\u7ae0\u53ef\u524d\u5f80\uff1ahttps:\/\/blog.csdn.net\/u010887744\/category_9356949.html\uff1b\n# Note: \n#    shell \u811a\u672c\u65e0\u6cd5\u7ef4\u62a4\u957f\u8fde\u63a5\uff0c\u6240\u4ee5\u6bcf\u6b21ttl\u90fd\u5c06\u521b\u5efa\u8fde\u63a5\uff0c\u6027\u80fd\u6709\u4e00\u5b9a\u5f71\u54cd\n#     \u6709\u60f3\u6cd5\u4f7f\u7528lua\u811a\u672c\u6279\u91cf ttl \u63d0\u5347\u6027\u80fd\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e0b\uff0c\u6b22\u8fce\u5206\u4eab\u53cd\u9988\n\ndb_ip=127.0.0.1      # Redis ip\ndb_port=$1           # Redis \u7aef\u53e3\npassword=$2          # Redis \u5bc6\u7801\ncursor=0             # \u7b2c\u4e00\u6b21\u6e38\u6807\ncnt=1000             # \u6bcf\u6b21\u8fed\u4ee3\u7684\u6570\u91cf\nnew_cursor=0         # \u4e0b\u4e00\u6b21\u6e38\u6807\nscan_num=0           # \u5df2scan\u7684key\u6570\u91cf\n\n.\/redis-cli -h $db_ip -p $db_port -a $password scan $cursor count $cnt &gt; scan_tmp_result\nnew_cursor=`sed -n &#039;1p&#039; scan_tmp_result`             # \u83b7\u53d6\u4e0b\u4e00\u6b21\u6e38\u6807\nsed -n &#039;2,$p&#039; scan_tmp_result &gt; scan_result          # \u83b7\u53d6 keys\n\ncat scan_result |while read line                     # \u5faa\u73af\u904d\u5386\u6240\u6709 keys\n\n# 2&gt;\/dev\/null\uff0c\u5c06\u6807\u51c6\u9519\u8bef\u4e22\u5f03\uff0cWarning: Using a password with &#039;-a&#039; or &#039;-u&#039; option on the command line interface may not be safe.\ndo\n    ttl_result=`.\/redis-cli -h $db_ip -p $db_port -a $password ttl $line 2&gt;\/dev\/null`      # \u4f7f\u7528 ttl \u6307\u4ee4\u83b7\u53d6 key \u8fc7\u671f\u65f6\u95f4\n    # $scan_num +=1;\n    if [[ $ttl_result == -1 ]];then                  # \u5224\u65ad\u8fc7\u671f\u65f6\u95f4\uff0c-1 \u662f\u4e0d\u8fc7\u671f\n        echo $line &gt;&gt; no_ttlkey.log                     # \u8ffd\u52a0\u5230\u6307\u5b9a\u6587\u4ef6\n    fi\ndone\n\necho &#039;scan_num: &#039;$scan_num\nwhile [ $cursor -ne $new_cursor ]    # \u82e5 \u6e38\u6807 \u4e0d\u4e3a 0 \uff0c\u5219\u8bc1\u660e\u6ca1\u6709\u8fed\u4ee3\u5b8c\u6240\u6709\u7684 key\uff0c\u7ee7\u7eed\u6267\u884c\ndo\n    .\/redis-cli -h $db_ip -p $db_port -a $password scan $new_cursor count $cnt 2&gt;\/dev\/null&gt; scan_tmp_result\n    new_cursor=`sed -n &#039;1p&#039; scan_tmp_result`\n    sed -n &#039;2,$p&#039; scan_tmp_result &gt; scan_result\n    cat scan_result |while read line\n    do\n        ttl_result=`.\/redis-cli -h $db_ip -p $db_port -a $password ttl $line 2&gt;\/dev\/null`\n        # $scan_num +=1;\n        if [[ $ttl_result == -1 ]];then\n            echo $line &gt;&gt; no_ttlkey.log\n        fi\n\n        #if [ $scan_num % 1000 == 0 ];then\n        #    sleep 0.5\n        #fi\n    done\n    sleep 0.1\ndone\nrm -f scan_tmp_result\nrm -f scan_result<\/code><\/pre>\n<h3>\u4f7f\u7528python\u811a\u672c\u5bfc\u51fa\u6570\u636e<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528python\uff0c\u5176\u5b9e\u4e0a\u9762\u5df2\u7ecf\u63d0\u5230\uff0cshell\u65e0\u6cd5\u7ef4\u62a4\u957f\u8fde\u63a5\uff0c\u5728Redis\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u91cf\u7684 ttl \u5c06\u521b\u5efa \u6d77\u91cf\u8fde\u63a5\uff0c\u5bf9\u6027\u80fd\u4ee5\u53caRedis\u7684\u7a33\u5b9a\u6027\u90fd\u6709\u4e00\u5b9a\u5f71\u54cd\u3002<\/p>\n<p>\u811a\u672c\u90e8\u5206\u53c2\u6570\u53ef\u901a\u8fc7\u6307\u4ee4\u4f20\u5165\uff0c\u53ef\u6309\u9700\u4fee\u6539\u811a\u672c\uff1a<\/p>\n<ul>\n<li><code>-host<\/code>\uff1aRedis\u8fde\u63a5\u5730\u5740\uff0c\u9ed8\u8ba4127.0.0.1<\/li>\n<li><code>-p<\/code>\uff1aRedis\u8fde\u63a5\u7aef\u53e3<\/li>\n<li><code>-d<\/code>\uff1a\u9700\u8981\u626b\u63cf\u7684db\uff0c\u9ed8\u8ba4\u4ec5\u626b\u63cf0\u5e93<\/li>\n<li><code>-a<\/code>\uff1aRedis\u8fde\u63a5\u5bc6\u7801<\/li>\n<li><code>-sn<\/code>\uff1asacn\u6307\u5b9a\u6570\u91cf\u7684key\u540e\u4fbf\u4f11\u77201\u79d2<\/li>\n<\/ul>\n<p>\u811a\u672c\u6267\u884c\uff1a<\/p>\n<p>\u6d4b\u8bd5 Redis \u5355\u673a\u7248 \u5171 30W key\uff0c\u6309\u7167\u9ed8\u8ba4\u7684\u4f11\u7720\u9891\u7387\uff08scan 1K \u4e2akey\u4f11\u77200.5\u79d2\uff09\u603b\u8ba1\u8017\u65f64\u5206\u949f\uff08\u53ef\u81ea\u884c\u4f18\u5316\u8c03\u6574\uff09\uff0c\u626b\u63cf\u7ed3\u679c\u7684 key \u4fdd\u5b58\u5728 \u5f53\u524d\u76ee\u5f55\u7684<code>{port}_{db}_no_ttl_keys.txt<\/code>\u4e2d\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u867d\u7136Python\u7248\u672c\u7684\u4f11\u7720\u65f6\u95f4\u66f4\u957f\uff0c\u4f46\u662f\u6027\u80fd\u5374\u63d0\u5347\u4e86\u5f88\u591a\u3002\u540c\u6837\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u53ef\u7ef4\u62a4\u8fde\u63a5\u6c60\u7684\u8bed\u8a00\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-bash\">[redis@xxx redis]$ python checknottl.py -p 6378 -d 0 -a password\nthere are 300005 keys in db[0] \nstartTime of db[0] is \uff1a2020-12-06 10:40:09\n[&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;]100.00%\ndone\nendTime of db[0] is \uff1a2020-12-06 10:44:24\nIt takes 254.684270859 seconds :\nno ttl keys number is : 300002\nthe file of keys with no ttl: .\/6378_0_no_ttl_keys.txt<\/code><\/pre>\n<p>\u5c06\u4ee5\u4e0b\u811a\u672c\u5185\u5bb9\u4fdd\u5b58\u5230<code>checknottl.py<\/code>\uff0c\u5b89\u88c5Python\u540e\u518d\u6267\u884c\u4e0a\u9762\u7684\u547d\u4ee4\u5373\u53ef<\/p>\n<pre><code class=\"language-python\"># encoding: utf-8\n&quot;&quot;&quot;\nmodify: zxiaofan\ndate: 2020-12-05\nfunc: \u67e5\u627eRedis\u4e2d\u6ca1\u6709\u8bbe\u7f6ettl\u7684 key\n\u811a\u672c\u53c2\u8003\u7f51\u7edc\uff08auther\u6768\u5947\u9f99\uff09\uff0c\u5df2\u505a\u4e00\u5b9a\u8c03\u6574\u4f18\u5316\uff0c\u66f4\u591aRedis\u7cfb\u5217\u6587\u7ae0\u53ef\u524d\u5f80\uff1ahttps:\/\/blog.csdn.net\/u010887744\/category_9356949.html\n\u811a\u672c\u5730\u5740\uff1ahttps:\/\/github.com\/zxiaofan\/OpenSource_Study\/tree\/master\/redis_scripts\uff1b\nNote:\n    \u5982\u679c\u63d0\u793a\u6ca1\u6709Redis\u6a21\u5757\u201cImportError: No module named redis\u201d\n    \u8bf7\u6267\u884c\u201cpython -m pip install redis\u201d\u5b89\u88c5Redis\u6a21\u5757\n    \u9ed8\u8ba4\u6bcf\u626b\u63cf1000\u4e2akey\u5373\u4f11\u77200.5\u79d2\n&quot;&quot;&quot;\nimport redis\nimport argparse\nimport time\nimport sys\n\nclass ShowProcess:\n    &quot;&quot;&quot;\n    \u663e\u793a\u5904\u7406\u8fdb\u5ea6\u7684\u7c7b\n    \u8c03\u7528\u8be5\u7c7b\u76f8\u5173\u51fd\u6570\u5373\u53ef\u5b9e\u73b0\u5904\u7406\u8fdb\u5ea6\u7684\u663e\u793a\n    &quot;&quot;&quot;\n    i = 0 # \u5f53\u524d\u7684\u5904\u7406\u8fdb\u5ea6\n    max_steps = 0 # \u603b\u5171\u9700\u8981\u5904\u7406\u7684\u6b21\u6570\n    max_arrow = 50 # \u8fdb\u5ea6\u6761\u7684\u957f\u5ea6\n\n    # \u521d\u59cb\u5316\u51fd\u6570\uff0c\u9700\u8981\u77e5\u9053\u603b\u5171\u7684\u5904\u7406\u6b21\u6570\n    def __init__(self, max_steps):\n        self.max_steps = max_steps\n        self.i = 0\n\n    # \u663e\u793a\u51fd\u6570\uff0c\u6839\u636e\u5f53\u524d\u7684\u5904\u7406\u8fdb\u5ea6i\u663e\u793a\u8fdb\u5ea6\n    # \u6548\u679c\u4e3a[&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;]100.00%\n    def show_process(self, i = None):\n        if i is not None:\n            self.i = i\n        else:\n            self.i += 1\n        num_arrow = int(self.i * self.max_arrow \/ self.max_steps) # \u8ba1\u7b97\u663e\u793a\u591a\u5c11\u4e2a&#039;&gt;&#039;\n        num_line = self.max_arrow - num_arrow # \u8ba1\u7b97\u663e\u793a\u591a\u5c11\u4e2a&#039;-&#039;\n        percent = self.i * 100.0 \/ self.max_steps # \u8ba1\u7b97\u5b8c\u6210\u8fdb\u5ea6\uff0c\u683c\u5f0f\u4e3axx.xx%\n        process_bar = &#039;[&#039; + &#039;&gt;&#039; * num_arrow + &#039; &#039; * num_line + &#039;]&#039;\\\n                      + &#039;%.2f&#039; % percent + &#039;%&#039; + &#039;\\r&#039; # \u5e26\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\uff0c&#039;\\r&#039;\u8868\u793a\u4e0d\u6362\u884c\u56de\u5230\u6700\u5de6\u8fb9\n        sys.stdout.write(process_bar) # \u8fd9\u4e24\u53e5\u6253\u5370\u5b57\u7b26\u5230\u7ec8\u7aef\n        sys.stdout.flush()\n\n    def close(self, words=&#039;done&#039;):\n        print &#039;&#039;\n        print words\n        self.i = 0\n\ndef check_ttl(redis_conn, no_ttl_file, dbindex, scannum_thensleep):\n    start_time = time.time()\n    no_ttl_num = 0\n    scan_num = 0\n    keys_num = redis_conn.dbsize()\n    print &quot;there are {num} keys in db[{index}] &quot;.format(num=keys_num, index=dbindex)\n    # \u6253\u5370\u626b\u63cfdb\u5f00\u59cb\u65f6\u95f4\n    print &quot;startTime of db[{index}] is \uff1a{start_time}&quot;.format(index=dbindex, start_time=time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime()))\n    process_bar = ShowProcess(keys_num)\n    with open(no_ttl_file, &#039;a&#039;) as f:\n\n        for key in redis_conn.scan_iter(count=1000):\n            process_bar.show_process()\n            if redis_conn.ttl(key) == -1:\n                no_ttl_num += 1\n                if no_ttl_num &lt; 1000:\n                    f.write(key+&#039;\\n&#039;)\n            else:\n                continue\n\n            scan_num +=1;\n            if(scan_num % scannum_thensleep == 0): # scan\u6307\u5b9a\u6570\u91cf\u540e\u5373\u4f11\u7720\n                time.sleep(0.5);\n\n    process_bar.close()\n    # \u6253\u5370\u626bdb\u63cf\u7ed3\u675f\u65f6\u95f4\n    print &quot;endTime of db[{index}] is \uff1a{end_time}&quot;.format(index=dbindex, end_time=time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime()))\n    print &quot;It takes {sec} seconds :&quot;.format(sec=(time.time() - start_time))\n    print &quot;no ttl keys number is :&quot;, no_ttl_num\n    print &quot;the file of keys with no ttl: %s&quot; % no_ttl_file\n\ndef main():\n    parser = argparse.ArgumentParser()\n    parser.add_argument(&#039;-p&#039;, type=int, dest=&#039;port&#039;, action=&#039;store&#039;, help=&#039;port of redis &#039;)\n    parser.add_argument(&#039;-d&#039;, type=str, dest=&#039;db_list&#039;, action=&#039;store&#039;, default=0,\n                        help=&#039;ex : -d all or -d 1,2,3,4 &#039;)\n    parser.add_argument(&#039;-host&#039;, type=str, dest=&#039;host&#039;, action=&#039;store&#039;, default=&#039;127.0.0.1&#039;,\n                        help=&#039; Redis host&#039;) # 20201205\uff0c\u652f\u6301\u8fde\u63a5\u8fdc\u7a0bRedis\n    parser.add_argument(&#039;-a&#039;, type=str, dest=&#039;password&#039;, action=&#039;store&#039;, default= None,\n                        help=&#039; Redis Password&#039;) # 20201205\uff0c\u652f\u6301\u4f20\u5165Redis\u5bc6\u7801\n    parser.add_argument(&#039;-sn&#039;, type=str, dest=&#039;scannum_thensleep&#039;, action=&#039;store&#039;, default=2000,\n                        help=&#039;After the number of scanning keys reaches [scannum_thensleep], sleep for 1 second &#039;) # 20201205\uff0c\u652f\u6301sacn\u6307\u5b9a\u6570\u91cf\u7684key\u540e\u53d8\u4f11\u77201\u79d2\n    args = parser.parse_args()\n    port = args.port\n    host = args.host\n    password = args.password\n    scannum_thensleep = int(args.scannum_thensleep)\n\n    if args.db_list == &#039;all&#039;:\n        db_list = [i for i in xrange(0, 16)]\n    else:\n        db_list = [int(i) for i in args.db_list.split(&#039;,&#039;)]\n    db_list = list(set(db_list)) # 20201205\uff0c\u53bb\u91cd\uff0c\u907f\u514d\u7528\u6237\u91cd\u590d\u8f93\u5165db\u5e8f\u53f7\uff1b\n\n    for index in db_list:\n        try:\n            pool = redis.ConnectionPool(host=host, port=port, db=index, password=password) # 20201205\uff0c\u652f\u6301\u4f20\u5165Redis\u5bc6\u7801\n            r = redis.StrictRedis(connection_pool=pool)\n        except redis.exceptions.ConnectionError as e:\n            print e\n        else:\n            no_ttl_keys_file = &quot;.\/{port}_{db}_no_ttl_keys.txt&quot;.format(port=port, db=index)\n            check_ttl(r, no_ttl_keys_file, index, scannum_thensleep)\n\nif __name__ == &#039;__main__&#039;:\n    main()<\/code><\/pre>\n<h2>\u5b89\u5168\u5220\u9664Redis\u4e2d\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684key<\/h2>\n<p>\u5229\u7528\u524d\u9762\u63d0\u5230\u76842\u79cd\u65b9\u5f0f\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u62ff\u5230\u6ca1\u6709\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u7684 key \u4e86\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u6570\u636e\u5206\u6790\uff0c\u660e\u786e\u54ea\u4e9b key \u662f\u9489\u5b50\u6237\uff0c\u7136\u540e\u5e72\u6389\u5b83\u3002<\/p>\n<pre><code class=\"language-bash\"># deletedata.txt \u6570\u636e\u793a\u4f8b\uff1a\n\ndel key-0\ndel key-1\ndel key-2\ndel key-3\n...<\/code><\/pre>\n<p>\u8003\u8651\u5230\u5927key\uff0c\u66f4\u63a8\u8350\u4f7f\u7528<code>UNLINK<\/code>\uff1a<\/p>\n<ul>\n<li>DEL\uff1a\u963b\u585e\u5f0f\u64cd\u4f5c<\/li>\n<li>UNLINK\uff1a\u5e76\u4e0d\u603b\u662f \u963b\u585e\uff0c\u5982\u679c\u503c\u5f88\u5c0f\uff0c\u548c DEL \u76f8\u540c\u6548\u679c\u5c06\u76f4\u63a5\u5220\u9664\uff0c\u5982\u679c\u503c\u8f83\u5927\uff0ckey\u5c06\u88ab\u653e\u5165\u5217\u8868\u4e2d\uff0c\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u91ca\u653e<\/li>\n<\/ul>\n<pre><code class=\"language-bash\"># deletedata.txt \u6570\u636e\u793a\u4f8b\uff1a\n\nUNLINK key-0\nUNLINK key-1\nUNLINK key-2\nUNLINK key-3\n...<\/code><\/pre>\n<p>\u7531\u4e8e UNLINK \u652f\u6301\u591a\u53c2\u6570\uff08DEL \u4e5f\u652f\u6301\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u8fd8\u53ef\u4ee5\u505a\u8fdb\u4e00\u6b65\u4f18\u5316\uff1a<\/p>\n<pre><code class=\"language-bash\"># deletedata.txt \u6570\u636e\u793a\u4f8b\uff1a\n\nUNLINK key-0 key-1 key-2 key-3 key-4 key-5\nUNLINK key-6 key-7 key-8 key-9 key-10 key-11\n...<\/code><\/pre>\n<p>\u6700\u540e\u6211\u4eec\u518d\u4f7f\u7528\u7ba1\u9053\u5220\u9664\u5373\u53ef\uff0c\u5982\u679c\u6570\u636e\u91cf\u5b9e\u5728\u8f83\u5927\uff0c\u5efa\u8bae\u5206\u6279\u5220\u9664<\/p>\n<pre><code class=\"language-bash\"># \u4f7f\u7528\u7ba1\u9053\u5220\u9664\n\ncat deletedata.txt | redis-cli -c --pipe<\/code><\/pre>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/zxiaofan.blog.csdn.net\/article\/details\/110739644\">https:\/\/zxiaofan.blog.csdn.net\/article\/details\/110739644<\/a><br \/>\n\u6700\u65b0\u811a\u672c\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/zxiaofan\/OpenSource_Study\/tree\/master\/redis_scripts\">https:\/\/github.com\/zxiaofan\/OpenSource_Study\/tree\/master\/redis_scripts<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u524d\u6bb5\u65f6\u95f4\u516c\u53f8\u6709\u65b0\u4e1a\u52a1\u9700\u8981\u4f7f\u7528Redis\uff0c\u4e8e\u662f\u67e5\u770b\u4e86\u751f\u4ea7\u4e00Redis\u96c6\u7fa4\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u7528\u4e8e\u8bc4\u4f30\u662f\u5426\u80fd\u76f4\u63a5\u63a5\u5165 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1991","post","type-post","status-publish","format-standard","hentry","category-redis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1991"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1991\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}