{"id":201283,"date":"2025-05-10T08:34:17","date_gmt":"2025-05-10T00:34:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201283\/"},"modified":"2025-05-10T08:34:17","modified_gmt":"2025-05-10T00:34:17","slug":"redis%e5%bf%ab%e7%85%a7%e6%a8%a1%e5%bc%8f_%e5%8a%a8%e5%8a%9b%e8%8a%82%e7%82%b9java%e5%ad%a6%e9%99%a2%e6%95%b4%e7%90%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201283\/","title":{"rendered":"redis\u5feb\u7167\u6a21\u5f0f_\u52a8\u529b\u8282\u70b9Java\u5b66\u9662\u6574\u7406"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u5feb\u7167\u6a21\u5f0f_\u52a8\u529b\u8282\u70b9Java\u5b66\u9662\u6574\u7406<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aredis\u5feb\u7167\u6a21\u5f0f_\u52a8\u529b\u8282\u70b9Java\u5b66\u9662\u6574\u7406\u300b\uff0c\u804a\u804aredis\u5feb\u7167\u6a21\u5f0f\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053redis\u662f\u5e26\u6709\u6301\u4e45\u5316\u8fd9\u4e2a\u80fd\u529b\u4e86\uff0c\u90a3\u5230\u5e95\u6301\u4e45\u5316\u6210\u5230\u54ea\u91cc\uff0c\u6301\u4e45\u5316\u6210\u5565\u6837\u5462\uff1f\uff1f\uff1f\u8fd9\u7bc7\u6211\u4eec\u4e00\u8d77\u6765\u5bfb\u6c42\u7b54\u6848\u3002<\/p>\n<p><strong>\u4e00\u3001\u5feb\u7167\u6a21\u5f0f<\/strong><\/p>\n<p>\u3000\u3000\u6216\u8bb8\u5728\u7528Redis\u4e4b\u521d\u7684\u65f6\u5019\uff0c\u5c31\u542c\u8bf4\u8fc7redis\u6709\u4e24\u79cd\u6301\u4e45\u5316\u6a21\u5f0f\uff0c\u7b2c\u4e00\u79cd\u662fSNAPSHOTTING\u6a21\u5f0f\uff0c\u8fd8\u662f\u4e00\u79cd\u662fAOF\u6a21\u5f0f\uff0c\u800c\u4e14\u5728\u5b9e\u6218\u573a\u666f\u4e0b\u7528\u7684\u6700\u591a\u7684\u83ab\u8fc7\u4e8eSNAPSHOTTING\u6a21\u5f0f\uff0c\u8fd9\u4e2a\u4e0d\u9700\u8981\u53cd\u9a73\u5427\uff0c\u800c\u4e14\u4f60\u53ef\u80fd\u8fd8\u77e5\u9053\uff0c\u4f7f\u7528SNAPSHOTTING\u6a21\u5f0f\uff0c\u9700\u8981\u5728redis.conf\u4e2d\u8bbe\u7f6e\u914d\u7f6e\u53c2\u6570\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>\n# Save the DB on disk:\n#\n# save \n   \n    \n      # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all \"save\" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save \"\" save 900 1 save 300 10 save 60 10000 \n    \n   <\/pre>\n<p>\u4e0a\u9762\u4e09\u7ec4\u547d\u4ee4\u4e5f\u662f\u975e\u5e38\u597d\u7406\u89e3\u7684\uff0c\u5c31\u662f\u8bf4900\u6307\u7684\u662f\u201c\u79d2\u6570\u201d\uff0c1\u6307\u7684\u662f\u201cchange\u6b21\u6570\u201d\uff0c\u63a5\u4e0b\u6765\u5982\u679c\u5728\u201c900s\u201c\u5185\u67091\u6b21\u66f4\u6539\uff0c\u90a3\u4e48\u5c31\u6267\u884csave\u4fdd\u5b58\uff0c\u540c\u6837\u7684\u9053\u7406\uff0c\u5982\u679c300s\u5185\u670910\u6b21change\uff0c60s\u5185\u67091w\u6b21change\uff0c\u90a3\u4e48\u4e5f\u4f1a\u6267\u884csave\u64cd\u4f5c\uff0c\u5c31\u8fd9\u4e48\u7b80\u5355\uff0c\u770b\u4e86\u6211\u521a\u624d\u8bf4\u4e86\u8fd9\u4e48\u51e0\u53e5\u8bdd\uff0c\u662f\u4e0d\u662f\u6709\u79cd\u76f4\u89c9\u5728<\/p>\n<p>\u544a\u8bc9\u4f60\uff0c\u6709\u4e24\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u8981\u6f84\u6e05\u4e00\u4e0b\uff1a<\/p>\n<p>1. \u4e0a\u9762\u8fd9\u4e2a\u64cd\u4f5c\u5e94\u8be5\u662fredis\u81ea\u8eab\u8fdb\u884c\u7684\u540c\u6b65\u64cd\u4f5c\uff0c\u8bf7\u95ee\u662f\u5426\u53ef\u4ee5\u624b\u5de5\u6267\u884csave\u5462\uff1f\u3000<\/p>\n<p>\u5f53\u7136\u53ef\u4ee5\u8fdb\u884c\u624b\u5de5\u64cd\u4f5c\uff0credis\u63d0\u4f9b\u4e86\u4e24\u4e2a\u64cd\u4f5c\u547d\u4ee4\uff1asave\uff0cbgsave\uff0c\u8fd9\u4e24\u4e2a\u547d\u4ee4\u90fd\u4f1a\u5f3a\u5236\u5c06\u6570\u636e\u5237\u65b0\u5230\u786c\u76d8\u4e2d\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255554763b12c1b68ae1.png\" class=\"aligncenter\"><\/p>\n<p>2. \u770b\u4e0a\u9762\u7684\u56fe\uff0c\u8c8c\u4f3cbgsave\u662f\u5f00\u542f\u5355\u72ec\u7ebf\u7a0b\u7684\uff0c\u8bf7\u95ee\u662f\u5417\uff1f<\/p>\n<p>\u786e\u5b9e\u5982\u4f60\u6240\u8bf4\uff0cbgsave\u662f\u5f00\u542f\u6b21\u7ebf\u7a0b\u8fdb\u884c\u6570\u636e\u5237\u65b0\u7684\uff0c\u4e0d\u4fe1\u7684\u8bdd\u6211\u4eec\u6765\u770b\u770b\u4ee3\u7801\uff0c\u5b83\u7684\u4ee3\u7801\u662f\u5728rdb.c\u6e90\u6587\u4ef6\u4e2d\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255554763b12c1bb5982.jpg?2017728113454\" class=\"aligncenter\"><\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6709\u6ca1\u6709\u770b\u5230\u4e00\u4e2a\u91cd\u70b9\uff0c\u90a3\u5c31\u662ffork\u65b9\u6cd5\uff0c\u5b83\u5c31\u662f\u4e00\u4e9b\u725b\u4eba\u53e3\u4e2d\u8bf4\u7684\u4ec0\u4e48fork\u51fa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4eca\u5929\u4f60\u4e5f\u7b97\u7ec8\u4e8e\u770b\u5230\u4e86\uff0c\u5176\u5b9eredis\u5e76\u4e0d\u662f\u5355\u7eaf\u7684\u5355\u7ebf\u7a0b\u670d\u52a1\uff0c\u81f3\u5c11fork\u544a\u8bc9\u6211\u4eec\uff0c\u5b83\u5728\u4e00\u4e9b\u573a\u666f\u4e0b\u4e5f\u662f\u4f1a\u5f00\u542f\u5de5\u4f5c\u7ebf\u7a0b\u7684\uff0c\u7136\u540e\u53ef\u4ee5\u770b\u5230\u4ee3\u7801\u4f1a\u5728\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u6267\u884c\u540c\u6b65\u7684bgsave\u64cd\u4f5c\uff0c\u5c31\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<p>3. \u80fd\u7b80\u5355\u8bf4\u4e0bsaveparams\u53c2\u6570\u5728redis\u6e90\u7801\u4e2d\u7684\u903b\u8f91\u5417\uff1f<\/p>\n<p>\u53ef\u4ee5\u7684\uff0c\u5176\u5b9e\u5728redis\u4e2d\u6709\u4e00\u4e2a\u5468\u671f\u6027\u51fd\u6570\uff0c\u53eb\u505aserverCron\uff0c\u5b83\u4f1a\u5468\u671f\u6027\u542f\u52a8\uff0c\u5927\u6982\u4f1a\u505a\u4e03\u4ef6\u4e8b\u60c5\uff0c\u5982redis\u6ce8\u91ca\u6240\u8bf4\uff1a<\/p>\n<pre>\n\/* This is our timer interrupt, called server.hz times per second.\n * Here is where we do a number of things that need to be done asynchronously.\n * For instance:\n *\n * - Active expired keys collection (it is also performed in a lazy way on\n * lookup).\n * - Software watchdog.\n * - Update some statistic.\n * - Incremental rehashing of the DBs hash tables.\n * - Triggering BGSAVE \/ AOF rewrite, and handling of terminated children.\n * - Clients timeout of different kinds.\n * - Replication reconnection.\n * - Many more...\n *\n * Everything directly called here will be called server.hz times per second,\n * so in order to throttle execution of things we want to do less frequently\n * a macro is used: run_with_period(milliseconds) { .... }\n *\/\n\nint serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {\n\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7ea2\u8272\u5b57\u4f53\u5c31\u662f\u505a\u4e86\u6211\u4eec\u6240\u5173\u5fc3\u7684save\u64cd\u4f5c\uff0c\u770b\u8fc7\u65b9\u6cd5\u7684\u6ce8\u91ca\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u627e\u4e00\u4e0b\u5177\u4f53\u903b\u8f91\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255554863b12c1c09d53.jpg?2017728113518\" class=\"aligncenter\"><\/p>\n<p>\u4ece\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u903b\u8f91\uff0c\u4f60\u5e94\u8be5\u53ef\u4ee5\u53d1\u73b0\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>.saveparams\u53c2\u6570\u662f\u5728server\u5bf9\u8c61\u4e0b\u9762\uff0c\u800cserver\u5bf9\u8c61\u6b63\u597d\u662fredisServer\u7c7b\u578b\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255554863b12c1c70c22.jpg?2017728113541\" class=\"aligncenter\"><\/p>\n<p>\u4ece\u4e0a\u9762\u56fe\u4e2d *saveparams \u7684\u6ce8\u91ca\u4e0a\u6765\u770b\uff0c\u4f60\u5e94\u8be5\u77e5\u9053*saveparams\u662fsaveparam\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u90a3\u73b0\u5728\u662f\u4e0d\u662f\u6709\u5f3a\u70c8\u7684\u597d\u5947\u5fc3\u60f3\u770b\u4e00\u4e0bsaveparam<\/p>\n<p>\u7c7b\u578b\u662f\u600e\u4e48\u5b9a\u4e49\u7684\u7684\u5462\uff1f\uff1f\uff1f \u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167255554863b12c1cb1e57.jpg\" class=\"aligncenter\"><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0csaveparam\u53c2\u6570\u91cc\u9762\u6709\u4e24\u4e2a\u53c2\u6570\uff0cseconds\u5c31\u662f\u4fdd\u5b58\u79d2\u6570\uff0cchanges\u5c31\u662f\u6539\u53d8\u91cf\uff0c\u800c\u8fd9\u4e8c\u4e2a\u53c2\u6570\u5c31\u5bf9\u5e94\u7740\u6211\u4eec\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684900 0 \u8fd9\u6837\u7684\u914d\u7f6e\u3002<\/p>\n<p> \u7136\u540e\u6211\u4eec\u901a\u8fc7if\u53d1\u73b0\uff0c\u5982\u679c\u7ec8\u6ee1\u8db3\uff0c\u5c31\u4f1a\u6700\u7ec8\u8c03\u7528rdbSaveBackground\u6765\u6301\u4e45\u5316\u6211\u4eec\u7684rdb\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u5feb\u7167\u6a21\u5f0f_\u52a8\u529b\u8282\u70b9Ja&#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-201283","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201283","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=201283"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201283\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}