{"id":98350,"date":"2024-10-21T01:52:16","date_gmt":"2024-10-20T17:52:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/98350\/"},"modified":"2024-10-21T01:52:17","modified_gmt":"2024-10-20T17:52:17","slug":"redis-%e7%a9%bf%e9%80%8f%e9%ab%98%e6%95%88%e5%ae%89%e5%85%a8%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/98350\/","title":{"rendered":"Redis \u7a7f\u900f\u9ad8\u6548\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848"},"content":{"rendered":"<h1 id=\"redis-%e7%a9%bf%e9%80%8f%e9%ab%98%e6%95%88%e5%ae%89%e5%85%a8%e7%9a%84%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-ROxuyOyRIG\">Redis \u7a7f\u900f\u9ad8\u6548\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6578\u64da\u7684\u5feb\u901f\u5b58\u53d6\u548c\u9ad8\u6548\u8655\u7406\u662f\u4f01\u696d\u6210\u529f\u7684\u95dc\u9375\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\u4e2d\u3002\u7136\u800c\uff0c\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u589e\u9577\uff0cRedis \u7a7f\u900f\u554f\u984c\u9010\u6f38\u6210\u70ba\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0e Redis \u7a7f\u900f\u7684\u6982\u5ff5\u53ca\u5176\u9ad8\u6548\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis-%e7%a9%bf%e9%80%8f%ef%bc%9f-ROxuyOyRIG\">\u4ec0\u9ebc\u662f Redis \u7a7f\u900f\uff1f<\/h2>\n<p>Redis \u7a7f\u900f\u662f\u6307\u7576\u7528\u6236\u8acb\u6c42\u4e00\u500b\u4e0d\u5b58\u5728\u7684\u6578\u64da\u6642\uff0c\u8acb\u6c42\u6703\u76f4\u63a5\u7a7f\u900f\u5230\u5f8c\u7aef\u6578\u64da\u5eab\uff0c\u5c0e\u81f4\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u589e\u52a0\u3002\u9019\u7a2e\u60c5\u6cc1\u901a\u5e38\u767c\u751f\u5728\u7528\u6236\u8acb\u6c42\u7684\u6578\u64da\u4e0d\u5b58\u5728\u65bc Redis \u4e2d\uff0c\u4e26\u4e14\u6c92\u6709\u9069\u7576\u7684\u9632\u8b77\u63aa\u65bd\u4f86\u963b\u6b62\u9019\u4e9b\u8acb\u6c42\u3002<\/p>\n<h2 id=\"redis-%e7%a9%bf%e9%80%8f%e7%9a%84%e5%bd%b1%e9%9f%bf-ROxuyOyRIG\">Redis \u7a7f\u900f\u7684\u5f71\u97ff<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u4e0b\u964d\uff1a<\/strong>\u5927\u91cf\u7684\u7121\u6548\u8acb\u6c42\u6703\u6d88\u8017\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8cc7\u6e90\uff0c\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u6d6a\u8cbb\uff1a<\/strong>\u7121\u6548\u8acb\u6c42\u6703\u6d6a\u8cbb\u7cfb\u7d71\u8cc7\u6e90\uff0c\u589e\u52a0\u904b\u884c\u6210\u672c\u3002<\/li>\n<li><strong>\u5b89\u5168\u98a8\u96aa\uff1a<\/strong>\u5982\u679c\u6c92\u6709\u9069\u7576\u7684\u9632\u8b77\uff0c\u653b\u64ca\u8005\u53ef\u80fd\u5229\u7528\u7a7f\u900f\u6f0f\u6d1e\u767c\u8d77 DDoS \u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba-redis-%e7%a9%bf%e9%80%8f%e7%9a%84%e6%96%b9%e6%a1%88-ROxuyOyRIG\">\u89e3\u6c7a Redis \u7a7f\u900f\u7684\u65b9\u6848<\/h2>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8%e5%b8%83%e9%9a%86%e9%81%8e%e6%bf%be%e5%99%a8%ef%bc%88bloom-filter%ef%bc%89-ROxuyOyRIG\">1. \u4f7f\u7528\u5e03\u9686\u904e\u6ffe\u5668\uff08Bloom Filter\uff09<\/h3>\n<p>\u5e03\u9686\u904e\u6ffe\u5668\u662f\u4e00\u7a2e\u7a7a\u9593\u6548\u7387\u9ad8\u7684\u6982\u7387\u578b\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u7528\u4f86\u5224\u65b7\u4e00\u500b\u5143\u7d20\u662f\u5426\u5728\u4e00\u500b\u96c6\u5408\u4e2d\u3002\u901a\u904e\u5728 Redis \u524d\u7aef\u4f7f\u7528\u5e03\u9686\u904e\u6ffe\u5668\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u904e\u6ffe\u6389\u4e0d\u5b58\u5728\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002<\/p>\n<pre><code>\n# \u5047\u8a2d\u4f7f\u7528 Python \u7684 redis-py \u5eab\nfrom redis import Redis\nfrom pybloom_live import BloomFilter\n\n# \u521d\u59cb\u5316 Redis \u548c\u5e03\u9686\u904e\u6ffe\u5668\nredis_client = Redis(host='localhost', port=6379)\nbloom = BloomFilter(capacity=100000, error_rate=0.001)\n\n# \u6dfb\u52a0\u5b58\u5728\u7684\u6578\u64da\u5230\u5e03\u9686\u904e\u6ffe\u5668\nfor item in existing_data:\n    bloom.add(item)\n\n# \u6aa2\u67e5\u8acb\u6c42\u7684\u6578\u64da\u662f\u5426\u5b58\u5728\nif request_data in bloom:\n    # \u67e5\u8a62 Redis\n    result = redis_client.get(request_data)\nelse:\n    # \u8fd4\u56de\u4e0d\u5b58\u5728\u7684\u97ff\u61c9\n    return \"Data not found\"\n<\/code><\/pre>\n<h3 id=\"2-%e7%b7%a9%e5%ad%98%e7%a9%ba%e7%b5%90%e6%9e%9c-ROxuyOyRIG\">2. \u7de9\u5b58\u7a7a\u7d50\u679c<\/h3>\n<p>\u5c0d\u65bc\u4e00\u4e9b\u5e38\u898b\u7684\u7121\u6548\u8acb\u6c42\uff0c\u53ef\u4ee5\u8003\u616e\u5c07\u7a7a\u7d50\u679c\u7de9\u5b58\u5230 Redis \u4e2d\u3002\u9019\u6a23\uff0c\u7576\u76f8\u540c\u7684\u8acb\u6c42\u518d\u6b21\u51fa\u73fe\u6642\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7a7a\u7d50\u679c\uff0c\u800c\u4e0d\u9700\u8981\u67e5\u8a62\u5f8c\u7aef\u6578\u64da\u5eab\u3002<\/p>\n<pre><code>\n# \u7de9\u5b58\u7a7a\u7d50\u679c\nif result is None:\n    redis_client.set(request_data, None, ex=60)  # \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n    return \"Data not found\"\n<\/code><\/pre>\n<h3 id=\"3-%e9%99%90%e6%b5%81%e5%92%8c%e9%98%b2%e7%81%ab%e7%89%86-ROxuyOyRIG\">3. \u9650\u6d41\u548c\u9632\u706b\u7246<\/h3>\n<p>\u901a\u904e\u8a2d\u7f6e\u9650\u6d41\u6a5f\u5236\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u8acb\u6c42\u7684\u983b\u7387\uff0c\u9632\u6b62\u60e1\u610f\u653b\u64ca\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u9632\u706b\u7246\u53ef\u4ee5\u904e\u6ffe\u6389\u53ef\u7591\u7684\u8acb\u6c42\uff0c\u9032\u4e00\u6b65\u63d0\u9ad8\u7cfb\u7d71\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-ROxuyOyRIG\">\u7d50\u8ad6<\/h2>\n<p>Redis \u7a7f\u900f\u554f\u984c\u5c0d\u65bc\u9ad8\u6027\u80fd\u61c9\u7528\u4f86\u8aaa\u662f\u4e00\u500b\u4e0d\u5bb9\u5ffd\u8996\u7684\u6311\u6230\u3002\u901a\u904e\u4f7f\u7528\u5e03\u9686\u904e\u6ffe\u5668\u3001\u7de9\u5b58\u7a7a\u7d50\u679c\u4ee5\u53ca\u9650\u6d41\u548c\u9632\u706b\u7246\u7b49\u63aa\u65bd\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u7a7f\u900f\u554f\u984c\u5e36\u4f86\u7684\u5f71\u97ff\uff0c\u63d0\u5347\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u53d6\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u89e3\u6c7a\u65b9\u6848\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u7a7f\u900f\u9ad8\u6548\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u5347\u6578\u64da\u5b58\u53d6\u6548\u7387\uff0c\u78ba\u4fdd\u7cfb\u7d71\u5b89\u5168\u6027\uff0c\u52a9\u529b\u696d\u52d9\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-98350","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98350","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=98350"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98350\/revisions"}],"predecessor-version":[{"id":98351,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98350\/revisions\/98351"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=98350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=98350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=98350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}