{"id":32486,"date":"2024-11-25T10:38:03","date_gmt":"2024-11-25T02:38:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32486\/"},"modified":"2024-11-25T10:38:03","modified_gmt":"2024-11-25T02:38:03","slug":"%e4%bd%bf%e7%94%a8-node-js-%e5%92%8c-redis-%e6%8e%a2%e7%b4%a2-bloom-filter-%e7%9a%84%e9%ad%85%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32486\/","title":{"rendered":"\u4f7f\u7528 Node.js \u548c Redis \u63a2\u7d22 Bloom Filter \u7684\u9b45\u529b"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169357999078500.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Node.js \u548c Redis \u63a2\u7d22 Bloom Filter \u7684\u9b45\u529b\u63d2\u56fe\" alt=\"\u4f7f\u7528 Node.js \u548c Redis \u63a2\u7d22 Bloom Filter \u7684\u9b45\u529b\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u6b63\u786e\u7684\u7528\u4f8b\u4e2d\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u770b\u8d77\u6765\u5c31\u50cf\u9b54\u6cd5\u3002\u8fd9\u662f\u4e00\u4e2a\u5927\u80c6\u7684\u8bf4\u6cd5\uff0c\u4f46\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u79cd\u5947\u602a\u7684\u6570\u636e\u7ed3\u6784\u3001\u5982\u4f55\u6700\u597d\u5730\u4f7f\u7528\u5b83\uff0c\u4ee5\u53ca\u4e00\u4e9b\u4f7f\u7528 Redis \u548c Node.js \u7684\u5b9e\u9645\u793a\u4f8b\u3002<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u4e00\u79cd\u6982\u7387\u6027\u3001\u5355\u5411\u6570\u636e\u7ed3\u6784\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u201c\u8fc7\u6ee4\u5668\u201d\u4e00\u8bcd\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\uff1b\u8fc7\u6ee4\u5668\u610f\u5473\u7740\u5b83\u662f\u4e00\u4e2a\u6d3b\u8dc3\u7684\u4e8b\u7269\uff0c\u4e00\u4e2a\u52a8\u8bcd\uff0c\u4f46\u5c06\u5b83\u89c6\u4e3a\u5b58\u50a8\uff0c\u4e00\u4e2a\u540d\u8bcd\u53ef\u80fd\u66f4\u5bb9\u6613\u3002\u4f7f\u7528\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u60a8\u53ef\u4ee5\u505a\u4e24\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u6dfb\u52a0\u4e00\u4e2a\u9879\u76ee\u3002<\/li>\n<li>\u68c0\u67e5\u4e4b\u524d\u662f\u5426<em>\u672a<\/em>\u6dfb\u52a0\u8fc7\u67d0\u4e2a\u9879\u76ee\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u662f\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u9650\u5236 &#8211; \u60a8\u65e0\u6cd5\u5220\u9664\u9879\u76ee\uff0c\u4e5f\u65e0\u6cd5\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u5217\u51fa\u9879\u76ee\u3002\u6b64\u5916\uff0c\u60a8\u65e0\u6cd5\u786e\u5b9a\u8fc7\u53bb\u662f\u5426\u5df2\u5c06\u67d0\u4e2a\u9879\u76ee\u6dfb\u52a0\u5230\u8fc7\u6ee4\u5668\u4e2d\u3002\u8fd9\u5c31\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u6982\u7387\u6027\u8d28\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u2014\u2014\u8bef\u62a5\u662f\u53ef\u80fd\u7684\uff0c\u4f46\u8bef\u62a5\u5219\u4e0d\u7136\u3002\u5982\u679c\u8fc7\u6ee4\u5668\u8bbe\u7f6e\u6b63\u786e\uff0c\u8bef\u62a5\u7684\u53ef\u80fd\u6027\u4f1a\u975e\u5e38\u5c0f\u3002<\/p>\n<p>\u5b58\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u6dfb\u52a0\u4e86\u5176\u4ed6\u529f\u80fd\uff0c\u4f8b\u5982\u5220\u9664\u6216\u7f29\u653e\uff0c\u4f46\u5b83\u4eec\u4e5f\u589e\u52a0\u4e86\u590d\u6742\u6027\u548c\u9650\u5236\u3002\u5728\u7ee7\u7eed\u4e86\u89e3\u53d8\u4f53\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u975e\u5e38\u91cd\u8981\u3002\u672c\u6587\u4ec5\u4ecb\u7ecd\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u3002<\/p>\n<p>\u6709\u4e86\u8fd9\u4e9b\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u83b7\u5f97\u8bb8\u591a\u597d\u5904\uff1a\u56fa\u5b9a\u5927\u5c0f\u3001\u57fa\u4e8e\u54c8\u5e0c\u7684\u52a0\u5bc6\u548c\u5feb\u901f\u67e5\u627e\u3002<\/p>\n<p>\u5f53\u60a8\u8bbe\u7f6e\u5e03\u9686\u8fc7\u6ee4\u5668\u65f6\uff0c\u60a8\u9700\u8981\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u5927\u5c0f\u3002\u6b64\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u5982\u679c\u8fc7\u6ee4\u5668\u4e2d\u6709\u4e00\u9879\u6216\u5341\u4ebf\u9879\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u589e\u957f\u8d85\u8fc7\u6307\u5b9a\u7684\u5927\u5c0f\u3002\u5f53\u60a8\u5411\u8fc7\u6ee4\u5668\u6dfb\u52a0\u66f4\u591a\u9879\u76ee\u65f6\uff0c\u8bef\u62a5\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u589e\u52a0\u3002\u5982\u679c\u60a8\u6307\u5b9a\u4e86\u8f83\u5c0f\u7684\u8fc7\u6ee4\u5668\uff0c\u5219\u4e0e\u4f7f\u7528\u8f83\u5927\u7684\u8fc7\u6ee4\u5668\u76f8\u6bd4\uff0c\u8bef\u62a5\u7387\u4f1a\u589e\u52a0\u5f97\u66f4\u5feb\u3002<\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u5efa\u7acb\u5728\u5355\u5411\u6563\u5217\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u4e0e\u6b63\u786e\u5b58\u50a8\u5bc6\u7801\u975e\u5e38\u76f8\u4f3c\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u6765\u786e\u5b9a\u4f20\u5165\u5176\u4e2d\u7684\u9879\u76ee\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u54c8\u5e0c\u672c\u8d28\u4e0a\u662f\u65e0\u6cd5\u9006\u8f6c\u7684\uff0c\u5e76\u4e14\u7531\u770b\u4f3c\u968f\u673a\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6709\u4eba\u83b7\u5f97\u4e86\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5b83\u4e0d\u4f1a\u76f4\u63a5\u6cc4\u9732\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p>\u6700\u540e\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u901f\u5ea6\u5f88\u5feb\u3002\u4e0e\u5176\u4ed6\u65b9\u6cd5\u76f8\u6bd4\uff0c\u8be5\u64cd\u4f5c\u6d89\u53ca\u7684\u6bd4\u8f83\u6b21\u6570\u8981\u5c11\u5f97\u591a\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u9632\u6b62\u5f71\u54cd\u6027\u80fd\u7684\u6570\u636e\u5e93\u547d\u4e2d\u3002<\/p>\n<p>\u73b0\u5728\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u9650\u5236\u548c\u4f18\u70b9\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u7684\u4e00\u4e9b\u60c5\u51b5\u3002<\/p>\n<h2>\u8bbe\u7f6e<\/h2>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528 Redis \u548c Node.js \u6765\u8bf4\u660e Bloom \u8fc7\u6ee4\u5668\u3002 Redis \u662f Bloom \u8fc7\u6ee4\u5668\u7684\u5b58\u50a8\u4ecb\u8d28\uff1b\u5b83\u901f\u5ea6\u5feb\uff0c\u4f4d\u4e8e\u5185\u5b58\u4e2d\uff0c\u5e76\u4e14\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u547d\u4ee4\uff08GETBIT\u3001SETBIT\uff09\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5b9e\u65bd\u6548\u7387\u3002\u6211\u5047\u8bbe\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 Node.js\u3001npm \u548c Redis\u3002\u60a8\u7684 Redis \u670d\u52a1\u5668\u5e94\u8be5\u5728 localhost \u4e0a\u7684\u9ed8\u8ba4\u7aef\u53e3\u4e0a\u8fd0\u884c\uff0c\u4ee5\u4fbf\u6211\u4eec\u7684\u793a\u4f8b\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4f1a\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u8fc7\u6ee4\u5668\uff1b\u800c\u662f\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u8fc7\u6ee4\u5668\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u5173\u6ce8 npm \u4e2d\u9884\u6784\u5efa\u6a21\u5757\u7684\u5b9e\u9645\u7528\u9014\uff1abloom-redis\u3002 bloom-redis \u6709\u4e00\u7ec4\u975e\u5e38\u7b80\u6d01\u7684\u65b9\u6cd5\uff1aadd\u3001contains \u548c clear\u3002<\/p>\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u9700\u8981\u54c8\u5e0c\u7b97\u6cd5\u6765\u751f\u6210\u9879\u76ee\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002 bloom-redis \u4f7f\u7528\u4f17\u6240\u5468\u77e5\u7684 MD5 \u7b97\u6cd5\uff0c\u5c3d\u7ba1\u5b83\u53ef\u80fd\u4e0d\u9002\u5408 Bloom \u8fc7\u6ee4\u5668\uff08\u6709\u70b9\u6162\uff0c\u6709\u70b9\u8fc7\u5927\uff09\uff0c\u4f46\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<h2>\u72ec\u7279\u7684\u7528\u6237\u540d<\/h2>\n<p>\u7528\u6237\u540d\uff0c\u5c24\u5176\u662f\u5728 URL \u4e2d\u6807\u8bc6\u7528\u6237\u7684\u7528\u6237\u540d\uff0c\u9700\u8981\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u60a8\u6784\u5efa\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u66f4\u6539\u7528\u6237\u540d\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u4ece\u672a\u4f7f\u7528\u8fc7\u7684\u7528\u6237\u540d\uff0c\u4ee5\u907f\u514d\u7528\u6237\u540d\u6df7\u6dc6\u548c\u88ab\u653b\u51fb\u3002<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u60a8\u9700\u8981\u5f15\u7528\u4e00\u4e2a\u5305\u542b\u66fe\u7ecf\u4f7f\u7528\u8fc7\u7684\u6bcf\u4e2a\u7528\u6237\u540d\u7684\u8868\uff0c\u800c\u5927\u89c4\u6a21\u65f6\u8fd9\u53ef\u80fd\u4f1a\u975e\u5e38\u6602\u8d35\u3002\u5e03\u9686\u8fc7\u6ee4\u5668\u5141\u8bb8\u60a8\u5728\u7528\u6237\u6bcf\u6b21\u91c7\u7528\u65b0\u540d\u79f0\u65f6\u6dfb\u52a0\u4e00\u4e2a\u9879\u76ee\u3002\u5f53\u7528\u6237\u68c0\u67e5\u7528\u6237\u540d\u662f\u5426\u88ab\u5360\u7528\u65f6\uff0c\u60a8\u6240\u9700\u8981\u505a\u7684\u5c31\u662f\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u5b83\u5c06\u80fd\u591f\u7edd\u5bf9\u786e\u5b9a\u5730\u544a\u8bc9\u60a8\u6240\u8bf7\u6c42\u7684\u7528\u6237\u540d\u662f\u5426\u5148\u524d\u5df2\u6dfb\u52a0\u3002\u8fc7\u6ee4\u5668\u53ef\u80fd\u4f1a\u9519\u8bef\u5730\u8fd4\u56de\u7528\u6237\u540d\u5df2\u88ab\u4f7f\u7528\uff0c\u800c\u5b9e\u9645\u4e0a\u7528\u6237\u540d\u5c1a\u672a\u88ab\u4f7f\u7528\uff0c\u4f46\u8fd9\u53ea\u662f\u4e3a\u4e86\u8c28\u614e\u8d77\u89c1\uff0c\u4e0d\u4f1a\u9020\u6210\u4efb\u4f55\u771f\u6b63\u7684\u4f24\u5bb3\uff08\u9664\u4e86\u7528\u6237\u53ef\u80fd\u65e0\u6cd5\u58f0\u660e\u201ck3w1d00d47\u201d\uff09 .<\/p>\n<p>\u4e3a\u4e86\u8bf4\u660e\u8fd9\u4e00\u70b9\uff0c\u8ba9\u6211\u4eec\u4f7f\u7528 Express \u6784\u5efa\u4e00\u4e2a\u5feb\u901f\u7684 REST \u670d\u52a1\u5668\u3002\u9996\u5148\uff0c\u521b\u5efa package.json \u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u7ec8\u7aef\u547d\u4ee4\u3002<\/p>\n<p>npm \u5b89\u88c5bloom-redis &#8211;save<\/p>\n<p>npm install express &#8211;save<\/p>\n<p>npm install redis &#8211;save<\/p>\n<p>bloom-redis \u7684\u9ed8\u8ba4\u9009\u9879\u5927\u5c0f\u8bbe\u7f6e\u4e3a 2 MB\u3002\u51fa\u4e8e\u8c28\u614e\u8003\u8651\uff0c\u8fd9\u662f\u9519\u8bef\u7684\uff0c\u4f46\u5b83\u76f8\u5f53\u5927\u3002\u8bbe\u7f6e\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5927\u5c0f\u81f3\u5173\u91cd\u8981\uff1a\u592a\u5927\u4f1a\u6d6a\u8d39\u5185\u5b58\uff0c\u592a\u5c0f\u5219\u8bef\u62a5\u7387\u4f1a\u592a\u9ad8\u3002\u786e\u5b9a\u5927\u5c0f\u6240\u6d89\u53ca\u7684\u6570\u5b66\u975e\u5e38\u590d\u6742\uff0c\u8d85\u51fa\u4e86\u672c\u6559\u7a0b\u7684\u8303\u56f4\uff0c\u4f46\u5e78\u8fd0\u7684\u662f\uff0c\u6709\u4e00\u4e2a\u5e03\u9686\u8fc7\u6ee4\u5668\u5927\u5c0f\u8ba1\u7b97\u5668\u53ef\u4ee5\u5b8c\u6210\u8fd9\u9879\u5de5\u4f5c\uff0c\u800c\u65e0\u9700\u7834\u89e3\u6559\u79d1\u4e66\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u521b\u5efa app.js \u5982\u4e0b\uff1a<\/p>\n<pre>\nvar\n  Bloom         =   require('bloom-redis'),\n  express       =   require('express'),\n  redis         =   require('redis'),\n  \n  app,\n  client,\n  filter;\n\n\/\/setup our Express server\napp = express();\n\n\/\/create the connection to Redis\nclient = redis.createClient();\n\n\nfilter = new Bloom.BloomFilter({ \n  client    : client, \/\/make sure the Bloom module uses our newly created connection to Redis\n  key       : 'username-bloom-filter', \/\/the Redis key\n  \n  \/\/calculated size of the Bloom filter.\n  \/\/This is where your size \/ probability trade-offs are made\n  \/\/http:\/\/hur.st\/bloomfilter?n=100000&amp;amp;p=1.0E-6\n  size      : 2875518, \/\/ ~350kb\n  numHashes : 20\n});\n\napp.get('\/check', function(req,res,next) {\n  \/\/check to make sure the query string has 'username'\n  if (typeof req.query.username === 'undefined') {\n    \/\/skip this route, go to the next one - will result in a 404 \/ not found\n    next('route');\n  } else {\n   filter.contains(\n     req.query.username, \/\/ the username from the query string\n     function(err, result) {\n       if (err) { \n        next(err); \/\/if an error is encountered, send it to the client\n        } else {\n          res.send({ \n            username : req.query.username, \n            \/\/if the result is false, then we know the item has *not* been used\n            \/\/if the result is true, then we can assume that the item has been used\n            status : result ? 'used' : 'free' \n          });\n        }\n      }\n    );\n  }\n});\n\n\napp.get('\/save',function(req,res,next) {\n  if (typeof req.query.username === 'undefined') {\n    next('route');\n  } else {\n    \/\/first, we need to make sure that it's not yet in the filter\n    filter.contains(req.query.username, function(err, result) {\n      if (err) { next(err); } else {\n        if (result) {\n          \/\/true result means it already exists, so tell the user\n          res.send({ username : req.query.username, status : 'not-created' });\n        } else {\n          \/\/we'll add the username passed in the query string to the filter\n          filter.add(\n            req.query.username, \n            function(err) {\n              \/\/The callback arguments to `add` provides no useful information, so we'll just check to make sure that no error was passed\n              if (err) { next(err); } else {\n                res.send({ \n                  username : req.query.username, status : 'created' \n                });\n              }\n            }\n          );\n        }\n      }\n    });\n  }\n});\n\napp.listen(8010);\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8981\u8fd0\u884c\u6b64\u670d\u52a1\u5668\uff1anode app.js\u3002\u8f6c\u5230\u6d4f\u89c8\u5668\u5e76\u5c06\u5176\u6307\u5411\uff1ahttps:\/\/localhost:8010\/check?username=kyle\u3002\u54cd\u5e94\u5e94\u8be5\u662f\uff1a{&#8220;username&#8221;:&#8221;kyle&#8221;,&#8221;status&#8221;:&#8221;free&#8221;}\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u901a\u8fc7\u5c06\u6d4f\u89c8\u5668\u6307\u5411 http:\/\/localhost:8010\/save?username=kyle \u6765\u4fdd\u5b58\u8be5\u7528\u6237\u540d\u3002\u54cd\u5e94\u5c06\u662f\uff1a{&#8220;username&#8221;:&#8221;kyle&#8221;,&#8221;status&#8221;:&#8221;created&#8221;}\u3002\u5982\u679c\u8fd4\u56de\u5730\u5740 http:\/\/localhost:8010\/check?username=kyle\uff0c\u54cd\u5e94\u5c06\u662f {&#8220;username&#8221;:&#8221;kyle&#8221;,&#8221;status &#8220;:&#8221;\u5df2\u4f7f\u7528&#8221;}.\u540c\u6837\uff0c\u8fd4\u56de http:\/\/localhost:8010\/save?username=kyle \u5c06\u5bfc\u81f4 {&#8220;username&#8221;:&#8221;kyle&#8221;,&#8221;status&#8221;:&#8221;not -\u521b\u5efa\u201c}\u3002<\/p>\n<p>\u4ece\u7ec8\u7aef\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u8fc7\u6ee4\u5668\u7684\u5927\u5c0f\uff1a redis-cli strlen \u7528\u6237\u540d-bloom-filter\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5bf9\u4e8e\u4e00\u9879\uff0c\u5b83\u5e94\u8be5\u663e\u793a 338622\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u7ee7\u7eed\u5c1d\u8bd5\u4f7f\u7528 \/save \u8def\u7531\u6dfb\u52a0\u66f4\u591a\u7528\u6237\u540d\u3002\u60a8\u60f3\u5c1d\u8bd5\u591a\u5c11\u5c31\u5c1d\u8bd5\u591a\u5c11\u3002<\/p>\n<p>\u5982\u679c\u60a8\u518d\u6b21\u68c0\u67e5\u5c3a\u5bf8\uff0c\u60a8\u53ef\u80fd\u4f1a\u53d1\u73b0\u5c3a\u5bf8\u7565\u6709\u589e\u52a0\uff0c\u4f46\u5e76\u975e\u6bcf\u6b21\u6dfb\u52a0\u90fd\u4f1a\u589e\u52a0\u3002\u5f88\u597d\u5947\uff0c\u5bf9\u5427\uff1f\u5728\u5185\u90e8\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u5728\u4fdd\u5b58\u5728 username-bloom \u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u4e0d\u540c\u4f4d\u7f6e\u8bbe\u7f6e\u5404\u4e2a\u4f4d\uff081\/0\uff09\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u56e0\u6b64\u5982\u679c\u60a8\u5728\u7d22\u5f15 0 \u5904\u8bbe\u7f6e\u4e00\u4f4d\uff0c\u7136\u540e\u5728\u7d22\u5f15 10,000 \u5904\u8bbe\u7f6e\u4e00\u4f4d\uff0c\u5219\u4e24\u8005\u4e4b\u95f4\u7684\u6240\u6709\u5185\u5bb9\u90fd\u5c06\u4e3a 0\u3002\u5bf9\u4e8e\u5b9e\u9645\u7528\u9014\uff0c\u4e00\u5f00\u59cb\u4e86\u89e3\u6bcf\u4e2a\u64cd\u4f5c\u7684\u7cbe\u786e\u673a\u5236\u5e76\u4e0d\u91cd\u8981\uff0c\u53ea\u9700\u77e5\u9053\u8fd9\u4e00\u70b9\u5373\u53ef\u8fd9\u662f\u6b63\u5e38\u7684\uff0c\u60a8\u5728 Redis \u4e2d\u7684\u5b58\u50a8\u6c38\u8fdc\u4e0d\u4f1a\u8d85\u8fc7\u60a8\u6307\u5b9a\u7684\u503c\u3002<\/p>\n<h2>\u65b0\u9c9c\u5185\u5bb9<\/h2>\n<p>\u7f51\u7ad9\u4e0a\u7684\u65b0\u9c9c\u5185\u5bb9\u53ef\u4ee5\u5438\u5f15\u7528\u6237\u56de\u5934\u5ba2\uff0c\u90a3\u4e48\u5982\u4f55\u6bcf\u6b21\u90fd\u5411\u7528\u6237\u5c55\u793a\u65b0\u7684\u5185\u5bb9\u5462\uff1f\u4f7f\u7528\u4f20\u7edf\u7684\u6570\u636e\u5e93\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5411\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5305\u542b\u7528\u6237\u6807\u8bc6\u7b26\u548c\u6545\u4e8b\u6807\u8bc6\u7b26\u7684\u65b0\u884c\uff0c\u7136\u540e\u5728\u51b3\u5b9a\u663e\u793a\u4e00\u6bb5\u5185\u5bb9\u65f6\u67e5\u8be2\u8be5\u8868\u3002\u6b63\u5982\u60a8\u53ef\u80fd\u60f3\u8c61\u7684\u90a3\u6837\uff0c\u60a8\u7684\u6570\u636e\u5e93\u5c06\u589e\u957f\u5f97\u975e\u5e38\u5feb\uff0c\u5c24\u5176\u662f\u968f\u7740\u7528\u6237\u548c\u5185\u5bb9\u7684\u589e\u957f\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6f0f\u62a5\uff08\u4f8b\u5982\u4e0d\u663e\u793a\u770b\u4e0d\u89c1\u7684\u5185\u5bb9\uff09\u7684\u540e\u679c\u975e\u5e38\u5c0f\uff0c\u8fd9\u4f7f\u5f97\u5e03\u9686\u8fc7\u6ee4\u5668\u6210\u4e3a\u4e00\u4e2a\u53ef\u884c\u7684\u9009\u62e9\u3002\u4e4d\u4e00\u770b\uff0c\u60a8\u53ef\u80fd\u8ba4\u4e3a\u6bcf\u4e2a\u7528\u6237\u90fd\u9700\u8981\u4e00\u4e2a\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u4f46\u6211\u4eec\u5c06\u4f7f\u7528\u7528\u6237\u6807\u8bc6\u7b26\u548c\u5185\u5bb9\u6807\u8bc6\u7b26\u7684\u7b80\u5355\u4e32\u8054\uff0c\u7136\u540e\u5c06\u8be5\u5b57\u7b26\u4e32\u63d2\u5165\u5230\u6211\u4eec\u7684\u8fc7\u6ee4\u5668\u4e2d\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u6240\u6709\u7528\u6237\u4f7f\u7528\u5355\u4e2a\u8fc7\u6ee4\u5668\u3002<\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u8ba9\u6211\u4eec\u6784\u5efa\u53e6\u4e00\u4e2a\u663e\u793a\u5185\u5bb9\u7684\u57fa\u672c Express \u670d\u52a1\u5668\u3002\u6bcf\u6b21\u60a8\u8bbf\u95ee\u8def\u7531 \/show-content\/any-username \uff08<em>any-username<\/em> \u662f\u4efb\u4f55 URL \u5b89\u5168\u503c\uff09\u65f6\uff0c\u90fd\u4f1a\u663e\u793a\u4e00\u6761\u65b0\u5185\u5bb9\uff0c\u76f4\u5230\u8be5\u7f51\u7ad9\u6ca1\u6709\u5185\u5bb9\u3002\u5728\u793a\u4f8b\u4e2d\uff0c\u5185\u5bb9\u662f\u53e4\u817e\u5821\u8ba1\u5212\u524d\u5341\u540d\u4e66\u7c4d\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u518d\u5b89\u88c5\u4e00\u4e2a npm \u6a21\u5757\u3002\u4ece\u7ec8\u7aef\u8fd0\u884c\uff1a npm install async &#8211;save<\/p>\n<p>\u60a8\u7684\u65b0 app.js \u6587\u4ef6\uff1a<\/p>\n<pre>\nvar\n  async         =   require('async'),\n  Bloom         =   require('bloom-redis'),\n  express       =   require('express'),\n  redis         =   require('redis'),\n  \n  app,\n  client,\n  filter,\n  \n  \/\/ From Project Gutenberg - opening lines of the top 10 public domain ebooks\n  \/\/ https:\/\/www.gutenberg.org\/browse\/scores\/top\n  openingLines = {\n    'pride-and-prejudice' : \n      'It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.',\n    'alices-adventures-in-wonderland' : \n      'Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it' }\n      \n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u60a8\u4ed4\u7ec6\u6ce8\u610f\u5f00\u53d1\u5de5\u5177\u4e2d\u7684\u5f80\u8fd4\u65f6\u95f4\uff0c\u60a8\u4f1a\u53d1\u73b0\u4f7f\u7528\u7528\u6237\u540d\u8bf7\u6c42\u5355\u4e2a\u8def\u5f84\u7684\u6b21\u6570\u8d8a\u591a\uff0c\u6240\u9700\u7684\u65f6\u95f4\u5c31\u8d8a\u957f\u3002\u867d\u7136\u68c0\u67e5\u8fc7\u6ee4\u5668\u9700\u8981\u56fa\u5b9a\u7684\u65f6\u95f4\uff0c\u4f46\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u68c0\u67e5\u662f\u5426\u5b58\u5728\u66f4\u591a\u9879\u76ee\u3002\u5e03\u9686\u8fc7\u6ee4\u5668\u80fd\u591f\u544a\u8bc9\u60a8\u7684\u4fe1\u606f\u6709\u9650\uff0c\u56e0\u6b64\u60a8\u6b63\u5728\u6d4b\u8bd5\u6bcf\u4e2a\u9879\u76ee\u662f\u5426\u5b58\u5728\u3002\u5f53\u7136\uff0c\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u5b83\u76f8\u5f53\u7b80\u5355\uff0c\u4f46\u6d4b\u8bd5\u6570\u767e\u4e2a\u9879\u76ee\u6548\u7387\u5f88\u4f4e\u3002<\/p>\n<h2>\u8fc7\u65f6\u6570\u636e<\/h2>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\u5c0f\u578b Express \u670d\u52a1\u5668\uff0c\u5b83\u5c06\u6267\u884c\u4e24\u4ef6\u4e8b\uff1a\u901a\u8fc7 POST \u63a5\u53d7\u65b0\u6570\u636e\uff0c\u5e76\u663e\u793a\u5f53\u524d\u6570\u636e\uff08\u4f7f\u7528 GET \u8bf7\u6c42\uff09\u3002\u5f53\u65b0\u6570\u636e\u88ab POST \u5230\u670d\u52a1\u5668\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u5c06\u68c0\u67e5\u5b83\u662f\u5426\u5b58\u5728\u4e8e\u8fc7\u6ee4\u5668\u4e2d\u3002\u5982\u679c\u5b83\u4e0d\u5b58\u5728\uff0c\u6211\u4eec\u4f1a\u5c06\u5176\u6dfb\u52a0\u5230 Redis \u4e2d\u7684\u96c6\u5408\u4e2d\uff0c\u5426\u5219\u6211\u4eec\u5c06\u8fd4\u56de null\u3002 GET \u8bf7\u6c42\u5c06\u4ece Redis \u83b7\u53d6\u5b83\u5e76\u5c06\u5176\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u8fd9\u4e0e\u524d\u4e24\u79cd\u60c5\u51b5\u4e0d\u540c\uff0c\u8bef\u62a5\u662f\u4e0d\u884c\u7684\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668\u4f5c\u4e3a\u7b2c\u4e00\u9053\u9632\u7ebf\u3002\u8003\u8651\u5230\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u5c5e\u6027\uff0c\u6211\u4eec\u53ea\u80fd\u786e\u5b9a\u67d0\u4e9b\u4e1c\u897f\u4e0d\u5728\u8fc7\u6ee4\u5668\u4e2d\uff0c\u56e0\u6b64\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u8ba9\u6570\u636e\u8fdb\u5165\u3002\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u8fd4\u56de\u7684\u6570\u636e\u53ef\u80fd\u5728\u8fc7\u6ee4\u5668\u4e2d\uff0c\u6211\u4eec\u5c06\u6839\u636e\u5b9e\u9645\u6570\u636e\u6e90\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u4ec0\u4e48\uff1f\u6211\u4eec\u83b7\u5f97\u4e86\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u68c0\u67e5\u5b9e\u9645\u6765\u6e90\u7684\u901f\u5ea6\u3002\u5728\u6570\u636e\u6e90\u901f\u5ea6\u8f83\u6162\u7684\u60c5\u51b5\u4e0b\uff08\u5916\u90e8 API\u3001\u5c0f\u578b\u6570\u636e\u5e93\u3001\u5e73\u9762\u6587\u4ef6\u7684\u4e2d\u95f4\uff09\uff0c\u786e\u5b9e\u9700\u8981\u63d0\u9ad8\u901f\u5ea6\u3002\u4e3a\u4e86\u6f14\u793a\u901f\u5ea6\uff0c\u6211\u4eec\u5728\u793a\u4f8b\u4e2d\u6dfb\u52a0 150 \u6beb\u79d2\u7684\u5b9e\u9645\u5ef6\u8fdf\u3002\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528 console.time \/ console.timeEnd \u6765\u8bb0\u5f55 Bloom \u8fc7\u6ee4\u5668\u68c0\u67e5\u548c\u975e Bloom \u8fc7\u6ee4\u5668\u68c0\u67e5\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8fd8\u5c06\u4f7f\u7528\u6781\u5176\u6709\u9650\u7684\u4f4d\u6570\uff1a\u4ec5 1024\u3002\u5b83\u5f88\u5feb\u5c31\u4f1a\u586b\u6ee1\u3002\u5f53\u5b83\u586b\u6ee1\u65f6\uff0c\u5b83\u5c06\u663e\u793a\u8d8a\u6765\u8d8a\u591a\u7684\u8bef\u62a5 &#8211; \u60a8\u4f1a\u770b\u5230\u54cd\u5e94\u65f6\u95f4\u968f\u7740\u8bef\u62a5\u7387\u7684\u586b\u6ee1\u800c\u589e\u52a0\u3002<\/p>\n<p>\u8be5\u670d\u52a1\u5668\u4f7f\u7528\u4e0e\u4e4b\u524d\u76f8\u540c\u7684\u6a21\u5757\uff0c\u56e0\u6b64\u5c06 app.js \u6587\u4ef6\u8bbe\u7f6e\u4e3a\uff1a<\/p>\n<pre>\nvar\n  async           =   require('async'),\n  Bloom           =   require('bloom-redis'),\n  bodyParser      =   require('body-parser'),\n  express         =   require('express'),\n  redis           =   require('redis'),\n  \n  app,\n  client,\n  filter,\n  \n  currentDataKey  = 'current-data',\n  usedDataKey     = 'used-data';\n  \napp = express();\nclient = redis.createClient();\n\nfilter = new Bloom.BloomFilter({ \n  client    : client,\n  key       : 'stale-bloom-filter',\n  \/\/for illustration purposes, this is a super small filter. It should fill up at around 500 items, so for a production load, you'd need something much larger!\n  size      : 1024,\n  numHashes : 20\n});\n\napp.post(\n  '\/',\n  bodyParser.text(),\n  function(req,res,next) {\n    var\n      used;\n      \n    console.log('POST -', req.body); \/\/log the current data being posted\n    console.time('post'); \/\/start measuring the time it takes to complete our filter and conditional verification process\n    \n    \/\/async.series is used to manage multiple asynchronous function calls.\n    async.series([\n      function(cb) {\n        filter.contains(req.body, function(err,filterStatus) {\n          if (err) { cb(err); } else {\n            used = filterStatus;\n            cb(err);\n          }\n        });\n      },\n      function(cb) {\n        if (used === false) {\n          \/\/Bloom filters do not have false negatives, so we need no further verification\n          cb(null);\n        } else {\n          \/\/it *may* be in the filter, so we need to do a follow up check\n          \/\/for the purposes of the tutorial, we'll add a 150ms delay in here since Redis can be fast enough to make it difficult to measure and the delay will simulate a slow database or API call\n          setTimeout(function() {\n            console.log('possible false positive');\n            client.sismember(usedDataKey, req.body, function(err, membership) {\n              if (err) { cb(err); } else {\n                \/\/sismember returns 0 if an member is not part of the set and 1 if it is.\n                \/\/This transforms those results into booleans for consistent logic comparison\n                used = membership === 0 ? false : true;\n                cb(err);\n              }\n            });\n          }, 150);\n        }\n      },\n      function(cb) {\n        if (used === false) {\n          console.log('Adding to filter');\n          filter.add(req.body,cb);\n        } else {\n          console.log('Skipped filter addition, [false] positive');\n          cb(null);\n        }\n      },\n      function(cb) {\n        if (used === false) {\n          client.multi()\n            .set(currentDataKey,req.body) \/\/unused data is set for easy access to the 'current-data' key\n            .sadd(usedDataKey,req.body) \/\/and added to a set for easy verification later\n            .exec(cb); \n        } else {\n          cb(null);\n        }\n      }\n      ],\n      function(err, cb) {\n        if (err) { next(err); } else {\n          console.timeEnd('post'); \/\/logs the amount of time since the console.time call above\n          res.send({ saved : !used }); \/\/returns if the item was saved, true for fresh data, false for stale data.\n        }\n      }\n    );\n});\n\napp.get('\/',function(req,res,next) {\n  \/\/just return the fresh data\n  client.get(currentDataKey, function(err,data) {\n    if (err) { next(err); } else {\n      res.send(data);\n    }\n  });\n});\n\napp.listen(8012);\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7531\u4e8e\u4f7f\u7528\u6d4f\u89c8\u5668 POST \u5230\u670d\u52a1\u5668\u53ef\u80fd\u4f1a\u5f88\u68d8\u624b\uff0c\u6240\u4ee5\u8ba9\u6211\u4eec\u4f7f\u7528curl \u6765\u6d4b\u8bd5\u3002<\/p>\n<p>curl &#8211;data\u201c\u60a8\u7684\u6570\u636e\u653e\u5728\u8fd9\u91cc\u201d&#8211;header\u201c\u5185\u5bb9\u7c7b\u578b\uff1atext\/plain\u201dhttp:\/\/localhost:8012\/<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5feb\u901f bash \u811a\u672c\u6765\u663e\u793a\u586b\u5145\u6574\u4e2a\u8fc7\u6ee4\u5668\u7684\u5916\u89c2\uff1a<\/p>\n<pre>\n#!\/bin\/bash\nfor i in `seq 1 500`;\ndo\n  curl --data \u201cdata $i\" --header \"Content-Type: text\/plain\" http:\/\/localhost:8012\/\ndone   \n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u89c2\u5bdf\u586b\u5145\u6216\u5b8c\u6574\u7684\u8fc7\u6ee4\u5668\u5f88\u6709\u8da3\u3002\u7531\u4e8e\u8fd9\u4e2a\u5f88\u5c0f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 redis-cli \u8f7b\u677e\u67e5\u770b\u3002\u901a\u8fc7\u5728\u6dfb\u52a0\u9879\u76ee\u4e4b\u95f4\u4ece\u7ec8\u7aef\u8fd0\u884c redis-cli get stale-filter \uff0c\u60a8\u5c06\u770b\u5230\u5404\u4e2a\u5b57\u8282\u589e\u52a0\u3002\u5b8c\u6574\u7684\u8fc7\u6ee4\u5668\u5c06\u4e3a\u6bcf\u4e2a\u5b57\u8282 \ufffd \u3002\u6b64\u65f6\uff0c\u8fc7\u6ee4\u5668\u5c06\u59cb\u7ec8\u8fd4\u56de\u6b63\u503c\u3002<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u5e76\u4e0d\u662f\u4e07\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u5728\u9002\u5f53\u7684\u60c5\u51b5\u4e0b\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u53ef\u4ee5\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u5feb\u901f\u3001\u6709\u6548\u7684\u8865\u5145\u3002<\/p>\n<p>\u5982\u679c\u60a8\u4ed4\u7ec6\u6ce8\u610f\u5f00\u53d1\u5de5\u5177\u4e2d\u7684\u5f80\u8fd4\u65f6\u95f4\uff0c\u60a8\u4f1a\u53d1\u73b0\u4f7f\u7528\u7528\u6237\u540d\u8bf7\u6c42\u5355\u4e2a\u8def\u5f84\u7684\u6b21\u6570\u8d8a\u591a\uff0c\u6240\u9700\u7684\u65f6\u95f4\u5c31\u8d8a\u957f\u3002\u867d\u7136\u68c0\u67e5\u8fc7\u6ee4\u5668\u9700\u8981\u56fa\u5b9a\u7684\u65f6\u95f4\uff0c\u4f46\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u6b63\u5728\u68c0\u67e5\u662f\u5426\u5b58\u5728\u66f4\u591a\u9879\u76ee\u3002\u5e03\u9686\u8fc7\u6ee4\u5668\u80fd\u591f\u544a\u8bc9\u60a8\u7684\u4fe1\u606f\u6709\u9650\uff0c\u56e0\u6b64\u60a8\u6b63\u5728\u6d4b\u8bd5\u6bcf\u4e2a\u9879\u76ee\u662f\u5426\u5b58\u5728\u3002\u5f53\u7136\uff0c\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u5b83\u76f8\u5f53\u7b80\u5355\uff0c\u4f46\u6d4b\u8bd5\u6570\u767e\u4e2a\u9879\u76ee\u6548\u7387\u5f88\u4f4e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Node.js \u548c Redis \u63a2\u7d22 Bloom Filter \u7684\u9b45\u529b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6b63\u786e\u7684\u7528\u4f8b\u4e2d\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u770b\u8d77\u6765\u5c31\u50cf\u9b54\u6cd5\u3002\u8fd9\u662f\u4e00\u4e2a\u5927\u80c6\u7684\u8bf4\u6cd5\uff0c\u4f46\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u8fd9\u79cd\u5947\u602a\u7684\u6570\u636e\u7ed3\u6784\u3001\u5982\u4f55\u6700\u597d\u5730\u4f7f\u7528\u5b83\uff0c\u4ee5\u53ca\u4e00\u4e9b\u4f7f\u7528 Redis \u548c Node.js \u7684\u5b9e\u9645\u793a\u4f8b\u3002 \u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u4e00\u79cd\u6982\u7387\u6027\u3001\u5355\u5411\u6570\u636e\u7ed3\u6784\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u201c\u8fc7\u6ee4\u5668\u201d\u4e00\u8bcd\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\uff1b\u8fc7\u6ee4\u5668\u610f\u5473\u7740\u5b83\u662f\u4e00\u4e2a\u6d3b\u8dc3\u7684\u4e8b\u7269\uff0c\u4e00\u4e2a\u52a8\u8bcd\uff0c\u4f46\u5c06\u5b83\u89c6\u4e3a\u5b58\u50a8\uff0c\u4e00\u4e2a\u540d\u8bcd\u53ef\u80fd\u66f4\u5bb9\u6613\u3002\u4f7f\u7528\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u60a8\u53ef\u4ee5\u505a\u4e24\u4ef6\u4e8b\uff1a \u6dfb\u52a0\u4e00\u4e2a\u9879\u76ee\u3002 \u68c0\u67e5\u4e4b\u524d\u662f\u5426\u672a\u6dfb\u52a0\u8fc7\u67d0\u4e2a\u9879\u76ee\u3002 \u8fd9\u4e9b\u662f\u9700\u8981\u7406\u89e3\u7684\u91cd\u8981\u9650\u5236 &#8211; \u60a8\u65e0\u6cd5\u5220\u9664\u9879\u76ee\uff0c\u4e5f\u65e0\u6cd5\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u4e2d\u5217\u51fa\u9879\u76ee\u3002\u6b64\u5916\uff0c\u60a8\u65e0\u6cd5\u786e\u5b9a\u8fc7\u53bb\u662f\u5426\u5df2\u5c06\u67d0\u4e2a\u9879\u76ee\u6dfb\u52a0\u5230\u8fc7\u6ee4\u5668\u4e2d\u3002\u8fd9\u5c31\u662f\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u6982\u7387\u6027\u8d28\u53d1\u6325\u4f5c\u7528\u7684\u5730\u65b9\u2014\u2014\u8bef\u62a5\u662f\u53ef\u80fd\u7684\uff0c\u4f46\u8bef\u62a5\u5219\u4e0d\u7136\u3002\u5982\u679c\u8fc7\u6ee4\u5668\u8bbe\u7f6e\u6b63\u786e\uff0c\u8bef\u62a5\u7684\u53ef\u80fd\u6027\u4f1a\u975e\u5e38\u5c0f\u3002 \u5b58\u5728\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u53d8\u4f53\uff0c\u5b83\u4eec\u6dfb\u52a0\u4e86\u5176\u4ed6\u529f\u80fd\uff0c\u4f8b\u5982\u5220\u9664\u6216\u7f29\u653e\uff0c\u4f46\u5b83\u4eec\u4e5f\u589e\u52a0\u4e86\u590d\u6742\u6027\u548c\u9650\u5236\u3002\u5728\u7ee7\u7eed\u4e86\u89e3\u53d8\u4f53\u4e4b\u524d\uff0c\u9996\u5148\u4e86\u89e3\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u975e\u5e38\u91cd\u8981\u3002\u672c\u6587\u4ec5\u4ecb\u7ecd\u7b80\u5355\u7684\u5e03\u9686\u8fc7\u6ee4\u5668\u3002 \u6709\u4e86\u8fd9\u4e9b\u9650\u5236\uff0c\u60a8\u53ef\u4ee5\u83b7\u5f97\u8bb8\u591a\u597d\u5904\uff1a\u56fa\u5b9a\u5927\u5c0f\u3001\u57fa\u4e8e\u54c8\u5e0c\u7684\u52a0\u5bc6\u548c\u5feb\u901f\u67e5\u627e\u3002 \u5f53\u60a8\u8bbe\u7f6e\u5e03\u9686\u8fc7\u6ee4\u5668\u65f6\uff0c\u60a8\u9700\u8981\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u5927\u5c0f\u3002\u6b64\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u5982\u679c\u8fc7\u6ee4\u5668\u4e2d\u6709\u4e00\u9879\u6216\u5341\u4ebf\u9879\uff0c\u5b83\u6c38\u8fdc\u4e0d\u4f1a\u589e\u957f\u8d85\u8fc7\u6307\u5b9a\u7684\u5927\u5c0f\u3002\u5f53\u60a8\u5411\u8fc7\u6ee4\u5668\u6dfb\u52a0\u66f4\u591a\u9879\u76ee\u65f6\uff0c\u8bef\u62a5\u7684\u53ef\u80fd\u6027\u5c31\u4f1a\u589e\u52a0\u3002\u5982\u679c\u60a8\u6307\u5b9a\u4e86\u8f83\u5c0f\u7684\u8fc7\u6ee4\u5668\uff0c\u5219\u4e0e\u4f7f\u7528\u8f83\u5927\u7684\u8fc7\u6ee4\u5668\u76f8\u6bd4\uff0c\u8bef\u62a5\u7387\u4f1a\u589e\u52a0\u5f97\u66f4\u5feb\u3002 \u5e03\u9686\u8fc7\u6ee4\u5668\u5efa\u7acb\u5728\u5355\u5411\u6563\u5217\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002\u4e0e\u6b63\u786e\u5b58\u50a8\u5bc6\u7801\u975e\u5e38\u76f8\u4f3c\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u6765\u786e\u5b9a\u4f20\u5165\u5176\u4e2d\u7684\u9879\u76ee\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u54c8\u5e0c\u672c\u8d28\u4e0a\u662f\u65e0\u6cd5\u9006\u8f6c\u7684\uff0c\u5e76\u4e14\u7531\u770b\u4f3c\u968f\u673a\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6709\u4eba\u83b7\u5f97\u4e86\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5b83\u4e0d\u4f1a\u76f4\u63a5\u6cc4\u9732\u4efb\u4f55\u5185\u5bb9\u3002 \u6700\u540e\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u901f\u5ea6\u5f88\u5feb\u3002\u4e0e\u5176\u4ed6\u65b9\u6cd5\u76f8\u6bd4\uff0c\u8be5\u64cd\u4f5c\u6d89\u53ca\u7684\u6bd4\u8f83\u6b21\u6570\u8981\u5c11\u5f97\u591a\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u9632\u6b62\u5f71\u54cd\u6027\u80fd\u7684\u6570\u636e\u5e93\u547d\u4e2d\u3002 \u73b0\u5728\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86\u5e03\u9686\u8fc7\u6ee4\u5668\u7684\u9650\u5236\u548c\u4f18\u70b9\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u770b\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u7684\u4e00\u4e9b\u60c5\u51b5\u3002 \u8bbe\u7f6e \u6211\u4eec\u5c06\u4f7f\u7528 Redis \u548c Node.js \u6765\u8bf4\u660e Bloom \u8fc7\u6ee4\u5668\u3002 Redis \u662f Bloom \u8fc7\u6ee4\u5668\u7684\u5b58\u50a8\u4ecb\u8d28\uff1b\u5b83\u901f\u5ea6\u5feb\uff0c\u4f4d\u4e8e\u5185\u5b58\u4e2d\uff0c\u5e76\u4e14\u6709\u4e00\u4e9b\u7279\u5b9a\u7684\u547d\u4ee4\uff08GETBIT\u3001SETBIT\uff09\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5b9e\u65bd\u6548\u7387\u3002\u6211\u5047\u8bbe\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86 Node.js\u3001npm \u548c Redis\u3002\u60a8\u7684 Redis \u670d\u52a1\u5668\u5e94\u8be5\u5728 localhost \u4e0a\u7684\u9ed8\u8ba4\u7aef\u53e3\u4e0a\u8fd0\u884c\uff0c\u4ee5\u4fbf\u6211\u4eec\u7684\u793a\u4f8b\u6b63\u5e38\u5de5\u4f5c\u3002 \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4f1a\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u8fc7\u6ee4\u5668\uff1b\u800c\u662f\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u8fc7\u6ee4\u5668\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u5173\u6ce8 npm \u4e2d\u9884\u6784\u5efa\u6a21\u5757\u7684\u5b9e\u9645\u7528\u9014\uff1abloom-redis\u3002 bloom-redis \u6709\u4e00\u7ec4\u975e\u5e38\u7b80\u6d01\u7684\u65b9\u6cd5\uff1aadd\u3001contains \u548c clear\u3002 \u5982\u524d\u6240\u8ff0\uff0c\u5e03\u9686\u8fc7\u6ee4\u5668\u9700\u8981\u54c8\u5e0c\u7b97\u6cd5\u6765\u751f\u6210\u9879\u76ee\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002 bloom-redis \u4f7f\u7528\u4f17\u6240\u5468\u77e5\u7684 MD5 \u7b97\u6cd5\uff0c\u5c3d\u7ba1\u5b83\u53ef\u80fd\u4e0d\u9002\u5408 Bloom \u8fc7\u6ee4\u5668\uff08\u6709\u70b9\u6162\uff0c\u6709\u70b9\u8fc7\u5927\uff09\uff0c\u4f46\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002 \u72ec\u7279\u7684\u7528\u6237\u540d \u7528\u6237\u540d\uff0c\u5c24\u5176\u662f\u5728 URL \u4e2d\u6807\u8bc6\u7528\u6237\u7684\u7528\u6237\u540d\uff0c\u9700\u8981\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u60a8\u6784\u5efa\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u66f4\u6539\u7528\u6237\u540d\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u90a3\u4e48\u60a8\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u4ece\u672a\u4f7f\u7528\u8fc7\u7684\u7528\u6237\u540d\uff0c\u4ee5\u907f\u514d\u7528\u6237\u540d\u6df7\u6dc6\u548c\u88ab\u653b\u51fb\u3002 \u5982\u679c\u6ca1\u6709\u5e03\u9686\u8fc7\u6ee4\u5668\uff0c\u60a8\u9700\u8981\u5f15\u7528\u4e00\u4e2a\u5305\u542b\u66fe\u7ecf\u4f7f\u7528\u8fc7\u7684\u6bcf\u4e2a\u7528\u6237\u540d\u7684\u8868\uff0c\u800c\u5927\u89c4\u6a21\u65f6\u8fd9\u53ef\u80fd\u4f1a\u975e\u5e38\u6602\u8d35\u3002\u5e03\u9686\u8fc7\u6ee4\u5668\u5141\u8bb8\u60a8\u5728\u7528\u6237\u6bcf\u6b21\u91c7\u7528\u65b0\u540d\u79f0\u65f6\u6dfb\u52a0\u4e00\u4e2a\u9879\u76ee\u3002\u5f53\u7528\u6237\u68c0\u67e5\u7528\u6237\u540d\u662f\u5426\u88ab\u5360\u7528\u65f6\uff0c\u60a8\u6240\u9700\u8981\u505a\u7684\u5c31\u662f\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u5b83\u5c06\u80fd\u591f\u7edd\u5bf9\u786e\u5b9a\u5730\u544a\u8bc9\u60a8\u6240\u8bf7\u6c42\u7684\u7528\u6237\u540d\u662f\u5426\u5148\u524d\u5df2\u6dfb\u52a0\u3002\u8fc7\u6ee4\u5668\u53ef\u80fd\u4f1a\u9519\u8bef\u5730\u8fd4\u56de\u7528\u6237\u540d\u5df2\u88ab\u4f7f\u7528\uff0c\u800c\u5b9e\u9645\u4e0a\u7528\u6237\u540d\u5c1a\u672a\u88ab\u4f7f\u7528\uff0c\u4f46\u8fd9\u53ea\u662f\u4e3a\u4e86\u8c28\u614e\u8d77\u89c1\uff0c\u4e0d\u4f1a\u9020\u6210\u4efb\u4f55\u771f\u6b63\u7684\u4f24\u5bb3\uff08\u9664\u4e86\u7528\u6237\u53ef\u80fd\u65e0\u6cd5\u58f0\u660e\u201ck3w1d00d47\u201d\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32486","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32486"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32486\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}