{"id":201190,"date":"2025-05-10T08:02:14","date_gmt":"2025-05-10T00:02:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201190\/"},"modified":"2025-05-10T08:02:14","modified_gmt":"2025-05-10T00:02:14","slug":"redis%e5%88%a0%e9%99%a4%e7%ad%96%e7%95%a5%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e6%b3%95%e5%8f%8a%e9%80%90%e5%87%ba%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201190\/","title":{"rendered":"Redis\u5220\u9664\u7b56\u7565\u7684\u4e09\u79cd\u65b9\u6cd5\u53ca\u9010\u51fa\u7b97\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5220\u9664\u7b56\u7565\u7684\u4e09\u79cd\u65b9\u6cd5\u53ca\u9010\u51fa\u7b97\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cgolang\u5b66\u4e60\u7f51\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aRedis\u5220\u9664\u7b56\u7565\u7684\u4e09\u79cd\u65b9\u6cd5\u53ca\u9010\u51fa\u7b97\u6cd5\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\u7b97\u6cd5\u3001\u7b56\u7565\u3001Redis\u5220\u9664\u3001\u9010\u51fa\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<h2>\u4e00\u3001\u524d\u8a00<\/h2>\n<p>\u5728\u6587\u7ae0\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u5148\u95ee\u5927\u5bb6\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u6211\u4eec\u4f7f\u7528\u6307\u4ee4\uff1a<code>expire key second<\/code>\u7ed9\u4e00\u4e2akey\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u8fc7\u671f\u65f6\u95f4\u4e00\u5230\uff0c\u8fd9\u4e2akey\u5bf9\u5e94\u7684<code>\u8fc7\u671f\u6570\u636e<\/code>\u771f\u7684\u88ab\u670d\u52a1\u5668\u7acb\u5373\u5220\u9664\u4e86\u5417\uff1f\u7b54\u6848\u662f\u5e76\u4e0d\u4f1a\u7acb\u5373\u5220\u9664\u3002\u77e5\u9053\u4e86\u8fd9\u4e2a\u7b54\u6848\uff0c\u5c31\u6765\u770b\u770bRedis\u4e2d\u5982\u4f55\u5904\u7406\u8fc7\u671f\u7684\u6570\u636e\u3002<\/p>\n<h2>\u4e8c\u3001Redis\u4e2d\u7684\u6570\u636e\u7279\u5f81<\/h2>\n<p>Redis\u662f\u4e00\u79cd\u5185\u5b58\u7ea7\u6570\u636e\u5e93\uff0c\u6240\u6709\u6570\u636e\u5747\u5b58\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u5185\u5b58\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7TTL\u6307\u4ee4\u83b7\u53d6\u5176\u72b6\u6001\uff0c<strong>\u6709\u4e09\u79cd\u72b6\u6001\uff1a&nbsp;<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<th>\u6307\u4ee4\u7ed3\u679c<\/th>\n<th>\u72b6\u6001<\/th>\n<\/tr>\n<tr>\n<td>XX<\/td>\n<td>\u5177\u6709\u65f6\u6548\u6027\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>-1<\/td>\n<td>\u6c38\u4e45\u6709\u6548\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>-2<\/td>\n<td>\u5df2\u7ecf\u8fc7\u671f\u7684\u6570\u636e&nbsp;\u6216 \u88ab\u5220\u9664\u7684\u6570\u636e \u6216 \u672a\u5b9a\u4e49\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e09\u3001\u65f6\u6548\u6027\u6570\u636e\u50a8\u5b58\u7ed3\u6784<\/h2>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651063afa93ea0d81.png\" class=\"aligncenter\"><\/p>\n<p>\u5f53\u6211\u4eec\u7528\u6307\u4ee4\u8bbe\u7f6e\u8fc7\u671f\u6570\u636e\u540e\uff0c\u6570\u636e\u5bf9\u5e94\u7684\u5730\u5740\u4f1a\u653e\u5728expires\u7a7a\u95f4\u4e2d\uff0c\u5b58\u50a8\u65b9\u5f0f\u662f\u54c8\u5e0c\uff0c\u5b58\u50a8\u7684value\u662f\u8fc7\u671f\u65f6\u95f4\u3002<\/p>\n<h2>\u56db\u3001\u6570\u636e\u5220\u9664\u7b56\u7565<\/h2>\n<p><code>\u6570\u636e\u5220\u9664\u7b56\u7565\u76ee\u6807\uff1a<\/code>\u5728\u5185\u5b58\u5360\u7528\u4e0eCPU\u5360\u7528\u4e4b\u95f4\u5bfb\u627e\u4e00\u79cd\u5e73\u8861\uff0c\u987e\u6b64\u5931\u5f7c\u90fd\u4f1a\u9020\u6210\u6574\u4f53redis\u6027\u80fd\u7684\u4e0b\u964d\uff0c\u751a\u81f3\u5f15\u53d1\u670d\u52a1\u5668\u5b95\u673a\u6216\u5185\u5b58\u6cc4\u9732<\/p>\n<h3>1\u3001\u5b9a\u65f6\u5220\u9664<\/h3>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u5f53key\u8bbe\u7f6e\u6709\u8fc7\u671f\u65f6\u95f4\uff0c\u4e14\u8fc7\u671f\u65f6\u95f4\u5230\u8fbe\u65f6\uff0c\u7531\u5b9a\u65f6\u5668\u4efb\u52a1\u7acb\u5373\u6267\u884c\u5bf9\u952e\u7684\u5220\u9664\u64cd\u4f5c<\/li>\n<li><strong>\u4f18\u70b9\uff1a<\/strong>\u8282\u7ea6\u5185\u5b58\uff0c\u5230\u65f6\u5c31\u5220\u9664\uff0c\u5feb\u901f\u91ca\u653e\u6389\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5360\u7528<\/li>\n<li><strong>\u7f3a\u70b9\uff1a<\/strong>CPU\u538b\u529b\u5f88\u5927\uff0c\u65e0\u8bbaCPU\u6b64\u65f6\u8d1f\u8f7d\u91cf\u591a\u9ad8\uff0c\u5747\u5360\u7528CPU\uff0c\u4f1a\u5f71\u54cdredis\u670d\u52a1\u5668\u54cd\u5e94\u65f6\u95f4\u548c\u6307\u4ee4\u541e\u5410\u91cf<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651063afa93ecafaa.png\" class=\"aligncenter\"><\/p>\n<h3>2\u3001\u60f0\u6027\u5220\u9664<\/h3>\n<ul>\n<li>\u6570\u636e\u5230\u8fbe\u8fc7\u671f\u65f6\u95f4\uff0c\u4e0d\u505a\u5904\u7406\u3002\u7b49\u4e0b\u6b21\u8bbf\u95ee\u8be5\u6570\u636e\u65f6\uff0c\u4f1a\u8c03\u7528<code>expireIfNeeded()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u8be5\u6570\u636e\u662f\u5426\u8fc7\u671f\uff1a \u5982\u679c\u672a\u8fc7\u671f\uff0c\u8fd4\u56de\u6570\u636e\uff0c\u53d1\u73b0\u5df2\u8fc7\u671f\uff0c\u5220\u9664\uff0c\u8fd4\u56de\u4e0d\u5b58\u5728<\/li>\n<li><strong>\u4f18\u70b9\uff1a<\/strong>\u8282\u7ea6CPU\u6027\u80fd\uff0c\u53d1\u73b0\u5fc5\u987b\u5220\u9664\u7684\u65f6\u5019\u624d\u5220\u9664<\/li>\n<li><strong>\u7f3a\u70b9\uff1a<\/strong>\u5185\u5b58\u538b\u529b\u5f88\u5927\uff0c\u51fa\u73b0\u957f\u671f\u5360\u7528\u5185\u5b58\u7684\u6570\u636e<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651163afa93f39bcd.png\" class=\"aligncenter\"><\/p>\n<h3>3\u3001\u5b9a\u671f\u5220\u9664<\/h3>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651163afa93fadd96.png\" class=\"aligncenter\"><\/p>\n<p><strong>\u6d41\u7a0b\u5206\u6790\uff1a<\/strong><\/p>\n<ul>\n<li>Redis\u670d\u52a1\u5668\u542f\u52a8\u521d\u59cb\u5316\u65f6\uff0c\u8bfb\u53d6\u914d\u7f6eserver.hz\u7684\u503c\uff0c\u9ed8\u8ba4\u4e3a10<\/li>\n<li>\u7136\u540e\u6bcf\u79d2\u949f\u6267\u884c10\u6b21<code>serverCron()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u7528\u6765\u68c0\u6d4b\u670d\u52a1\u5668<\/li>\n<li><code>serverCron()<\/code>\u65b9\u6cd5\u53c8\u4f1a\u8c03\u7528<code>databasesCron()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u662f\u7528\u6765\u904d\u5386\u6570\u636e\u5e93\u7684\uff0credis\u9ed8\u8ba4\u670916\u4e2a\u6570\u636e\u5e93\uff0c\u4ece\u7b2c\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u59cb<\/li>\n<li><code>databasesCron()<\/code>\u65b9\u6cd5\u53c8\u4f1a\u8c03\u7528<code>activeExpireCycle()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u5bf9\u6bcf\u4e00\u4e2a<code>expires[*]<\/code>\u9010\u4e00\u8fdb\u884c\u68c0\u6d4b\uff0c\u6bcf\u6b21\u6267\u884c250ms\/server.hz\uff1b\u5bf9\u67d0\u4e2a<code>expires[*]<\/code>\u68c0\u6d4b\u65f6\uff0c\u968f\u673a\u6311\u9009W\u4e2akey\u68c0\u6d4b<\/li>\n<li>\u5982\u679c\u68c0\u6d4b\u5230key\u8d85\u65f6\uff0c\u5219\u5220\u9664key\uff1b\u5982\u679c\u4e00\u8f6e\u4e2d\u5220\u9664\u7684key\u7684\u6570\u91cf&gt;W25%\uff0c\u5faa\u73af\u8be5\u8fc7\u7a0b\uff1b\u5982\u679c\u4e00\u8f6e\u4e2d\u5220\u9664\u7684key\u7684\u6570\u91cf\u2264W25%\uff0c\u68c0\u67e5\u4e0b\u4e00\u4e2a<code>expires[*]<\/code>\uff0c0-15\u5faa\u73af\u3002\u5176\u4e2dW\u53d6\u503c=ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP\u5c5e\u6027\u503c<\/li>\n<li>\u5982\u679cactiveExpireCycle()\u6267\u884c\u65f6\u95f4\u5230\u671f\uff0c\u4e0b\u6b21\u4ececurrent_db\u7ee7\u7eed\u5411\u4e0b\u6267\u884c<\/li>\n<li>\u53c2\u6570current_db\u7528\u4e8e\u8bb0\u5f55activeExpireCycle() \u8fdb\u5165\u54ea\u4e2aexpires[*] \u6267\u884c<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651263afa940b87bc.png\" class=\"aligncenter\"><\/p>\n<ul>\n<li><code>\u603b\u7ed3\uff1a<\/code>\u5468\u671f\u6027\u8f6e\u8be2redis\u5e93\u4e2d\u7684\u65f6\u6548\u6027\u6570\u636e\uff0c\u91c7\u7528\u968f\u673a\u62bd\u53d6\u7684\u7b56\u7565\uff0c\u5229\u7528\u8fc7\u671f\u6570\u636e\u5360\u6bd4\u7684\u65b9\u5f0f\u63a7\u5236\u5220\u9664\u9891\u5ea6<\/li>\n<li>\u5185\u5b58\u538b\u529b\u4e0d\u662f\u5f88\u5927\uff0c\u957f\u671f\u5360\u7528\u5185\u5b58\u7684\u51b7\u6570\u636e\u4f1a\u88ab\u6301\u7eed\u6e05\u7406<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u5220\u9664\u7b56\u7565\u5bf9\u6bd4<\/h2>\n<table>\n<tbody>\n<tr>\n<th>\u5b9a\u65f6\u5220\u9664<\/th>\n<th>\u8282\u7ea6\u5185\u5b58\uff0c\u65e0\u5360\u7528<\/th>\n<th>\u4e0d\u5206\u65f6\u6bb5\u5360\u7528CPU\u8d44\u6e90\uff0c\u9891\u5ea6\u9ad8<\/th>\n<th>\u62ff\u65f6\u95f4\u6362\u7a7a\u95f4<\/th>\n<\/tr>\n<tr>\n<td>\u60f0\u6027\u5220\u9664<\/td>\n<td>\u5185\u5b58\u5360\u7528\u4e25\u91cd<\/td>\n<td>\u5ef6\u65f6\u6267\u884c\uff0cCPU\u5229\u7528\u7387\u9ad8<\/td>\n<td>\u62ff\u7a7a\u95f4\u6362\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\u5b9a\u671f\u5220\u9664<\/td>\n<td>\u5185\u5b58\u5b9a\u671f\u968f\u673a\u6e05\u7406<\/td>\n<td>\u6bcf\u79d2\u82b1\u8d39\u56fa\u5b9a\u7684CPU\u8d44\u6e90\u7ef4\u62a4\u5185\u5b58<\/td>\n<td>\u968f\u673a\u62bd\u67e5\uff0c\u91cd\u70b9\u62bd\u67e5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u516d\u3001\u9010\u51fa\u7b97\u6cd5<\/h2>\n<h3>1\u3001\u6982\u5ff5\u5f15\u5165<\/h3>\n<p>\u5f53\u65b0\u6570\u636e\u8fdb\u5165redis\u65f6\uff0c\u5982\u679c\u5185\u5b58\u4e0d\u8db3\u600e\u4e48\u529e\uff1fRedis\u4f7f\u7528\u5185\u5b58\u5b58\u50a8\u6570\u636e\uff0c\u5728\u6267\u884c\u6bcf\u4e00\u4e2a\u547d\u4ee4\u524d\uff0c\u4f1a\u8c03\u7528<code>freeMemoryIfNeeded()<\/code>\u68c0\u6d4b\u5185\u5b58\u662f\u5426\u5145\u8db3\u3002\u5982\u679c\u5185\u5b58\u4e0d\u6ee1\u8db3\u65b0\u52a0\u5165\u6570\u636e\u7684\u6700\u4f4e\u5b58\u50a8\u8981\u6c42\uff0credis\u8981\u4e34\u65f6\u5220\u9664\u4e00\u4e9b\u6570\u636e\u4e3a\u5f53\u524d\u6307\u4ee4\u6e05\u7406\u5b58\u50a8\u7a7a\u95f4\u3002\u6e05\u7406\u6570\u636e\u7684\u7b56\u7565\u79f0\u4e3a\u9010\u51fa\u7b97\u6cd5\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u9010\u51fa\u6570\u636e\u7684\u8fc7\u7a0b\u4e0d\u662f100%\u80fd\u591f\u6e05\u7406\u51fa\u8db3\u591f\u7684\u53ef\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5982\u679c\u4e0d\u6210\u529f\u5219\u53cd\u590d\u6267\u884c\u3002\u5f53\u5bf9\u6240\u6709\u6570\u636e\u5c1d\u8bd5\u5b8c\u6bd5\u540e\uff0c\u5982\u679c\u4e0d\u80fd\u8fbe\u5230\u5185\u5b58\u6e05\u7406\u7684\u8981\u6c42\uff0c\u5c06\u51fa\u73b0\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167245651363afa94121899.png\" class=\"aligncenter\"><\/p>\n<h3>2\u3001\u516b\u79cd\u914d\u7f6e<\/h3>\n<p>\u68c0\u6d4b\u6613\u5931\u6570\u636e\uff08<code>\u53ef\u80fd\u4f1a\u8fc7\u671f\u7684\u6570\u636e\u96c6server.db[i].expires<\/code> \uff09<\/p>\n<table>\n<tbody>\n<tr>\n<th>volatile-lru<\/th>\n<th>\u6311\u9009\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<\/th>\n<\/tr>\n<tr>\n<td>volatile-lfu<\/td>\n<td>\u6311\u9009\u6700\u8fd1\u4f7f\u7528\u6b21\u6570\u6700\u5c11\u7684\u6570\u636e\u6dd8\u6c70<\/td>\n<\/tr>\n<tr>\n<td>volatile-ttl<\/td>\n<td>\u6311\u9009\u5c06\u8981\u8fc7\u671f\u7684\u6570\u636e\u6dd8\u6c70<\/td>\n<\/tr>\n<tr>\n<td>volatile-random<\/td>\n<td>\u4efb\u610f\u9009\u62e9\u6570\u636e\u6dd8\u6c70<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u68c0\u6d4b\u5168\u5e93\u6570\u636e\uff08<code>\u6240\u6709\u6570\u636e\u96c6server.db[i].dict<\/code> \uff09<\/p>\n<table>\n<tbody>\n<tr>\n<th>allkeys-lru<\/th>\n<th>\u6311\u9009\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\u6dd8\u6c70<\/th>\n<\/tr>\n<tr>\n<td>allkeys-lfu<\/td>\n<td>\u6311\u9009\u6700\u8fd1\u4f7f\u7528\u6b21\u6570\u6700\u5c11\u7684\u6570\u636e\u6dd8\u6c70<\/td>\n<\/tr>\n<tr>\n<td>allkeys-random<\/td>\n<td>\u4efb\u610f\u9009\u62e9\u6570\u636e\u6dd8\u6c70<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u653e\u5f03\u6570\u636e\u9a71\u9010\uff1a<\/strong><\/p>\n<table>\n<tbody>\n<tr>\n<th>no-enviction<\/th>\n<th>\u7981\u6b62\u9a71\u9010\u6570\u636e\uff08redis4.0\u4e2d\u9ed8\u8ba4\u7b56\u7565\uff09<\/th>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5220\u9664\u7b56\u7565\u7684\u4e09\u79cd\u65b9\u6cd5\u53ca\u9010&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201190","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201190","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201190"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201190\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}