{"id":195445,"date":"2024-11-13T15:03:21","date_gmt":"2024-11-13T07:03:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/195445\/"},"modified":"2024-11-13T15:03:23","modified_gmt":"2024-11-13T07:03:23","slug":"%e8%88%87php%e7%b5%90%e5%90%88%e5%af%a6%e7%8f%be%e6%9b%b4%e9%ab%98%e6%80%a7%e8%83%bdredis%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%88redis%e7%9a%84%e4%bd%bf%e7%94%a8php%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/195445\/","title":{"rendered":"\u8207PHP\u7d50\u5408\u5be6\u73fe\u66f4\u9ad8\u6027\u80fdRedis\u7684\u4f7f\u7528\uff08redis\u7684\u4f7f\u7528php\uff09"},"content":{"rendered":"<h1 id=\"%e8%88%87php%e7%b5%90%e5%90%88%e5%af%a6%e7%8f%be%e6%9b%b4%e9%ab%98%e6%80%a7%e8%83%bdredis%e7%9a%84%e4%bd%bf%e7%94%a8-OFLOkjDOiQ\">\u8207PHP\u7d50\u5408\u5be6\u73fe\u66f4\u9ad8\u6027\u80fdRedis\u7684\u4f7f\u7528<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u9700\u8981\u5c0b\u627e\u66f4\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5c07Redis\u8207PHP\u7d50\u5408\uff0c\u4ee5\u5be6\u73fe\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-OFLOkjDOiQ\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08REmote DIctionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\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\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6027\u80fd\uff1aRedis\u80fd\u5920\u4ee5\u6beb\u79d2\u7d1a\u7684\u5ef6\u9072\u8655\u7406\u6578\u64da\u8acb\u6c42\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1a\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a\u4e0d\u50c5\u50c5\u662f\u9375\u503c\u5c0d\uff0c\u9084\u652f\u6301\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002<\/li>\n<li>\u5206\u4f48\u5f0f\uff1a\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u9069\u5408\u5927\u898f\u6a21\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87php%e8%88%87redis%e7%b5%90%e5%90%88%ef%bc%9f-OFLOkjDOiQ\">\u70ba\u4ec0\u9ebc\u9078\u64c7PHP\u8207Redis\u7d50\u5408\uff1f<\/h2>\n<p>PHP\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u670d\u52d9\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bcWeb\u958b\u767c\u3002\u5c07PHP\u8207Redis\u7d50\u5408\uff0c\u53ef\u4ee5\u5145\u5206\u767c\u63ee\u5169\u8005\u7684\u512a\u52e2\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u4e3b\u8981\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u5feb\u901f\u6578\u64da\u5b58\u53d6\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8PHP\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u67e5\u8a62\u6b21\u6578\uff0c\u5f9e\u800c\u964d\u4f4e\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u7c21\u5316\u6578\u64da\u64cd\u4f5c\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-OFLOkjDOiQ\">\u5982\u4f55\u5728PHP\u4e2d\u4f7f\u7528Redis<\/h2>\n<p>\u8981\u5728PHP\u4e2d\u4f7f\u7528Redis\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddRedis\u64f4\u5c55\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo pecl install redis<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u9700\u5728php.ini\u4e2d\u555f\u7528Redis\u64f4\u5c55\uff1a<\/p>\n<pre><code>extension=redis.so<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230Redis\u670d\u52d9\u5668\uff1a<\/p>\n<pre><code>&lt;?php\n$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\n?&gt;<\/code><\/pre>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b-OFLOkjDOiQ\">\u57fa\u672c\u64cd\u4f5c\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684Redis\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h4 id=\"%e8%a8%ad%e7%bd%ae%e5%92%8c%e7%8d%b2%e5%8f%96%e5%80%bc-OFLOkjDOiQ\">\u8a2d\u7f6e\u548c\u7372\u53d6\u503c<\/h4>\n<pre><code>&lt;?php\n$redis-&gt;set('key', 'value');\n$value = $redis-&gt;get('key');\necho $value; \/\/ \u8f38\u51fa: value\n?&gt;<\/code><\/pre>\n<h4 id=\"%e4%bd%bf%e7%94%a8%e5%93%88%e5%b8%8c-OFLOkjDOiQ\">\u4f7f\u7528\u54c8\u5e0c<\/h4>\n<pre><code>&lt;?php\n$redis-&gt;hSet('user:1000', 'name', 'John');\n$redis-&gt;hSet('user:1000', 'age', 30);\n$user = $redis-&gt;hGetAll('user:1000');\nprint_r($user); \/\/ \u8f38\u51fa: Array ( [name] =&gt; John [age] =&gt; 30 )\n?&gt;<\/code><\/pre>\n<h4 id=\"%e5%88%97%e8%a1%a8%e6%93%8d%e4%bd%9c-OFLOkjDOiQ\">\u5217\u8868\u64cd\u4f5c<\/h4>\n<pre><code>&lt;?php\n$redis-&gt;lPush('mylist', 'item1');\n$redis-&gt;lPush('mylist', 'item2');\n$items = $redis-&gt;lRange('mylist', 0, -1);\nprint_r($items); \/\/ \u8f38\u51fa: Array ( [0] =&gt; item2 [1] =&gt; item1 )\n?&gt;<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-OFLOkjDOiQ\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u6642\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li><strong>\u5408\u7406\u8a2d\u8a08\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9023\u63a5\u6c60\uff1a<\/strong>\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e0b\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u6e1b\u5c11\u9023\u63a5\u5efa\u7acb\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u5b9a\u671f\u6e05\u7406\u904e\u671f\u6578\u64da\uff1a<\/strong>\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff0c\u5b9a\u671f\u6e05\u7406\u4e0d\u518d\u9700\u8981\u7684\u6578\u64da\uff0c\u4ee5\u91cb\u653e\u5167\u5b58\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OFLOkjDOiQ\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u5c07Redis\u8207PHP\u7d50\u5408\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u8a2a\u554f\uff0c\u986f\u8457\u63d0\u5347\u61c9\u7528\u6027\u80fd\u3002\u7121\u8ad6\u662f\u7de9\u5b58\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\uff0c\u9084\u662f\u8655\u7406\u9ad8\u4e26\u767c\u8acb\u6c42\uff0cRedis\u90fd\u80fd\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6027\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684Redis\u670d\u52d9\u5668\uff0c\u5f9e\u800c\u7372\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5c07PHP\u8207Redis\u7d50\u5408\uff0c\u5be6\u73fe\u66f4\u9ad8\u6027\u80fd\u7684\u6578\u64da\u8655\u7406\uff0c\u63d0\u5347\u61c9\u7528\u6548\u80fd\u8207\u97ff\u61c9\u901f\u5ea6\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-195445","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195445","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=195445"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195445\/revisions"}],"predecessor-version":[{"id":195446,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/195445\/revisions\/195446"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=195445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=195445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=195445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}