{"id":161243,"date":"2024-11-04T04:48:22","date_gmt":"2024-11-03T20:48:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/161243\/"},"modified":"2024-11-04T04:48:23","modified_gmt":"2024-11-03T20:48:23","slug":"%e5%9f%ba%e6%96%bc-redis-%e7%9a%84-php-%e6%87%89%e7%94%a8%e6%80%a7%e8%83%bd%e7%9b%a3%e6%8e%a7%ef%bc%88redis-%e7%9b%a3%e6%8e%a7-php%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/161243\/","title":{"rendered":"\u57fa\u65bc Redis \u7684 PHP \u61c9\u7528\u6027\u80fd\u76e3\u63a7\uff08redis \u76e3\u63a7 php\uff09"},"content":{"rendered":"<h1 id=\"%e5%9f%ba%e6%96%bc-redis-%e7%9a%84-php-%e6%87%89%e7%94%a8%e6%80%a7%e8%83%bd%e7%9b%a3%e6%8e%a7%ef%bc%88redis-%e7%9b%a3%e6%8e%a7-php%ef%bc%89-ROKfkxSmUP\">\u57fa\u65bc Redis \u7684 PHP \u61c9\u7528\u6027\u80fd\u76e3\u63a7\uff08redis \u76e3\u63a7 php\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6027\u80fd\u76e3\u63a7\u662f\u78ba\u4fdd\u61c9\u7528\u7a69\u5b9a\u904b\u884c\u7684\u91cd\u8981\u74b0\u7bc0\u3002\u7279\u5225\u662f\u5c0d\u65bc\u4f7f\u7528 PHP \u8a9e\u8a00\u958b\u767c\u7684\u61c9\u7528\uff0c\u5982\u4f55\u6709\u6548\u5730\u76e3\u63a7\u5176\u6027\u80fd\uff0c\u5c24\u5176\u662f\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u90e8\u5206\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u6311\u6230\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u5b58\u5132\u548c\u6aa2\u7d22\u80fd\u529b\uff0c\u4e26\u4e14\u53ef\u4ee5\u7528\u65bc\u6027\u80fd\u76e3\u63a7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u57fa\u65bc Redis \u9032\u884c PHP \u61c9\u7528\u7684\u6027\u80fd\u76e3\u63a7\u3002<\/p>\n<h2 id=\"redis-%e7%b0%a1%e4%bb%8b-ROKfkxSmUP\">Redis \u7c21\u4ecb<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\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\u6578\u64da\u5b58\u53d6\u901f\u5ea6\uff0cRedis \u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u5206\u6790\u7b49\u5834\u666f\u3002\u5c0d\u65bc PHP \u61c9\u7528\u4f86\u8aaa\uff0c\u4f7f\u7528 Redis \u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u4e26\u4e14\u80fd\u5920\u5be6\u73fe\u66f4\u70ba\u7cbe\u7d30\u7684\u6027\u80fd\u76e3\u63a7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-redis-%e9%80%b2%e8%a1%8c%e6%80%a7%e8%83%bd%e7%9b%a3%e6%8e%a7-ROKfkxSmUP\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Redis \u9032\u884c\u6027\u80fd\u76e3\u63a7<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis \u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\uff0c\u9069\u5408\u7528\u65bc\u9700\u8981\u5be6\u6642\u76e3\u63a7\u7684\u5834\u666f\u3002<\/li>\n<li><strong>\u6578\u64da\u7d50\u69cb\u9748\u6d3b\uff1a<\/strong>Redis \u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u6578\u64da\u985e\u578b\u4f86\u5b58\u5132\u6027\u80fd\u6307\u6a19\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis \u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\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%a8-redis-%e7%9b%a3%e6%8e%a7-php-%e6%87%89%e7%94%a8%e6%80%a7%e8%83%bd-ROKfkxSmUP\">\u5982\u4f55\u4f7f\u7528 Redis \u76e3\u63a7 PHP \u61c9\u7528\u6027\u80fd<\/h2>\n<p>\u8981\u5728 PHP \u61c9\u7528\u4e2d\u4f7f\u7528 Redis \u9032\u884c\u6027\u80fd\u76e3\u63a7\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd Redis \u548c\u76f8\u61c9\u7684 PHP \u64f4\u5c55\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-redis-ROKfkxSmUP\">1. \u5b89\u88dd Redis<\/h3>\n<p>\u5728 Linux \u7cfb\u7d71\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Redis\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install redis-server<\/code><\/pre>\n<h3 id=\"2-%e5%ae%89%e8%a3%9d-php-redis-%e6%93%b4%e5%b1%95-ROKfkxSmUP\">2. \u5b89\u88dd PHP Redis \u64f4\u5c55<\/h3>\n<p>\u4f7f\u7528 PECL \u5b89\u88dd PHP Redis \u64f4\u5c55\uff1a<\/p>\n<pre><code>sudo pecl install redis<\/code><\/pre>\n<p>\u7136\u5f8c\u5728 php.ini \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u555f\u7528\u64f4\u5c55\uff1a<\/p>\n<pre><code>extension=redis.so<\/code><\/pre>\n<h3 id=\"3-%e7%9b%a3%e6%8e%a7%e6%80%a7%e8%83%bd%e6%8c%87%e6%a8%99-ROKfkxSmUP\">3. \u76e3\u63a7\u6027\u80fd\u6307\u6a19<\/h3>\n<p>\u5728 PHP \u61c9\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Redis \u5b58\u5132\u6027\u80fd\u6307\u6a19\uff0c\u4f8b\u5982\u8acb\u6c42\u8655\u7406\u6642\u9593\u3001\u6578\u64da\u5eab\u67e5\u8a62\u6b21\u6578\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230 Redis\n$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\n\n\/\/ \u8a08\u7b97\u8acb\u6c42\u8655\u7406\u6642\u9593\n$start_time = microtime(true);\n\/\/ \u6a21\u64ec\u8acb\u6c42\u8655\u7406\nsleep(1); \/\/ \u5047\u8a2d\u8acb\u6c42\u8655\u7406\u9700\u8981 1 \u79d2\n$end_time = microtime(true);\n$request_time = $end_time - $start_time;\n\n\/\/ \u5c07\u6027\u80fd\u6307\u6a19\u5b58\u5132\u5230 Redis\n$redis-&gt;incr('request_count');\n$redis-&gt;set('last_request_time', $request_time);\n?&gt;<\/code><\/pre>\n<h3 id=\"4-%e6%9f%a5%e8%a9%a2%e6%80%a7%e8%83%bd%e6%95%b8%e6%93%9a-ROKfkxSmUP\">4. \u67e5\u8a62\u6027\u80fd\u6578\u64da<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e Redis \u63d0\u4f9b\u7684\u547d\u4ee4\u67e5\u8a62\u6027\u80fd\u6578\u64da\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u7372\u53d6\u8acb\u6c42\u6b21\u6578\n$request_count = $redis-&gt;get('request_count');\n\/\/ \u7372\u53d6\u6700\u5f8c\u4e00\u6b21\u8acb\u6c42\u7684\u8655\u7406\u6642\u9593\n$last_request_time = $redis-&gt;get('last_request_time');\n\necho \"\u8acb\u6c42\u6b21\u6578: \" . $request_count . \"&lt;br&gt;\";\necho \"\u6700\u5f8c\u4e00\u6b21\u8acb\u6c42\u8655\u7406\u6642\u9593: \" . $last_request_time . \" \u79d2&lt;br&gt;\";\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-ROKfkxSmUP\">\u7d50\u8ad6<\/h2>\n<p>\u57fa\u65bc Redis \u7684 PHP \u61c9\u7528\u6027\u80fd\u76e3\u63a7\u4e0d\u50c5\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u53ca\u6642\u767c\u73fe\u6027\u80fd\u74f6\u9838\uff0c\u9084\u80fd\u5920\u63d0\u4f9b\u66f4\u70ba\u7cbe\u78ba\u7684\u6578\u64da\u5206\u6790\u3002\u901a\u904e\u5408\u7406\u5730\u8a2d\u8a08\u6027\u80fd\u6307\u6a19\u7684\u5b58\u5132\u548c\u67e5\u8a62\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u548c\u7528\u6236\u9ad4\u9a57\u3002\u96a8\u8457\u61c9\u7528\u898f\u6a21\u7684\u64f4\u5927\uff0c\u9078\u64c7\u5408\u9069\u7684\u76e3\u63a7\u5de5\u5177\u548c\u6280\u8853\u5c07\u8b8a\u5f97\u6108\u52a0\u91cd\u8981\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u53ca\u5176\u61c9\u7528\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u57fa\u65bc Redis \u7684 PHP \u61c9\u7528\u6027\u80fd\u76e3\u63a7\uff0c\u63d0\u5347\u61c9\u7528\u6548\u80fd\uff0c\u5be6\u6642\u8ffd\u8e64\u548c\u512a\u5316 Redis \u6578\u64da\u5eab\u7684\u6027\u80fd\u6307\u6a19\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-161243","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161243","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=161243"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161243\/revisions"}],"predecessor-version":[{"id":161244,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/161243\/revisions\/161244"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=161243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=161243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=161243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}