{"id":3747,"date":"2025-05-28T23:48:56","date_gmt":"2025-05-28T15:48:56","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3747"},"modified":"2025-05-28T23:48:56","modified_gmt":"2025-05-28T15:48:56","slug":"%e7%bb%93%e5%90%88-php%e3%80%81mysql-%e5%92%8c-redis-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%bc%93%e5%ad%98%e5%8a%a0%e9%80%9f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/05\/28\/%e7%bb%93%e5%90%88-php%e3%80%81mysql-%e5%92%8c-redis-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%bc%93%e5%ad%98%e5%8a%a0%e9%80%9f%ef%bc%9f\/","title":{"rendered":"\u7ed3\u5408 PHP\u3001MySQL \u548c Redis \u5982\u4f55\u5b9e\u73b0\u7f13\u5b58\u52a0\u901f\uff1f"},"content":{"rendered":"\n<p>\u7ed3\u5408 PHP\u3001MySQL \u548c Redis \u5b9e\u73b0 <strong>\u7f13\u5b58\u52a0\u901f<\/strong> \u662f Web \u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6848\u3002\u6838\u5fc3\u601d\u60f3\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5148\u67e5 Redis \u7f13\u5b58<\/strong><\/li>\n\n\n\n<li><strong>\u7f13\u5b58\u547d\u4e2d\u5219\u76f4\u63a5\u8fd4\u56de<\/strong><\/li>\n\n\n\n<li><strong>\u672a\u547d\u4e2d\u5219\u67e5 MySQL \u2192 \u5b58\u5165 Redis \u2192 \u8fd4\u56de\u6570\u636e<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u4ece<strong>\u5b9e\u6218\u89d2\u5ea6<\/strong>\u6765\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\uff0c\u5e76\u4e14\u9488\u5bf9\u5b9e\u73b0\u4ee3\u7801\u3001\u67b6\u6784\u5efa\u8bae\u3001\u7f13\u5b58\u7b56\u7565\u3001\u6ce8\u610f\u4e8b\u9879\u7b49\u7b80\u5355\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6574\u4f53\u67b6\u6784\u56fe<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;\u5ba2\u6237\u7aef\u8bf7\u6c42]\n      \u2193\n&#91;PHP \u811a\u672c]\n      \u2193\n\u68c0\u67e5 Redis \u7f13\u5b58 \u2192 \u547d\u4e2d\uff1f\u2192 \u662f \u2192 \u8fd4\u56de\n                      \u2193\n                   \u5426\n                      \u2193\n              \u67e5\u8be2 MySQL \u6570\u636e\u5e93\n                      \u2193\n         \u5c06\u6570\u636e\u5199\u5165 Redis \u7f13\u5b58\uff08\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff09\n                      \u2193\n                   \u8fd4\u56de\u6570\u636e<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u73af\u5883\u51c6\u5907<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u6269\u5c55<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5 PHP Redis \u6269\u5c55\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Ubuntu\/Debian\nsudo apt install php-redis\n\n# macOS\uff08\u4f7f\u7528 brew\uff09\nbrew install php@8.1-redis<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5 Redis \u670d\u52a1\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install redis\nsudo systemctl start redis<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u786e\u4fdd\u8fde\u63a5\u6210\u529f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\necho $redis-&gt;ping();  \/\/ \u8f93\u51fa PONG \u8868\u793a\u8fde\u63a5\u6210\u529f<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001PHP+MySQL+Redis \u7f13\u5b58\u793a\u4f8b\u4ee3\u7801<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f\uff1a\u67e5\u8be2\u7528\u6237\u4fe1\u606f\u5e76\u7f13\u5b58 60 \u79d2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u8fde\u63a5 Redis\n$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\n\n\/\/ \u5047\u8bbe\u6211\u4eec\u8981\u67e5\u8be2 ID \u4e3a 1 \u7684\u7528\u6237\u4fe1\u606f\n$userId = 1;\n$cacheKey = \"user:$userId\";\n\n\/\/ 1. \u68c0\u67e5\u7f13\u5b58\nif ($redis-&gt;exists($cacheKey)) {\n    \/\/ \u4ece Redis \u83b7\u53d6\u6570\u636e\n    $userData = json_decode($redis-&gt;get($cacheKey), true);\n    echo \"\u6765\u81ea\u7f13\u5b58\u7684\u6570\u636e\uff1a\\n\";\n} else {\n    \/\/ 2. \u8fde\u63a5\u6570\u636e\u5e93\n    $pdo = new PDO(\"mysql:host=localhost;dbname=testdb\", \"root\", \"password\");\n    $stmt = $pdo-&gt;prepare(\"SELECT id, name, email FROM users WHERE id = ?\");\n    $stmt-&gt;execute(&#91;$userId]);\n    $userData = $stmt-&gt;fetch(PDO::FETCH_ASSOC);\n\n    \/\/ 3. \u5b58\u5165\u7f13\u5b58\uff08\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a60\u79d2\uff09\n    if ($userData) {\n        $redis-&gt;setex($cacheKey, 60, json_encode($userData));\n    }\n\n    echo \"\u6765\u81ea\u6570\u636e\u5e93\u7684\u6570\u636e\uff1a\\n\";\n}\n\n\/\/ \u8f93\u51fa\u6570\u636e\nprint_r($userData);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u7f13\u5b58\u7b56\u7565\u5efa\u8bae<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7b56\u7565<\/th><th>\u5efa\u8bae\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>\u7f13\u5b58\u952e\u8bbe\u8ba1<\/td><td>\u4f7f\u7528\u7ed3\u6784\u5316 key\uff0c\u5982\uff1a<code>user:{id}<\/code>\uff0c<code>article:{id}<\/code><\/td><\/tr><tr><td>\u8fc7\u671f\u65f6\u95f4<\/td><td>\u5e38\u89c4\u6570\u636e 60-300 \u79d2\uff0c\u70ed\u70b9\u6570\u636e\u53ef\u5ef6\u957f<\/td><\/tr><tr><td>\u7f13\u5b58\u5931\u6548<\/td><td>\u4f7f\u7528 <code>setex()<\/code> \u6216 <code>expire()<\/code> \u660e\u786e\u8bbe\u5b9a<\/td><\/tr><tr><td>\u7f13\u5b58\u96ea\u5d29<\/td><td>\u7ed9\u6bcf\u4e2a key \u8bbe\u7f6e<strong>\u4e0d\u540c\u7684\u8fc7\u671f\u65f6\u95f4<\/strong>\uff08\u5982\u52a0\u968f\u673a\u6570\uff09<\/td><\/tr><tr><td>\u7f13\u5b58\u7a7f\u900f<\/td><td>\u67e5\u8be2\u65e0\u7ed3\u679c\u4e5f\u7f13\u5b58\u4e00\u6bb5\u65f6\u95f4\uff08\u9632\u6b62\u53cd\u590d\u51fb\u7a7f\uff09<\/td><\/tr><tr><td>\u7f13\u5b58\u66f4\u65b0<\/td><td>\u5199\u6570\u636e\u5e93\u6210\u529f\u540e\uff0c\u66f4\u65b0\u7f13\u5b58\u6216\u5ef6\u8fdf\u5f02\u6b65\u66f4\u65b0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6269\u5c55\uff1a\u66f4\u65b0\u7f13\u5b58\u7b56\u7565\uff08\u5199\u540e\u6e05\u7f13\u5b58\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u66f4\u65b0\u7528\u6237\u4fe1\u606f\u540e\u6e05\u9664\u7f13\u5b58\n$stmt = $pdo-&gt;prepare(\"UPDATE users SET name = ? WHERE id = ?\");\n$stmt-&gt;execute(&#91;\"\u65b0\u540d\u5b57\", $userId]);\n\n\/\/ \u5220\u9664\u7f13\u5b58\n$redis-&gt;del(\"user:$userId\");<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6027\u80fd\u4f18\u5316\u70b9<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4f18\u5316\u70b9<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>\u4f7f\u7528\u8fde\u63a5\u6c60<\/td><td>\u5bf9\u4e8e Redis\u3001MySQL \u90fd\u5e94\u4f7f\u7528\u8fde\u63a5\u6c60\uff08\u5982 Swoole\/FPM\uff09<\/td><\/tr><tr><td>\u70ed\u70b9\u6570\u636e\u6c38\u4e45\u7f13\u5b58<\/td><td>\u8bbe\u7f6e\u9ad8\u4f18\u5148\u7ea7\u6570\u636e\u4e0d\u6e05\u9664<\/td><\/tr><tr><td>\u589e\u52a0\u5f02\u6b65\u66f4\u65b0\/\u9884\u70ed\u673a\u5236<\/td><td>\u907f\u514d\u9ad8\u5e76\u53d1\u4e0b\u7f13\u5b58\u7a7f\u900f<\/td><\/tr><tr><td>\u4f7f\u7528 JSON \u7f16\u7801\u4f18\u5316\u6570\u636e\u7ed3\u6784<\/td><td>\u5b58\u50a8\u590d\u6742\u6570\u636e\u63a8\u8350\u4f7f\u7528 <code>json_encode()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5b98\u65b9\u4e0e\u5b9e\u7528\u94fe\u63a5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\ud83d\udcd8 <a href=\"https:\/\/www.php.net\/manual\/zh\/book.redis.php\">PHP Redis \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/developer.aliyun.com\/article\/735795\">MySQL + Redis \u7f13\u5b58\u6a21\u5f0f\u5b9e\u8df5 &#8211; \u963f\u91cc\u4e91\u5f00\u53d1\u6307\u5357<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/juejin.cn\/post\/6844903877423331342\">Redis \u7f13\u5b58\u7b56\u7565\u603b\u7ed3\uff08\u6398\u91d1\uff09<\/a><\/li>\n\n\n\n<li>\ud83d\udcd8 <a href=\"https:\/\/www.cnblogs.com\/mazong1122\/p\/16067093.html\">\u9ad8\u5e76\u53d1\u4e0b\u7f13\u5b58\u7a7f\u900f\/\u96ea\u5d29\/\u51fb\u7a7f\u8be6\u89e3<\/a><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9879\u76ee<\/th><th>\u662f\u5426\u63a8\u8350<\/th><\/tr><\/thead><tbody><tr><td>\u5c0f\u6d41\u91cf\u9879\u76ee<\/td><td>\u2705 \u53ef\u7528\u7b80\u5355\u65b9\u5f0f\u5982\u4e0a\u6240\u8ff0<\/td><\/tr><tr><td>\u4e2d\u5927\u578b\u9879\u76ee<\/td><td>\u2705 \u5efa\u8bae\u7ed3\u5408\u961f\u5217\u3001\u7f13\u5b58\u9884\u70ed\u3001\u5206\u5e03\u5f0f\u9501\u7b49<\/td><\/tr><tr><td>\u9ad8\u5e76\u53d1\u9879\u76ee<\/td><td>\u2705 \u5e94\u52a0\u5165\u7f13\u5b58\u4e00\u81f4\u6027\u63a7\u5236\u673a\u5236\uff08\u5982\u5ef6\u8fdf\u53cc\u5220\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed3\u5408 PHP\u3001MySQL \u548c Redis \u5b9e\u73b0 \u7f13\u5b58\u52a0\u901f \u662f Web \u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u6027\u80fd\u4f18\u5316\u65b9\u6848\u3002\u6838\u5fc3\u601d\u60f3\u662f\uff1a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3748,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-3747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3747"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3747\/revisions"}],"predecessor-version":[{"id":3749,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3747\/revisions\/3749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3748"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}