{"id":167111,"date":"2024-11-05T22:27:24","date_gmt":"2024-11-05T14:27:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167111\/"},"modified":"2024-11-05T22:27:25","modified_gmt":"2024-11-05T14:27:25","slug":"%e5%88%a9%e7%94%a8redis%e6%9f%a5%e7%9c%8b%e5%af%a6%e6%99%82%e6%97%a5%e8%aa%8c%ef%bc%88redis%e6%9f%a5%e7%9c%8b%e5%af%a6%e6%99%82%e6%97%a5%e8%aa%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167111\/","title":{"rendered":"\u5229\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\uff08redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e6%9f%a5%e7%9c%8b%e5%af%a6%e6%99%82%e6%97%a5%e8%aa%8c-cQHWZasyLy\">\u5229\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u5be6\u6642\u65e5\u8a8c\u7684\u76e3\u63a7\u548c\u5206\u6790\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u512a\u5316\u548c\u6545\u969c\u6392\u9664\u81f3\u95dc\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u5b58\u5132\uff0c\u9084\u53ef\u4ee5\u7528\u4f86\u5be6\u6642\u67e5\u770b\u548c\u7ba1\u7406\u65e5\u8a8c\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-cQHWZasyLy\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u8b80\u5beb\u6027\u80fd\u548c\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e6%9f%a5%e7%9c%8b%e5%af%a6%e6%99%82%e6%97%a5%e8%aa%8c%ef%bc%9f-cQHWZasyLy\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u6578\u64da\uff0c\u9069\u5408\u9700\u8981\u5be6\u6642\u8655\u7406\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u6578\u64da\u7d50\u69cb\u9748\u6d3b\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u65b9\u5f0f\u4f86\u5b58\u5132\u65e5\u8a8c\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u9078\u9805\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u670d\u52d9\u5668\u91cd\u555f\u800c\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e6%9f%a5%e7%9c%8b%e5%af%a6%e6%99%82%e6%97%a5%e8%aa%8c-cQHWZasyLy\">\u5982\u4f55\u4f7f\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5229\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis-cQHWZasyLy\">1. \u5b89\u88ddRedis<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddRedis\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728Ubuntu\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install redis-server<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%aeredis-cQHWZasyLy\">2. \u914d\u7f6eRedis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u7de8\u8f2fRedis\u914d\u7f6e\u6587\u4ef6\u4f86\u9032\u884c\u57fa\u672c\u914d\u7f6e\u3002\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/etc\/redis\/redis.conf\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u8abf\u6574\u8a2d\u7f6e\uff0c\u4f8b\u5982\u8a2d\u7f6e\u5bc6\u78bc\u3001\u6301\u4e45\u5316\u9078\u9805\u7b49\u3002<\/p>\n<h3 id=\"3-%e5%af%a6%e6%99%82%e5%af%ab%e5%85%a5%e6%97%a5%e8%aa%8c-cQHWZasyLy\">3. \u5be6\u6642\u5beb\u5165\u65e5\u8a8c<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u5217\u8868\u6578\u64da\u7d50\u69cb\u4f86\u5b58\u5132\u65e5\u8a8c\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07\u65e5\u8a8c\u5beb\u5165Redis\uff1a<\/p>\n<pre><code>import redis\nimport time\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5beb\u5165\u65e5\u8a8c\nwhile True:\n    log_message = f\"\u65e5\u8a8c\u4fe1\u606f\uff1a{time.ctime()}\"\n    r.lpush('logs', log_message)\n    time.sleep(1)<\/code><\/pre>\n<h3 id=\"4-%e5%af%a6%e6%99%82%e6%9f%a5%e7%9c%8b%e6%97%a5%e8%aa%8c-cQHWZasyLy\">4. \u5be6\u6642\u67e5\u770b\u65e5\u8a8c<\/h3>\n<p>\u8981\u5be6\u6642\u67e5\u770b\u65e5\u8a8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u8a02\u95b1\/\u767c\u5e03\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8a02\u95b1\u65e5\u8a8c\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a02\u95b1\u65e5\u8a8c\np = r.pubsub()\np.subscribe('logs')\n\nfor message in p.listen():\n    if message['type'] == 'message':\n        print(message['data'].decode('utf-8'))<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cQHWZasyLy\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904eRedis\u7684\u9ad8\u6027\u80fd\u548c\u591a\u6a23\u7684\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u65e5\u8a8c\u7684\u5beb\u5165\u548c\u67e5\u770b\u529f\u80fd\u3002\u7121\u8ad6\u662f\u7528\u65bc\u6545\u969c\u6392\u9664\u9084\u662f\u6027\u80fd\u76e3\u63a7\uff0cRedis\u90fd\u80fd\u63d0\u4f9b\u5373\u6642\u7684\u6578\u64da\u652f\u6301\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u904b\u884c\u60a8\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u89e3\u6c7a\u65b9\u6848\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528Redis\u67e5\u770b\u5be6\u6642\u65e5\u8a8c\uff0c\u5feb\u901f\u7372\u53d6\u548c\u5206\u6790\u65e5\u8a8c\u6578\u64da\uff0c\u63d0\u5347\u7cfb\u7d71\u76e3\u63a7\u6548\u7387\uff0c\u5be6\u73fe\u5373\u6642\u53cd\u994b\u8207\u554f\u984c\u6392\u67e5\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-167111","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167111","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=167111"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167111\/revisions"}],"predecessor-version":[{"id":167112,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167111\/revisions\/167112"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}