{"id":194661,"date":"2024-11-13T08:31:20","date_gmt":"2024-11-13T00:31:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194661\/"},"modified":"2024-11-13T08:31:21","modified_gmt":"2024-11-13T00:31:21","slug":"%e7%94%a8redis%e6%9e%b6%e6%a7%8b%e7%9a%84php%e7%b6%b2%e7%ab%99%ef%bc%8c%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%ef%bc%88redis%e6%9e%b6%e6%a7%8bphp%e7%b6%b2%e7%ab%99%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194661\/","title":{"rendered":"\u7528Redis\u67b6\u69cb\u7684PHP\u7db2\u7ad9\uff0c\u5feb\u901f\u9ad8\u6548\uff08redis\u67b6\u69cbphp\u7db2\u7ad9\uff09"},"content":{"rendered":"<h1 id=\"%e7%94%a8redis%e6%9e%b6%e6%a7%8b%e7%9a%84php%e7%b6%b2%e7%ab%99%ef%bc%8c%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88-YMKSUgTpXF\">\u7528Redis\u67b6\u69cb\u7684PHP\u7db2\u7ad9\uff0c\u5feb\u901f\u9ad8\u6548<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u5011\u9700\u8981\u5c0b\u627e\u66f4\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\u548c\u6578\u64da\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u8d8a\u4f86\u8d8a\u591a\u5730\u88ab\u7528\u65bcPHP\u7db2\u7ad9\u7684\u67b6\u69cb\u4e2d\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u63d0\u5347PHP\u7db2\u7ad9\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-YMKSUgTpXF\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08REmote DIctionary Server\uff09\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\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u662f\u901f\u5ea6\u5feb\u3001\u652f\u6301\u6301\u4e45\u5316\u3001\u4e26\u4e14\u80fd\u5920\u5728\u5167\u5b58\u4e2d\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0c\u9019\u4f7f\u5f97Redis\u6210\u70ba\u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e6%9e%b6%e6%a7%8bphp%e7%b6%b2%e7%ab%99%ef%bc%9f-YMKSUgTpXF\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u67b6\u69cbPHP\u7db2\u7ad9\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u64cd\u4f5c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u6beb\u79d2\u7d1a\u5225\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\u800c\u4e0d\u6703\u9020\u6210\u5ef6\u9072\u3002<\/li>\n<li><strong>\u6578\u64da\u6301\u4e45\u5316\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9019\u6a23\u5373\u4f7f\u5728\u7cfb\u7d71\u5d29\u6f70\u7684\u60c5\u6cc1\u4e0b\uff0c\u6578\u64da\u4e5f\u4e0d\u6703\u4e1f\u5931\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684API\u8a2d\u8a08\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\uff0c\u4e26\u4e14\u6709\u8c50\u5bcc\u7684\u6587\u6a94\u548c\u793e\u5340\u652f\u6301\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u9748\u6d3b\u5730\u61c9\u5c0d\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8redis%ef%bc%9f-YMKSUgTpXF\">\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528Redis\uff1f<\/h2>\n<p>\u8981\u5728PHP\u4e2d\u4f7f\u7528Redis\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddRedis\u670d\u52d9\u5668\u548cPHP\u7684Redis\u64f4\u5c55\u3002\u4ee5\u4e0b\u662f\u5b89\u88dd\u548c\u914d\u7f6e\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis%e6%9c%8d%e5%8b%99%e5%99%a8-YMKSUgTpXF\">1. \u5b89\u88ddRedis\u670d\u52d9\u5668<\/h3>\n<pre><code>sudo apt-get update\nsudo apt-get install redis-server\n<\/code><\/pre>\n<h3 id=\"2-%e5%ae%89%e8%a3%9dphp-redis%e6%93%b4%e5%b1%95-YMKSUgTpXF\">2. \u5b89\u88ddPHP Redis\u64f4\u5c55<\/h3>\n<pre><code>sudo apt-get install php-redis\n<\/code><\/pre>\n<h3 id=\"3-%e9%85%8d%e7%bd%aeredis-YMKSUgTpXF\">3. \u914d\u7f6eRedis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u901a\u904e\u7de8\u8f2fRedis\u7684\u914d\u7f6e\u6587\u4ef6\u4f86\u8abf\u6574\u8a2d\u7f6e\uff0c\u901a\u5e38\u4f4d\u65bc \/etc\/redis\/redis.conf\u3002\u53ef\u4ee5\u6839\u64da\u9700\u8981\u4fee\u6539\u7aef\u53e3\u3001\u5167\u5b58\u9650\u5236\u7b49\u53c3\u6578\u3002<\/p>\n<h3 id=\"4-%e5%9c%a8php%e4%b8%ad%e9%80%a3%e6%8e%a5redis-YMKSUgTpXF\">4. \u5728PHP\u4e2d\u9023\u63a5Redis<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728PHP\u4e2d\u9023\u63a5Redis\u4e26\u9032\u884c\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u5275\u5efaRedis\u5c0d\u8c61\n$redis = new Redis();\n\n\/\/ \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\n$redis-&gt;connect('127.0.0.1', 6379);\n\n\/\/ \u8a2d\u7f6e\u4e00\u500b\u9375\u503c\u5c0d\n$redis-&gt;set('key', 'value');\n\n\/\/ \u7372\u53d6\u9375\u7684\u503c\n$value = $redis-&gt;get('key');\necho $value; \/\/ \u8f38\u51fa: value\n?&gt;\n<\/code><\/pre>\n<h2 id=\"redis%e5%9c%a8php%e7%b6%b2%e7%ab%99%e4%b8%ad%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-YMKSUgTpXF\">Redis\u5728PHP\u7db2\u7ad9\u4e2d\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<ul>\n<li><strong>\u7de9\u5b58\uff1a<\/strong>\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u5c64\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7db2\u7ad9\u7684\u97ff\u61c9\u901f\u5ea6\uff0c\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u6703\u8a71\u7ba1\u7406\uff1a<\/strong>Redis\u53ef\u4ee5\u7528\u4f86\u5b58\u5132\u7528\u6236\u6703\u8a71\u4fe1\u606f\uff0c\u652f\u6301\u9ad8\u4e26\u767c\u7684\u7528\u6236\u8a2a\u554f\u3002<\/li>\n<li><strong>\u6d88\u606f\u968a\u5217\uff1a<\/strong>\u5229\u7528Redis\u7684\u5217\u8868\u7d50\u69cb\uff0c\u53ef\u4ee5\u5be6\u73fe\u7c21\u55ae\u7684\u6d88\u606f\u968a\u5217\u7cfb\u7d71\uff0c\u9069\u5408\u8655\u7406\u7570\u6b65\u4efb\u52d9\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YMKSUgTpXF\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u67b6\u69cb\u7684PHP\u7db2\u7ad9\u80fd\u5920\u986f\u8457\u63d0\u5347\u6027\u80fd\u548c\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e2d\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u7f6e\u548c\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eeRedis\u7684\u512a\u52e2\uff0c\u70ba\u7528\u6236\u63d0\u4f9b\u66f4\u5feb\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u66f4\u597d\u7684\u4f7f\u7528\u9ad4\u9a57\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u7db2\u7ad9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u642d\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u7528Redis\u67b6\u69cb\u7684PHP\u7db2\u7ad9\uff0c\u5be6\u73fe\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\uff0c\u512a\u5316\u7db2\u7ad9\u6027\u80fd\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-194661","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194661","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=194661"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194661\/revisions"}],"predecessor-version":[{"id":194662,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194661\/revisions\/194662"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}