{"id":65,"date":"2026-07-31T09:00:00","date_gmt":"2026-07-31T01:00:00","guid":{"rendered":"https:\/\/fsdata.site\/?p=65"},"modified":"2026-07-24T00:36:49","modified_gmt":"2026-07-23T16:36:49","slug":"redis-%e7%bc%93%e5%ad%98%e7%a9%bf%e9%80%8f-%e5%87%bb%e7%a9%bf-%e9%9b%aa%e5%b4%a9%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88%ef%bc%88%e9%99%84%e4%bb%a3%e7%a0%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fsdata.site\/?p=65","title":{"rendered":"Redis \u7f13\u5b58\u7a7f\u900f\/\u51fb\u7a7f\/\u96ea\u5d29\u89e3\u51b3\u65b9\u6848\uff08\u9644\u4ee3\u7801\uff09"},"content":{"rendered":"<h2>\u4e00\u3001\u7f13\u5b58\u662f\u628a\u53cc\u5203\u5251<\/h2>\n<p>Redis \u7528\u5f97\u597d\uff0c\u7cfb\u7edf\u8d77\u98de\uff1b\u7528\u4e0d\u597d\uff0c\u534a\u591c\u6551\u706b\u3002\u672c\u6587\u628a\u7f13\u5b58\u6700\u5e38\u89c1\u7684\u4e09\u5927\u95ee\u9898\u2014\u2014\u7a7f\u900f\u3001\u51fb\u7a7f\u3001\u96ea\u5d29\u2014\u2014\u8bb2\u900f\uff0c\u5e76\u7ed9\u51fa\u53ef\u76f4\u63a5\u843d\u5730\u7684\u4ee3\u7801\u65b9\u6848\u3002<\/p>\n<h2>\u4e8c\u3001\u7f13\u5b58\u7a7f\u900f\uff1a\u67e5\u4e0d\u5b58\u5728\u7684\u6570\u636e<\/h2>\n<p><strong>\u73b0\u8c61<\/strong>\uff1a\u8bf7\u6c42\u4e00\u4e2a\u6570\u636e\u5e93\u548c\u7f13\u5b58\u90fd\u6ca1\u6709\u7684 key\uff0c\u6bcf\u6b21\u90fd\u6253\u5230 DB\u3002<br \/>\n<strong>\u65b9\u6848 1\uff1a\u7f13\u5b58\u7a7a\u503c<\/strong><\/p>\n<pre><code>String val = redis.get(key);\nif (val == null) {\n    val = db.query(key);\n    if (val == null) {\n        redis.setex(key, 300, \"\"); \/\/ \u7a7a\u503c\u7f13\u5b58 5 \u5206\u949f\n    }\n}\n<\/code><\/pre>\n<p><strong>\u65b9\u6848 2\uff1a\u5e03\u9686\u8fc7\u6ee4\u5668<\/strong>\uff1a\u5728\u7f13\u5b58\u524d\u52a0\u4e00\u5c42 BloomFilter\uff0c\u4e0d\u5b58\u5728\u7684 key \u76f4\u63a5\u62e6\u622a\u3002<\/p>\n<h2>\u4e09\u3001\u7f13\u5b58\u51fb\u7a7f\uff1a\u70ed\u70b9 key \u8fc7\u671f\u77ac\u95f4<\/h2>\n<p><strong>\u73b0\u8c61<\/strong>\uff1a\u67d0\u4e2a\u7206\u6b3e key \u7a81\u7136\u5931\u6548\uff0c\u6d77\u91cf\u8bf7\u6c42\u540c\u65f6\u6253\u5230 DB\u3002<br \/>\n<strong>\u65b9\u6848\uff1a\u4e92\u65a5\u9501\uff08\u5206\u5e03\u5f0f\u9501\uff09<\/strong><\/p>\n<pre><code>String val = redis.get(key);\nif (val == null) {\n    if (redis.setnx(\"lock:\"+key, \"1\", 10)) { \/\/ \u62ff\u5230\u9501\n        val = db.query(key);\n        redis.setex(key, 3600, val);\n        redis.del(\"lock:\"+key);\n    } else {\n        Thread.sleep(50); \/\/ \u6ca1\u62ff\u5230\u9501\uff0c\u7a0d\u7b49\u91cd\u8bd5\n        return getFromCache(key);\n    }\n}\n<\/code><\/pre>\n<h2>\u56db\u3001\u7f13\u5b58\u96ea\u5d29\uff1a\u5927\u91cf key \u540c\u65f6\u5931\u6548<\/h2>\n<p><strong>\u73b0\u8c61<\/strong>\uff1a\u6279\u91cf key \u5728\u540c\u4e00\u65f6\u523b\u8fc7\u671f\uff0cDB \u88ab\u77ac\u95f4\u538b\u57ae\u3002<br \/>\n<strong>\u65b9\u6848\uff1a\u8fc7\u671f\u65f6\u95f4\u52a0\u968f\u673a\u6296\u52a8<\/strong><\/p>\n<pre><code>int base = 3600;\nint jitter = new Random().nextInt(600); \/\/ 0~10\u5206\u949f\u968f\u673a\nredis.setex(key, base + jitter, val);\n<\/code><\/pre>\n<p>\u53e6\u5916\uff1a\u6838\u5fc3\u6570\u636e\u53ef\u8bbe<strong>\u903b\u8f91\u8fc7\u671f<\/strong>\uff08\u4e0d\u771f\u8fc7\u671f\uff0c\u540e\u53f0\u5f02\u6b65\u5237\u65b0\uff09\u3002<\/p>\n<h2>\u4e94\u3001\u9632\u5751\u8865\u5145<\/h2>\n<ul>\n<li>\u7f13\u5b58\u4e0e\u6570\u636e\u5e93\u4e00\u81f4\u6027\uff1a\u5199\u65f6\u5148\u66f4\u65b0 DB \u518d\u5220\u7f13\u5b58\uff08Cache Aside\uff09\uff0c\u6ce8\u610f\u91cd\u8bd5\u3002<\/li>\n<li>\u5927 key \u62c6\u5206\uff1a\u4e00\u4e2a hash \u5b58\u4e07\u7ea7\u5b57\u6bb5\u4f1a\u963b\u585e\uff0c\u62c6\u6210\u591a\u4e2a\u3002<\/li>\n<li>\u907f\u514d\u70ed key \u96c6\u4e2d\uff1aRedis Cluster \u5206\u7247 + \u672c\u5730\u7f13\u5b58\u4e8c\u7ea7\u515c\u5e95\u3002<\/li>\n<\/ul>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u4e09\u5927\u5751\u8bb0\u7262&#8221;\u7a7a\u503c\u3001\u52a0\u9501\u3001\u6296\u52a8&#8221;\u516d\u5b57\u8bc0\uff0c\u518d\u914d\u5408\u76d1\u63a7\uff0cRedis \u5c31\u80fd\u7a33\u7a33\u625b\u4f4f\u9ad8\u5e76\u53d1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bb2\u900f\u7f13\u5b58\u4e09\u5927\u5751\uff1a\u7a7f\u900f\u7528\u7a7a\u503c+\u5e03\u9686\u3001\u51fb\u7a7f\u7528\u4e92\u65a5\u9501\u3001\u96ea\u5d29\u7528\u8fc7\u671f\u6296\u52a8\uff0c\u9644\u53ef\u76f4\u63a5\u843d\u5730\u7684 Java \u4ee3\u7801\u3002<\/p>\n","protected":false},"author":2,"featured_media":156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=65"}],"version-history":[{"count":1,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/65\/revisions\/152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/media\/156"}],"wp:attachment":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}