{"id":175117,"date":"2024-11-08T02:52:35","date_gmt":"2024-11-07T18:52:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175117\/"},"modified":"2024-11-08T02:52:36","modified_gmt":"2024-11-07T18:52:36","slug":"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e8%bc%95%e9%87%8f%e7%b4%9aredis%e6%9c%8d%e5%8b%99%ef%bc%88%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e7%b0%a1%e5%8c%96%e7%89%88redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175117\/","title":{"rendered":"\u5982\u4f55\u5be6\u73fe\u8f15\u91cf\u7d1aRedis\u670d\u52d9\uff08\u5982\u4f55\u5be6\u73fe\u7c21\u5316\u7248redis\uff09"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e8%bc%95%e9%87%8f%e7%b4%9aredis%e6%9c%8d%e5%8b%99%ef%bc%88%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%be%e7%b0%a1%e5%8c%96%e7%89%88redis%ef%bc%89-fEAwtISCmy\">\u5982\u4f55\u5be6\u73fe\u8f15\u91cf\u7d1aRedis\u670d\u52d9\uff08\u5982\u4f55\u5be6\u73fe\u7c21\u5316\u7248redis\uff09<\/h1>\n<p>Redis\u662f\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u5834\u666f\u3002\u5118\u7ba1Redis\u529f\u80fd\u5f37\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u4e00\u500b\u66f4\u8f15\u91cf\u7d1a\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u4ee5\u6e1b\u5c11\u8cc7\u6e90\u6d88\u8017\u548c\u63d0\u9ad8\u6027\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5be6\u73fe\u4e00\u500b\u7c21\u5316\u7248\u7684Redis\u670d\u52d9\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e8%bc%95%e9%87%8f%e7%b4%9aredis%e7%9a%84%e9%9c%80%e6%b1%82-fEAwtISCmy\">\u8f15\u91cf\u7d1aRedis\u7684\u9700\u6c42<\/h2>\n<p>\u5728\u67d0\u4e9b\u61c9\u7528\u5834\u666f\u4e2d\uff0c\u4f7f\u7528\u5b8c\u6574\u7684Redis\u670d\u52d9\u53ef\u80fd\u6703\u5c0e\u81f4\u4e0d\u5fc5\u8981\u7684\u8cc7\u6e90\u6d6a\u8cbb\u3002\u4f8b\u5982\uff0c\u5728\u5c0f\u578b\u61c9\u7528\u6216\u958b\u767c\u74b0\u5883\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u80fd\u53ea\u9700\u8981\u57fa\u672c\u7684\u9375\u503c\u5b58\u5132\u529f\u80fd\uff0c\u800c\u4e0d\u9700\u8981Redis\u7684\u6240\u6709\u9ad8\u7d1a\u7279\u6027\u3002\u9019\u6642\uff0c\u5be6\u73fe\u4e00\u500b\u8f15\u91cf\u7d1a\u7684Redis\u670d\u52d9\u5c31\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%b0%a1%e5%8c%96%e7%89%88redis%e7%9a%84%e8%a8%ad%e8%a8%88%e6%80%9d%e8%b7%af-fEAwtISCmy\">\u7c21\u5316\u7248Redis\u7684\u8a2d\u8a08\u601d\u8def<\/h2>\n<p>\u8981\u5be6\u73fe\u4e00\u500b\u7c21\u5316\u7248\u7684Redis\u670d\u52d9\uff0c\u6211\u5011\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u7bc4\u570d\uff1a<\/strong>\u50c5\u5be6\u73fe\u57fa\u672c\u7684\u9375\u503c\u5b58\u5132\u548c\u6aa2\u7d22\u529f\u80fd\uff0c\u7701\u7565\u9ad8\u7d1a\u529f\u80fd\u5982\u6301\u4e45\u5316\u3001\u8a02\u95b1\/\u767c\u5e03\u7b49\u3002<\/li>\n<li><strong>\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u4f7f\u7528\u7c21\u55ae\u7684\u6578\u64da\u7d50\u69cb\u4f86\u5b58\u5132\u9375\u503c\u5c0d\uff0c\u4f8b\u5982\u4f7f\u7528Python\u7684\u5b57\u5178\u3002<\/li>\n<li><strong>\u7db2\u7d61\u901a\u4fe1\uff1a<\/strong>\u4f7f\u7528\u7c21\u55ae\u7684TCP\/IP\u5354\u8b70\u4f86\u5be6\u73fe\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u4e4b\u9593\u7684\u901a\u4fe1\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e7%b0%a1%e5%8c%96%e7%89%88redis%e7%9a%84%e7%a4%ba%e4%be%8b-fEAwtISCmy\">\u5be6\u73fe\u7c21\u5316\u7248Redis\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Python\u5be6\u73fe\u7684\u7c21\u5316\u7248Redis\u670d\u52d9\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\nimport socket\nimport threading\n\nclass SimpleRedis:\n    def __init__(self):\n        self.store = {}\n\n    def set(self, key, value):\n        self.store[key] = value\n        return \"OK\"\n\n    def get(self, key):\n        return self.store.get(key, None)\n\ndef handle_client(client_socket):\n    redis = SimpleRedis()\n    while True:\n        request = client_socket.recv(1024).decode('utf-8')\n        if not request:\n            break\n        command, *args = request.split()\n        if command == \"SET\":\n            response = redis.set(args[0], args[1])\n        elif command == \"GET\":\n            response = redis.get(args[0])\n        else:\n            response = \"ERROR: Unknown command\"\n        client_socket.send(response.encode('utf-8'))\n    client_socket.close()\n\ndef main():\n    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server.bind(('0.0.0.0', 6379))\n    server.listen(5)\n    print(\"Listening on port 6379...\")\n    while True:\n        client_socket, addr = server.accept()\n        print(f\"Accepted connection from {addr}\")\n        client_handler = threading.Thread(target=handle_client, args=(client_socket,))\n        client_handler.start()\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u7c21\u55ae\u7684Redis\u670d\u52d9\uff0c\u652f\u6301\u57fa\u672c\u7684SET\u548cGET\u547d\u4ee4\u3002\u5ba2\u6236\u7aef\u53ef\u4ee5\u901a\u904eTCP\u9023\u63a5\u5230\u670d\u52d9\u5668\uff0c\u4e26\u767c\u9001\u547d\u4ee4\u4f86\u64cd\u4f5c\u6578\u64da\u3002<\/p>\n<h2 id=\"%e6%80%a7%e8%83%bd%e8%80%83%e9%87%8f-fEAwtISCmy\">\u6027\u80fd\u8003\u91cf<\/h2>\n<p>\u96d6\u7136\u9019\u500b\u7c21\u5316\u7248\u7684Redis\u670d\u52d9\u5728\u529f\u80fd\u4e0a\u6709\u6240\u9650\u5236\uff0c\u4f46\u5b83\u5728\u6027\u80fd\u4e0a\u537b\u80fd\u5920\u63d0\u4f9b\u66f4\u5feb\u7684\u97ff\u61c9\u6642\u9593\u548c\u66f4\u4f4e\u7684\u8cc7\u6e90\u6d88\u8017\u3002\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u5c0f\u578b\u61c9\u7528\u6216\u958b\u767c\u74b0\u5883\u4e2d\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-fEAwtISCmy\">\u7d50\u8ad6<\/h2>\n<p>\u5be6\u73fe\u4e00\u500b\u8f15\u91cf\u7d1a\u7684Redis\u670d\u52d9\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u7279\u5b9a\u5834\u666f\u4e0b\u63d0\u9ad8\u6027\u80fd\u548c\u964d\u4f4e\u8cc7\u6e90\u6d88\u8017\u3002\u901a\u904e\u7c21\u5316\u529f\u80fd\u7bc4\u570d\u548c\u4f7f\u7528\u57fa\u672c\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6211\u5011\u53ef\u4ee5\u5feb\u901f\u69cb\u5efa\u4e00\u500b\u6eff\u8db3\u9700\u6c42\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\u3002\u5c0d\u65bc\u9700\u8981\u66f4\u9ad8\u6027\u80fd\u548c\u66f4\u4f4e\u8cc7\u6e90\u6d88\u8017\u7684\u61c9\u7528\uff0c\u9019\u6a23\u7684\u89e3\u6c7a\u65b9\u6848\u7121\u7591\u662f\u503c\u5f97\u8003\u616e\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u3001<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5be6\u73fe\u8f15\u91cf\u7d1aRedis\u670d\u52d9\uff0c\u7c21\u5316\u7248Redis\u7684\u8a2d\u8a08\u8207\u5be6\u73fe\uff0c\u63d0\u5347\u6027\u80fd\u8207\u8cc7\u6e90\u5229\u7528\u6548\u7387\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-175117","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175117","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=175117"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175117\/revisions"}],"predecessor-version":[{"id":175118,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175117\/revisions\/175118"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}