{"id":201669,"date":"2025-05-10T10:05:53","date_gmt":"2025-05-10T02:05:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201669\/"},"modified":"2025-05-10T10:05:53","modified_gmt":"2025-05-10T02:05:53","slug":"%e6%80%bb%e7%bb%93%e4%b8%80%e4%b8%8bredis%e7%9a%84%e7%bc%93%e5%ad%98%e9%9b%aa%e5%b4%a9%e3%80%81%e7%bc%93%e5%ad%98%e5%87%bb%e7%a9%bf%e3%80%81%e7%bc%93%e5%ad%98%e7%a9%bf%e9%80%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201669\/","title":{"rendered":"\u603b\u7ed3\u4e00\u4e0bRedis\u7684\u7f13\u5b58\u96ea\u5d29\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u603b\u7ed3\u4e00\u4e0bRedis\u7684\u7f13\u5b58\u96ea\u5d29\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;font-family:\">\u300a\u603b\u7ed3\u4e00\u4e0bRedis\u7684\u7f13\u5b58\u96ea\u5d29\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;font-family:\"><span style=\"color: #FF6600;font-family:\">Redis\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f<\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;font-family:\">\u6570\u636e\u5e93<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<h2><span style=\"color: #48b378\">\u7f13\u5b58\u51fb\u7a7f<\/span><\/h2>\n<p>\u7f13\u5b58\u51fb\u7a7f\u6307\u7684\u662f\u5728\u9ad8\u5e76\u53d1\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u7f13\u5b58\u7684key\u5728\u7f13\u5b58\u4e2d\u4e0d\u5b58\u5728\uff0c\u5bfc\u81f4\u6bcf\u6b21\u8bf7\u6c42\u90fd\u8981\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u4ece\u800c\u5bfc\u81f4\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\uff0c\u751a\u81f3\u5d29\u6e83\u3002\u8fd9\u79cd\u60c5\u51b5\u901a\u5e38\u53d1\u751f\u5728\u4e00\u4e9b\u70ed\u70b9\u6570\u636e\u4e0a\uff0c\u6bd4\u5982\u7528\u6237\u767b\u5f55\u4fe1\u606f\u7b49\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u539f\u56e0<\/span><\/h3>\n<p>\u7f13\u5b58\u51fb\u7a7f\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u5728\u67d0\u4e9b\u70ed\u70b9\u6570\u636e\u7684key\u5931\u6548\u6216\u8005\u88ab\u5220\u9664\u65f6\uff0c\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u540c\u65f6\u8bbf\u95ee\u8fd9\u4e2akey\uff0c\u5bfc\u81f4\u7f13\u5b58\u4e2d\u4e0d\u5b58\u5728\u8fd9\u4e2akey\u7684\u6570\u636e\uff0c\u4ece\u800c\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u9700\u8981\u53bb\u8bbf\u95ee\u6570\u636e\u5e93\u83b7\u53d6\u6570\u636e\uff0c\u9020\u6210\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u89e3\u51b3\u65b9\u6848<\/span><\/h3>\n<p>1.\u8bbe\u7f6e\u70ed\u70b9\u6570\u636e\u6c38\u4e0d\u8fc7\u671f<\/p>\n<p>\u5728\u7f13\u5b58\u4e2d\u8bbe\u7f6e\u70ed\u70b9\u6570\u636e\u6c38\u4e0d\u8fc7\u671f\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u7f13\u5b58\u51fb\u7a7f\u95ee\u9898\u3002\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5bfc\u81f4\u7f13\u5b58\u4e2d\u5b58\u5728\u5f88\u591a\u8fc7\u671f\u4f46\u662f\u5360\u7528\u5185\u5b58\u7684\u6570\u636e\uff0c\u56e0\u6b64\u9700\u8981\u5728\u8bbe\u7f6e\u7f13\u5b58\u6570\u636e\u65f6\u8fdb\u884c\u6743\u8861\u3002<\/p>\n<p>String key = &#8220;hot_data&#8221;;<br \/>String value = redis.get(key);<br \/>if (value == null) {<br \/> value = db.get(key);<br \/> if (value != null) {<br \/> redis.set(key, value);<br \/> redis.persist(key); \/\/\u8bbe\u7f6ekey\u6c38\u4e0d\u8fc7\u671f<br \/> }<br \/>}<\/p>\n<p>2.\u8bbe\u7f6e\u70ed\u70b9\u6570\u636e\u77ed\u671f\u8fc7\u671f<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u7f13\u5b58\u4e2d\u8fc7\u591a\u5360\u7528\u5185\u5b58\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5c06\u70ed\u70b9\u6570\u636e\u8bbe\u7f6e\u4e00\u4e2a\u76f8\u5bf9\u8f83\u77ed\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u6bd4\u59821\u5206\u949f\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u8fc7\u671f\u6570\u636e\u5360\u7528\u8fc7\u591a\u5185\u5b58\u3002\u5f53\u70ed\u70b9\u6570\u636e\u8fc7\u671f\u540e\uff0c\u53ef\u4ee5\u5728\u540e\u53f0\u5f02\u6b65\u66f4\u65b0\u7f13\u5b58\u6570\u636e\u3002<\/p>\n<p>String key = &#8220;hot_data&#8221;;<br \/>String value = redis.get(key);<br \/>if (value == null) {<br \/> \/\/\u6dfb\u52a0\u5206\u5e03\u5f0f\u9501\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f<br \/> if(redis.setNx(&#8220;lock_&#8221;+key,&#8221;value&#8221;)){<br \/> value = db.get(key);<br \/> if (value != null) {<br \/> redis.set(key, value);<br \/> redis.expire(key,60); \/\/\u8bbe\u7f6ekey\u8fc7\u671f\u65f6\u95f4\u4e3a1\u5206\u949f<br \/> }<br \/> redis.del(&#8220;lock_&#8221;+key);<br \/> }else {<br \/> Thread.sleep(50);<br \/> return queryDataFromCache(key);<br \/> }<br \/>}<\/p>\n<h2><span style=\"color: #48b378\">\u7f13\u5b58\u7a7f\u900f<\/span><\/h2>\n<p>\u7f13\u5b58\u7a7f\u900f\u6307\u7684\u662f\u5f53\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u540c\u65f6\u67e5\u8be2\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684key\u65f6\uff0c\u7531\u4e8e\u7f13\u5b58\u4e2d\u6ca1\u6709\u5bf9\u5e94\u7684\u6570\u636e\uff0c\u6240\u4ee5\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u4f1a\u53bb\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u539f\u56e0<\/span><\/h3>\n<p>\u7f13\u5b58\u7a7f\u900f\u7684\u539f\u56e0\u662f\u7531\u4e8e\u9ed1\u5ba2\u653b\u51fb\u6216\u8005\u6076\u610f\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5bf9\u67d0\u4e9b\u4e0d\u5b58\u5728\u7684\u6570\u636e\u8fdb\u884c\u5927\u91cf\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u5bfc\u81f4\u7f13\u5b58\u7a7f\u900f\u95ee\u9898\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u89e3\u51b3\u65b9\u6848<\/span><\/h3>\n<p>1.\u5bf9\u67e5\u8be2\u7ed3\u679c\u4e3a\u7a7a\u7684key\u8bbe\u7f6e\u7a7a\u503c<\/p>\n<p>\u5f53\u7f13\u5b58\u67e5\u8be2\u7684\u7ed3\u679c\u4e3a\u7a7a\u65f6\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u8bbe\u7f6e\u4e3a\u7a7a\u503c\u5199\u5165\u7f13\u5b58\uff0c\u8fd9\u6837\u4e0b\u6b21\u67e5\u8be2\u76f8\u540c\u7684key\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u907f\u514d\u4e86\u67e5\u8be2\u6570\u636e\u5e93\u7684\u5f00\u9500\u3002<\/p>\n<p>String key <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">&#8220;not_exist_data&#8221;<\/span><span>;<\/span><br \/>String value <span style=\"color: #d73a49\">=<\/span> redis<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/>if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">==<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u6dfb\u52a0\u5206\u5e03\u5f0f\u9501\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f<br \/> if<span style=\"color: #997\">(<\/span>redis<span style=\"color: #005cc5\">.setNx<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span>,<\/span><span style=\"color: #690\">&#8220;value&#8221;<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">{<\/span><br \/> value <span style=\"color: #d73a49\">=<\/span> db<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> value<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> <span style=\"color: #690\">&#8220;&#8221;<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u7a7a\u503c<br \/> redis<span style=\"color: #005cc5\">.expire<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> <span style=\"color: #005cc5\">60<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a1\u5206\u949f<br \/><span style=\"color: #997\">}<\/span><br \/> redis<span style=\"color: #005cc5\">.del<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/> Thread<span style=\"color: #005cc5\">.sleep<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #005cc5\">50<\/span><span style=\"color: #997\">)<\/span><span>;<\/span><br \/> return queryDataFromCache<span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><br \/><span style=\"color: #997\">}<\/span><\/p>\n<p>2.BloomFilter\u8fc7\u6ee4\u975e\u6cd5\u8bf7\u6c42<\/p>\n<p>\u4f7f\u7528BloomFilter\u53ef\u4ee5\u5bf9\u8bf7\u6c42\u53c2\u6570\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5c06\u975e\u6cd5\u8bf7\u6c42\u62e6\u622a\u5728\u7cfb\u7edf\u5916\u90e8\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5bf9\u7cfb\u7edf\u7684\u538b\u529b\u3002<\/p>\n<p>BloomFilter filter <span style=\"color: #d73a49\">=<\/span> new BloomFilter<span style=\"color: #997\">(<\/span><span style=\"color: #005cc5\">10000<\/span><span>,<\/span> <span style=\"color: #005cc5\">0.001<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u5e03\u9686\u8fc7\u6ee4\u5668<br \/>String key <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">&#8220;not_exist_data&#8221;<\/span><span>;<\/span><br \/>if<span style=\"color: #997\">(<\/span>filter<span style=\"color: #005cc5\">.mightContain<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">{<\/span><br \/> return <span style=\"color: #905\">null<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><br \/>String value <span style=\"color: #d73a49\">=<\/span> redis<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/>if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">==<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u6dfb\u52a0\u5206\u5e03\u5f0f\u9501\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f<br \/> if<span style=\"color: #997\">(<\/span>redis<span style=\"color: #005cc5\">.setNx<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span>,<\/span><span style=\"color: #690\">&#8220;value&#8221;<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">{<\/span><br \/> value <span style=\"color: #d73a49\">=<\/span> db<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> value<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/> filter<span style=\"color: #005cc5\">.put<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u5c06\u975e\u6cd5key\u52a0\u5165\u8fc7\u6ee4\u5668<br \/><span style=\"color: #997\">}<\/span><br \/> redis<span style=\"color: #005cc5\">.del<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/> Thread<span style=\"color: #005cc5\">.sleep<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #005cc5\">50<\/span><span style=\"color: #997\">)<\/span><span>;<\/span><br \/> return queryDataFromCache<span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><br \/><span style=\"color: #997\">}<\/span><\/p>\n<h2><span style=\"color: #48b378\">\u7f13\u5b58\u96ea\u5d29<\/span><\/h2>\n<p>\u7f13\u5b58\u96ea\u5d29\u6307\u7684\u662f\u5728\u7f13\u5b58\u4e2d\u5b58\u5728\u5927\u91cf\u7684key\u8fc7\u671f\u65f6\u95f4\u76f8\u540c\u6216\u8005\u5931\u6548\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u8fd9\u4e9bkey\u540c\u65f6\u5931\u6548\u65f6\uff0c\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u90fd\u4f1a\u6d8c\u5165\u6570\u636e\u5e93\uff0c\u5bfc\u81f4\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\uff0c\u751a\u81f3\u5d29\u6e83\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u539f\u56e0<\/span><\/h3>\n<p>\u7f13\u5b58\u96ea\u5d29\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u5728\u7f13\u5b58\u4e2d\u5b58\u5728\u5927\u91cf\u7684key\u540c\u65f6\u8fc7\u671f\uff0c\u5bfc\u81f4\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u540c\u65f6\u6d8c\u5165\u6570\u636e\u5e93\u3002<\/p>\n<h3><span style=\"color: #48b378\">\u89e3\u51b3\u65b9\u6848<\/span><\/h3>\n<p>1.\u7f13\u5b58\u6570\u636e\u968f\u673a\u8fc7\u671f\u65f6\u95f4 \u4e3a\u4e86\u907f\u514d\u7f13\u5b58\u4e2d\u5927\u91cfkey\u540c\u65f6\u8fc7\u671f\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6bcf\u4e2a\u7f13\u5b58\u6570\u636e\u7684\u8fc7\u671f\u65f6\u95f4\u4e0d\u540c\uff0c\u6bd4\u5982\u53ef\u4ee5\u5728\u539f\u6709\u8fc7\u671f\u65f6\u95f4\u7684\u57fa\u7840\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u968f\u673a\u65f6\u95f4\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5927\u91cfkey\u540c\u65f6\u8fc7\u671f\u7684\u60c5\u51b5\u3002<\/p>\n<p>String key <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">&#8220;hot_data&#8221;<\/span><span>;<\/span><br \/>String value <span style=\"color: #d73a49\">=<\/span> redis<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/>if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">==<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u6dfb\u52a0\u5206\u5e03\u5f0f\u9501\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f<br \/> if<span style=\"color: #997\">(<\/span>redis<span style=\"color: #005cc5\">.set<\/span><\/p>\n<p>2.\u7f13\u5b58\u6570\u636e\u9884\u52a0\u8f7d \u4e3a\u4e86\u907f\u514d\u5728\u7f13\u5b58\u4e2d\u5927\u91cf\u7684key\u5931\u6548\uff0c\u53ef\u4ee5\u5728\u7f13\u5b58\u6570\u636e\u8fc7\u671f\u4e4b\u524d\uff0c\u63d0\u524d\u5c06\u7f13\u5b58\u6570\u636e\u5237\u65b0\u5230\u7f13\u5b58\u4e2d\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u53ef\u7528\u6027\u3002<\/p>\n<p>String key <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">&#8220;hot_data&#8221;<\/span><span>;<\/span><br \/>String value <span style=\"color: #d73a49\">=<\/span> redis<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/>if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">==<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u6dfb\u52a0\u5206\u5e03\u5f0f\u9501\uff0c\u907f\u514d\u7f13\u5b58\u7a7f\u900f<br \/> if<span style=\"color: #997\">(<\/span>redis<span style=\"color: #005cc5\">.setNx<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span>,<\/span><span style=\"color: #690\">&#8220;value&#8221;<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">{<\/span><br \/> value <span style=\"color: #d73a49\">=<\/span> db<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> value<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> redis<span style=\"color: #005cc5\">.expire<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> <span style=\"color: #005cc5\">1800<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a30\u5206\u949f<br \/><span style=\"color: #997\">}<\/span><br \/> redis<span style=\"color: #005cc5\">.del<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #690\">&#8220;lock_&#8221;<\/span><span style=\"color: #d73a49\">+<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/> Thread<span style=\"color: #005cc5\">.sleep<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #005cc5\">50<\/span><span style=\"color: #997\">)<\/span><span>;<\/span><br \/> return queryDataFromCache<span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><br \/><span style=\"color: #997\">}<\/span>else <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u5224\u65ad\u7f13\u5b58\u662f\u5426\u9700\u8981\u5237\u65b0<br \/> if<span style=\"color: #997\">(<\/span>redis<span style=\"color: #005cc5\">.ttl<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span> <span style=\"color: #d73a49\"> <span style=\"color: #005cc5\">300<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #997\">{<\/span><br \/> new Thread<span style=\"color: #997\">(<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #d73a49\">-&gt;<\/span> <span style=\"color: #997\">{<\/span><br \/> String newValue <span style=\"color: #d73a49\">=<\/span> db<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> if <span style=\"color: #997\">(<\/span>newValue <span style=\"color: #d73a49\">!=<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> newValue<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> redis<span style=\"color: #005cc5\">.expire<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> <span style=\"color: #005cc5\">1800<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a30\u5206\u949f<br \/><span style=\"color: #997\">}<\/span><br \/><span style=\"color: #997\">}<\/span><span style=\"color: #997\">)<\/span><span style=\"color: #005cc5\">.start<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><br \/><span style=\"color: #997\">}<\/span><\/span><\/p>\n<p>3.\u9650\u6d41\u964d\u7ea7 \u5f53\u7f13\u5b58\u96ea\u5d29\u95ee\u9898\u51fa\u73b0\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u9650\u6d41\u964d\u7ea7\u7684\u65b9\u5f0f\u6765\u51cf\u5c11\u5bf9\u6570\u636e\u5e93\u7684\u8bf7\u6c42\uff0c\u4ece\u800c\u4fdd\u8bc1\u7cfb\u7edf\u7684\u53ef\u7528\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6eHystrix\u7b49\u9650\u6d41\u964d\u7ea7\u6846\u67b6\u6765\u5b9e\u73b0\u3002<\/p>\n<p>String key <span style=\"color: #d73a49\">=<\/span> <span style=\"color: #690\">&#8220;hot_data&#8221;<\/span><span>;<\/span><br \/>String value <span style=\"color: #d73a49\">=<\/span> redis<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/>if <span style=\"color: #997\">(<\/span>value <span style=\"color: #d73a49\">==<\/span> <span style=\"color: #905\">null<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #997\">{<\/span><br \/><span style=\"color: #d73a49\">\/\/<\/span>\u4f7f\u7528Hystrix\u8fdb\u884c\u9650\u6d41\u964d\u7ea7<br \/> value <span style=\"color: #d73a49\">=<\/span> HystrixCommand<span style=\"color: #005cc5\">.execute<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #997\">(<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #d73a49\">-&gt;<\/span> <span style=\"color: #997\">{<\/span><br \/> String data <span style=\"color: #d73a49\">=<\/span> db<span style=\"color: #005cc5\">.get<\/span><span style=\"color: #997\">(<\/span>key<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> redis<span style=\"color: #005cc5\">.set<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> data<span style=\"color: #997\">)<\/span><span>;<\/span><br \/> redis<span style=\"color: #005cc5\">.expire<\/span><span style=\"color: #997\">(<\/span>key<span>,<\/span> <span style=\"color: #005cc5\">1800<\/span><span style=\"color: #997\">)<\/span><span>;<\/span> <span style=\"color: #d73a49\">\/\/<\/span>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a30\u5206\u949f<br \/> return data<span>;<\/span><br \/><span style=\"color: #997\">}<\/span><span>,<\/span> <span style=\"color: #997\">(<\/span><span style=\"color: #997\">)<\/span> <span style=\"color: #d73a49\">-&gt;<\/span> <span style=\"color: #997\">{<\/span><br \/> return <span style=\"color: #690\">&#8220;\u7cfb\u7edf\u7e41\u5fd9\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff01&#8221;<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><span style=\"color: #997\">)<\/span><span>;<\/span><br \/><span style=\"color: #997\">}<\/span><\/p>\n<h2><span style=\"color: #48b378\">\u603b\u7ed3<\/span><\/h2>\n<p>Redis\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u53ef\u7528\u6027\u3002\u4f46\u662f\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f\u548c\u7f13\u5b58\u96ea\u5d29\u7b49\u95ee\u9898\uff0c\u91c7\u7528\u9002\u5f53\u7684\u89e3\u51b3\u65b9\u6848\u6765\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u7684\u53d1\u751f\uff0c\u4ece\u800c\u4fdd\u8bc1\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p><\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u603b\u7ed3\u4e00\u4e0bRedis\u7684\u7f13\u5b58\u96ea\u5d29\u3001\u7f13\u5b58\u51fb\u7a7f\u3001\u7f13\u5b58\u7a7f\u900f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u603b\u7ed3\u4e00\u4e0bRedis\u7684\u7f13\u5b58\u96ea\u5d29\u3001\u7f13&#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-201669","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201669","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=201669"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201669\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}