{"id":22452,"date":"2024-11-21T12:01:24","date_gmt":"2024-11-21T04:01:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22452\/"},"modified":"2024-11-21T12:01:24","modified_gmt":"2024-11-21T04:01:24","slug":"thinkphp-s-%e7%bc%93%e5%ad%98%e4%b8%a2%e5%a4%b1%e6%80%8e%e4%b9%88%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22452\/","title":{"rendered":"thinkphp s \u7f13\u5b58\u4e22\u5931\u600e\u4e48\u529e"},"content":{"rendered":"<p>\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u6211\u4eec\u5e38\u5e38\u4f1a\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f1a\u53d1\u73b0\u7f13\u5b58\u4e22\u5931\u7684\u95ee\u9898\uff0c\u8fd9\u4f1a\u4e25\u91cd\u5f71\u54cd\u7f51\u7ad9\u7684\u8fd0\u884c\u6548\u7387\u548c\u7528\u6237\u7684\u4f53\u9a8c\u3002<\/p>\n<p>\u672c\u6587\u5c06\u63a2\u8ba8ThinkPHP\u6846\u67b6\u4e2d\u7f13\u5b58\u4e22\u5931\u7684\u539f\u56e0\u548c\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n<p>\u539f\u56e0\u4e00\uff1a\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f<\/p>\n<p>ThinkPHP\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u7f13\u5b58\u7cfb\u7edf\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u7f13\u5b58\uff0c\u5982\u6587\u4ef6\u7f13\u5b58\u3001Redis\u7f13\u5b58\u7b49\u3002\u5728\u8bbe\u7f6e\u7f13\u5b58\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u7f13\u5b58\u7684\u6709\u6548\u65f6\u95f4\uff0c\u5728\u8fd9\u6bb5\u65f6\u95f4\u5185\u7f13\u5b58\u4f1a\u751f\u6548\uff0c\u8d85\u8fc7\u4e86\u8fd9\u6bb5\u65f6\u95f4\u7f13\u5b58\u5c31\u4f1a\u8fc7\u671f\u3002<\/p>\n<p>\u5f53\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f\u540e\uff0c\u4e0b\u4e00\u6b21\u8c03\u7528\u8be5\u7f13\u5b58\u65f6\uff0c\u5c31\u4f1a\u4ece\u6570\u636e\u5e93\u6216\u5176\u5b83\u6570\u636e\u6e90\u4e2d\u91cd\u65b0\u83b7\u53d6\u6570\u636e\u5e76\u91cd\u65b0\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u4e4b\u524d\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u4e22\u5931\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u8c03\u6574\u7f13\u5b58\u65f6\u95f4<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u7f13\u5b58\u7684\u6709\u6548\u65f6\u95f4\u6765\u5224\u65ad\u7f13\u5b58\u662f\u5426\u5df2\u8fc7\u671f\uff0c\u5f53\u7f13\u5b58\u8fc7\u671f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u6765\u907f\u514d\u7f13\u5b58\u7684\u4e22\u5931\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>$key = 'user_info';\n$user_info = cache($key);\nif (!$user_info) {\n    \/\/\u7f13\u5b58\u4e0d\u5b58\u5728\uff0c\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\n    $user_info = Db::name('user')-&gt;where('id', 1)-&gt;find();\n    \/\/\u8bbe\u7f6e\u7f13\u5b58\u5e76\u8bbe\u7f6e\u6709\u6548\u65f6\u95f4\u4e3a10\u5206\u949f\n    cache($key, $user_info, 600);\n}\nreturn $user_info;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5f53\u7f13\u5b58\u4e0d\u5b58\u5728\u65f6\uff0c\u6211\u4eec\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\uff0c\u5e76\u8bbe\u7f6e\u6709\u6548\u65f6\u95f4\u4e3a10\u5206\u949f\uff0c\u5982\u679c\u7f13\u5b58\u5df2\u5b58\u5728\u4f46\u8fc7\u671f\u4e86\uff0c\u90a3\u4e48\u5728\u8c03\u7528cache\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5c06\u65b0\u83b7\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\u5e76\u91cd\u7f6e\u6709\u6548\u65f6\u95f4\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u7f13\u5b58\u7684\u4e22\u5931\u3002<\/p>\n<p>\u539f\u56e0\u4e8c\uff1a\u7f13\u5b58\u88ab\u6e05\u9664<\/p>\n<p>\u9664\u4e86\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f\u5916\uff0c\u6211\u4eec\u6709\u65f6\u53ef\u80fd\u4f1a\u624b\u52a8\u6e05\u9664\u7f13\u5b58\u6216\u8005\u901a\u8fc7\u7cfb\u7edf\u811a\u672c\u81ea\u52a8\u6e05\u9664\u7f13\u5b58\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u4e4b\u524d\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u88ab\u6e05\u9664\uff0c\u5e76\u4e14\u65e0\u6cd5\u518d\u83b7\u53d6\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u907f\u514d\u8bef\u5220\u7f13\u5b58<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bf9\u7f13\u5b58\u8fdb\u884c\u5206\u7c7b\u6216\u8005\u7ed9\u6bcf\u4e2a\u7f13\u5b58\u8bbe\u7f6e\u72ec\u7acb\u7684\u952e\u503c\u6765\u907f\u514d\u8bef\u5220\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/\u5206\u7c7b\u7f13\u5b58\n$key = 'user_info_' . $user_id;\n$user_info = cache($key);\n\n\/\/\u72ec\u7acb\u952e\u503c\n$key = 'user_info';\n$user_info = cache($key . '_' . $user_id);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5206\u7c7b\u7f13\u5b58\u6216\u8bbe\u7f6e\u72ec\u7acb\u952e\u503c\u6765\u907f\u514d\u8bef\u5220\u7f13\u5b58\uff0c\u8fd9\u6837\u5373\u4f7f\u67d0\u4e2a\u7f13\u5b58\u88ab\u8bef\u5220\u4e86\uff0c\u5176\u5b83\u7f13\u5b58\u4e5f\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<p>\u539f\u56e0\u4e09\uff1a\u7f13\u5b58\u5b58\u50a8\u7a7a\u95f4\u4e0d\u8db3<\/p>\n<p>\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u7f13\u5b58\u5360\u7528\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5982\u679c\u5b58\u50a8\u7a7a\u95f4\u4e0d\u8db3\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7f13\u5b58\u4e22\u5931\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u589e\u52a0\u7f13\u5b58\u5b58\u50a8\u7a7a\u95f4<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u7f13\u5b58\u7684\u5b58\u50a8\u7a7a\u95f4\u6765\u907f\u514d\u7f13\u5b58\u4e22\u5931\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/\u4f7f\u7528Redis\u7f13\u5b58\n$redis = new Redis();\n$redis-&gt;connect('127.0.0.1', 6379);\n$redis-&gt;setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);\n$redis-&gt;setOption(Redis::OPT_PREFIX, 'thinkphp_');\n\n\/\/\u8bbe\u7f6eRedis\u6700\u5927\u5185\u5b58\u4e3a512MB\n$redis-&gt;config('SET', 'maxmemory', '512mb');\n\n\/\/\u83b7\u53d6\u5f53\u524dRedis\u4f7f\u7528\u7684\u603b\u5185\u5b58\u548c\u5269\u4f59\u5185\u5b58\n$used_memory = $redis-&gt;info('memory')['used_memory'];\n$free_memory = $redis-&gt;info('memory')['maxmemory'] - $used_memory;\n\n\/\/\u8c03\u6574\u7f13\u5b58\u5b58\u50a8\u7a7a\u95f4\nif ($free_memory &lt; 1024 * 1024 * 100) {\n    $redis-&gt;config('SET', 'maxmemory', '1024mb');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Redis\u7f13\u5b58\uff0c\u5e76\u8bbe\u7f6e\u5176\u6700\u5927\u5185\u5b58\u4e3a512MB\uff0c\u5728\u83b7\u53d6\u5f53\u524dRedis\u4f7f\u7528\u7684\u603b\u5185\u5b58\u548c\u5269\u4f59\u5185\u5b58\u540e\uff0c\u5982\u679c\u5269\u4f59\u5185\u5b58\u5c0f\u4e8e100MB\uff0c\u5c31\u5c06\u6700\u5927\u5185\u5b58\u8c03\u6574\u4e3a1GB\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u7531\u4e8e\u5b58\u50a8\u7a7a\u95f4\u4e0d\u8db3\u5bfc\u81f4\u7684\u7f13\u5b58\u4e22\u5931\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u7f13\u5b58\u4e22\u5931\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u3001\u5206\u7c7b\u7f13\u5b58\u548c\u589e\u52a0\u7f13\u5b58\u5b58\u50a8\u7a7a\u95f4\u7b49\u63aa\u65bd\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u7f13\u5b58\u4e22\u5931\uff0c\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp s \u7f13\u5b58\u4e22\u5931\u600e\u4e48\u529e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u6211\u4eec\u5e38\u5e38\u4f1a\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u4f1a\u53d1\u73b0\u7f13\u5b58\u4e22\u5931\u7684\u95ee\u9898\uff0c\u8fd9\u4f1a\u4e25\u91cd\u5f71\u54cd\u7f51\u7ad9\u7684\u8fd0\u884c\u6548\u7387\u548c\u7528\u6237\u7684\u4f53\u9a8c\u3002 \u672c\u6587\u5c06\u63a2\u8ba8ThinkPHP\u6846\u67b6\u4e2d\u7f13\u5b58\u4e22\u5931\u7684\u539f\u56e0\u548c\u89e3\u51b3\u529e\u6cd5\u3002 \u539f\u56e0\u4e00\uff1a\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f ThinkPHP\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u7f13\u5b58\u7cfb\u7edf\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u7f13\u5b58\uff0c\u5982\u6587\u4ef6\u7f13\u5b58\u3001Redis\u7f13\u5b58\u7b49\u3002\u5728\u8bbe\u7f6e\u7f13\u5b58\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u7f13\u5b58\u7684\u6709\u6548\u65f6\u95f4\uff0c\u5728\u8fd9\u6bb5\u65f6\u95f4\u5185\u7f13\u5b58\u4f1a\u751f\u6548\uff0c\u8d85\u8fc7\u4e86\u8fd9\u6bb5\u65f6\u95f4\u7f13\u5b58\u5c31\u4f1a\u8fc7\u671f\u3002 \u5f53\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f\u540e\uff0c\u4e0b\u4e00\u6b21\u8c03\u7528\u8be5\u7f13\u5b58\u65f6\uff0c\u5c31\u4f1a\u4ece\u6570\u636e\u5e93\u6216\u5176\u5b83\u6570\u636e\u6e90\u4e2d\u91cd\u65b0\u83b7\u53d6\u6570\u636e\u5e76\u91cd\u65b0\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u4e4b\u524d\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u4e22\u5931\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89e3\u51b3\u529e\u6cd5\uff1a\u8c03\u6574\u7f13\u5b58\u65f6\u95f4 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u7f13\u5b58\u7684\u6709\u6548\u65f6\u95f4\u6765\u5224\u65ad\u7f13\u5b58\u662f\u5426\u5df2\u8fc7\u671f\uff0c\u5f53\u7f13\u5b58\u8fc7\u671f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u6765\u907f\u514d\u7f13\u5b58\u7684\u4e22\u5931\uff0c\u4f8b\u5982\uff1a $key = &#8216;user_info&#8217;; $user_info = cache($key); if (!$user_info) { \/\/\u7f13\u5b58\u4e0d\u5b58\u5728\uff0c\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e $user_info = Db::name(&#8216;user&#8217;)-&gt;where(&#8216;id&#8217;, 1)-&gt;find(); \/\/\u8bbe\u7f6e\u7f13\u5b58\u5e76\u8bbe\u7f6e\u6709\u6548\u65f6\u95f4\u4e3a10\u5206\u949f cache($key, $user_info, 600); } return $user_info; \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5f53\u7f13\u5b58\u4e0d\u5b58\u5728\u65f6\uff0c\u6211\u4eec\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\uff0c\u5e76\u8bbe\u7f6e\u6709\u6548\u65f6\u95f4\u4e3a10\u5206\u949f\uff0c\u5982\u679c\u7f13\u5b58\u5df2\u5b58\u5728\u4f46\u8fc7\u671f\u4e86\uff0c\u90a3\u4e48\u5728\u8c03\u7528cache\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5c06\u65b0\u83b7\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\u5e76\u91cd\u7f6e\u6709\u6548\u65f6\u95f4\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u907f\u514d\u7f13\u5b58\u7684\u4e22\u5931\u3002 \u539f\u56e0\u4e8c\uff1a\u7f13\u5b58\u88ab\u6e05\u9664 \u9664\u4e86\u7f13\u5b58\u65f6\u95f4\u8fc7\u671f\u5916\uff0c\u6211\u4eec\u6709\u65f6\u53ef\u80fd\u4f1a\u624b\u52a8\u6e05\u9664\u7f13\u5b58\u6216\u8005\u901a\u8fc7\u7cfb\u7edf\u811a\u672c\u81ea\u52a8\u6e05\u9664\u7f13\u5b58\uff0c\u8fd9\u6837\u5c31\u4f1a\u5bfc\u81f4\u4e4b\u524d\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u7684\u6570\u636e\u88ab\u6e05\u9664\uff0c\u5e76\u4e14\u65e0\u6cd5\u518d\u83b7\u53d6\u3002 \u89e3\u51b3\u529e\u6cd5\uff1a\u907f\u514d\u8bef\u5220\u7f13\u5b58 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bf9\u7f13\u5b58\u8fdb\u884c\u5206\u7c7b\u6216\u8005\u7ed9\u6bcf\u4e2a\u7f13\u5b58\u8bbe\u7f6e\u72ec\u7acb\u7684\u952e\u503c\u6765\u907f\u514d\u8bef\u5220\u7f13\u5b58\uff0c\u4f8b\u5982\uff1a \/\/\u5206\u7c7b\u7f13\u5b58 $key = &#8216;user_info_&#8217; . $user_id; $user_info = cache($key); \/\/\u72ec\u7acb\u952e\u503c $key = &#8216;user_info&#8217;; $user_info = cache($key . &#8216;_&#8217; . $user_id); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22452","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22452"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22452\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}