{"id":11046,"date":"2022-11-12T03:25:21","date_gmt":"2022-11-11T19:25:21","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=11046"},"modified":"2023-12-11T10:31:11","modified_gmt":"2023-12-11T02:31:11","slug":"php-session-%e5%b0%8d%e7%b3%bb%e7%b5%b1%e7%9a%84%e5%bd%b1%e9%9f%bf","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=11046","title":{"rendered":"PHP - Session \u5c0d\u7cfb\u7d71\u7684\u5f71\u97ff"},"content":{"rendered":"<p>--<\/p>\n<p>PHP \u5982\u679c\u9700\u8981\u4f7f\u7528 Session \u4e00\u5b9a\u8981\u6ce8\u610f\u57fa\u672c\u4f7f\u7528\u539f\u5247\uff0c\u53ef\u4ee5\u53c3\u8003 Hoyo \u6700\u8fd1\u9047\u5230\u7684\u6848\u4f8b\u5f15\u4ee5\u70ba\u6212<\/p>\n<p>--<\/p>\n<h2>\u6848\u4f8b\u4e00\uff1a\u4e0d\u7576\u7684\u5b58\u653e\u5167\u5bb9<\/h2>\n<p>\u516c\u53f8\u524d\u54e1\u5de5\u72af\u8822\u5c07\u5927\u91cf\u8cc7\u6599\u5132\u5b58\u65bc Session\uff0cPHP \u7684 Session \u5132\u5b58\u7a7a\u9593\u56e0\u70ba\u662f\u96f2\u7aef\u4e3b\u6a5f\uff0c\u786c\u789f\u7a7a\u9593\u5f88\u5c0f\u53ea\u6709 30G \u5c0e\u81f4\u7a7a\u9593\u5beb\u6eff\u7cfb\u7d71\u767c\u751f\u932f\u8aa4<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone wp-image-11047 size-large\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156-1024x103.png\" alt=\"\" width=\"640\" height=\"64\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156-1024x103.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156-300x30.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156-768x77.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156-500x50.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221107_103156.png 1154w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>\u89e3\u6c7a\uff1a\u627e\u5230\u72af\u8822\u7684\u7a0b\u5f0f\uff0c\u4fee\u6b63\u5373\u53ef<\/p>\n<p>--<\/p>\n<h2>\u6848\u4f8b\u4e8c\uff1a\u4e0d\u7576\u7684\u5b58\u653e\u4f4d\u7f6e<\/h2>\n<p>\u4f7f\u7528 MySQL \u7576\u4f5c Session \u5132\u5b58\u7a7a\u9593\uff0c\u5728\u76f8\u7c3f\u61c9\u7528\u5167\uff0c\u5927\u91cf\u7167\u7247\u56e0\u70ba\u6b0a\u9650\u6aa2\u67e5\u554f\u984c\u4f7f\u7528 Session\uff0c\u5c0e\u81f4\u7db2\u9801\u8f09\u5165\u901f\u5ea6\u7de9\u6162\uff0c\u5728 500 \u5f35\u7167\u7247\u6e2c\u8a66\u4e0b\uff0c\u6c92\u6709 Session \u6aa2\u67e5\u7684 3 \u79d2\u5ef6\u9577\u5230\u8d85\u904e 30 \u79d2<\/p>\n<p>\u9019\u662f Hoyo \u81ea\u5df1\u7684\u76f8\u7c3f\u5e73\u53f0\uff0c\u56e0\u70ba\u591a\u7db2\u7ad9\u8de8\u7db2\u57df Session \u7684\u61c9\u7528\uff0c\u4e00\u958b\u59cb\u4f7f\u7528 MySQL \u8cc7\u6599\u5eab\u7576\u4f5c\u5132\u5b58\u5a92\u4ecb\uff0c\u5f8c\u4f86\u4e00\u76f4\u9047\u5230\u7167\u7247\u591a\u6642\u6548\u7387\u4e0d\u5f70\u554f\u984c\uff0c\u4e0b\u5b9a\u6c7a\u5fc3\u89e3\u6c7a\uff0c\u56e0\u70ba\u7cfb\u7d71\u67b6\u69cb\u6bd4\u8f03\u8907\u96dc\u61f7\u7591\u904e Nginx Proxy \u505a\u4e86\u5927\u91cf\u7684\u8a2d\u5b9a\uff0c\u4e5f\u4ee5\u70ba\u662f HTTP 1.1 \u7684\u9650\u5236\u63a1\u7528\u4e86 HTTP\/2 \u751a\u81f3\u662f HTTP\/3 \u4e5f\u662f\u6539\u5584\u4e0d\u5927\uff0c\u4e5f\u4ee5\u70ba\u662f Docker \u7684\u6548\u80fd\u554f\u984c\u6216\u662f\u5bb6\u88e1\u8def\u7531\u5668\u8001\u820a\uff0c\u8d70\u4e86\u5f88\u591a\u5f4e\u8def\u6700\u5f8c\u624d\u78ba\u5b9a\u662f Session \u5b58\u653e\u5728\u8cc7\u6599\u5eab\u7684\u554f\u984c<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-large wp-image-11066\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-1024x517.png\" alt=\"\" width=\"584\" height=\"295\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-1024x517.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-300x151.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-768x388.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-1536x775.png 1536w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117-500x252.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031117.png 1920w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-11067\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031130.png\" alt=\"\" width=\"373\" height=\"29\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031130.png 373w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-031130-300x23.png 300w\" sizes=\"(max-width: 373px) 100vw, 373px\" \/><\/p>\n<p>\u5c07\u8cc7\u6599\u8868\u8f49\u63db\u6210 Memory \u5b58\u653e\u5728\u8a18\u61b6\u9ad4\u5f8c\uff0c\u901f\u5ea6\u5feb\u4e86\u4e00\u4e9b\uff0c\u4e0d\u904e\u9084\u662f\u4e0d\u7406\u60f3<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-large wp-image-11080\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-1024x517.png\" alt=\"\" width=\"584\" height=\"295\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-1024x517.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-300x151.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-768x388.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-1536x776.png 1536w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042-500x252.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221114-205042.png 1919w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/p>\n<p>\u89e3\u6c7a\uff1a\u6539\u653e\u5728 Redis \u5c31\u89e3\u6c7a\u4e86<\/p>\n<ul>\n<li><a href=\"https:\/\/levelup.gitconnected.com\/highly-performant-php-sessions-with-redis-b2dc17b4f4e4\" target=\"_blank\" rel=\"noopener\">Highly Performant PHP Sessions with Redis<\/a><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">ini_set('session.save_handler', 'redis');\r\nini_set('session.save_path', 'tcp:\/\/1.2.3.4:6379');<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone size-large wp-image-11064\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-1024x518.png\" alt=\"\" width=\"584\" height=\"295\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-1024x518.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-300x152.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-768x388.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-1536x776.png 1536w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705-500x253.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030705.png 1917w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-11065\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030723.png\" alt=\"\" width=\"363\" height=\"26\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030723.png 363w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/11\/20221112-030723-300x21.png 300w\" sizes=\"(max-width: 363px) 100vw, 363px\" \/><\/p>\n<p>--<\/p>\n<h2>\u4e00\u500b\u6708\u5f8c...<\/h2>\n<p>\u53c8\u8b8a\u6162\u4e86\uff0c\u7136\u5f8c\u61f7\u7591\u662f\u6709\u4e86\u8cc7\u6599\u7684\u554f\u984c\uff0c\u96d6\u7136\u624d 1000 \u591a\u7b46\uff0c\u6e05\u7a7a\u4e4b\u5f8c\u5c31\u53c8\u6062\u5fa9\u539f\u672c\u7684\u901f\u5ea6\uff0c\u7136\u5f8c\u5c31<\/p>\n<ul>\n<li><a href=\"https:\/\/medium.com\/jerrynotes\/redis-whats-slowlog-be87515bbfeb\" target=\"_blank\" rel=\"noopener\">Redis What\u2019s Slowlog?<\/a><\/li>\n<\/ul>\n<p>\u67e5\u4e86\u4e00\u4e0b<\/p>\n<pre class=\"lang:default decode:true \">192.168.0.6:6379&gt; SLOWLOG GET\r\n1) 1) (integer) 1\r\n   2) (integer) 1670123800\r\n   3) (integer) 10757\r\n   4) 1) \"KEYS\"\r\n      2) \"sess_*\"\r\n   5) \"172.18.0.1:51378\"\r\n   6) \"\"\r\n2) 1) (integer) 0\r\n   2) (integer) 1669562864\r\n   3) (integer) 17423\r\n   4) 1) \"KEYS\"\r\n      2) \"sess_*\"\r\n   5) \"172.18.0.1:60924\"\r\n   6) \"\"<\/pre>\n<p>\u518d\u5230 PHP Redis \u770b\u4e86\u4e00\u4e0b\uff0c\u679c\u7136\u662f\u9019\u6a23\u7528\u7684<\/p>\n<pre class=\"lang:php decode:true \">\/\/ \u5783\u573e\u56de\u6536\u51fd\u5f0f\uff0c\u547c\u53eb\u9031\u671f\u7531 session.gc_probability \u548c session.gc_divisor \u5f15\u6578\u63a7\u5236\r\npublic function gc($maxlifetime) {\r\n    $allKeys = $this-&gt;redis-&gt;keys(\"{$this-&gt;prefix}*\");\r\n    foreach($allKeys as $key) {\r\n        if($this-&gt;redis-&gt;exists($key) &amp;&amp; $this-&gt;redis-&gt;hGet($key, 'expires') + $maxlifetime &lt; time()) {\r\n            $this-&gt;redis-&gt;del($key);\r\n        }\r\n    }\r\n    return true;\r\n}<\/pre>\n<p>\u53c3\u8003<\/p>\n<ul>\n<li><a id=\"cb_post_title_url\" class=\"postTitle2 vertical-middle\" href=\"https:\/\/www.cnblogs.com\/soundcode\/p\/10847848.html\" target=\"_blank\" rel=\"noopener\"> <span role=\"heading\" aria-level=\"2\">redis hash\u7ed3\u6784\u5982\u4f55\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/span> <\/a><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true \">\/\/ \u6703\u8a71\u5132\u5b58\u8cc7\u6599\u6642\u547c\u53eb\u8a72\u65b9\u6cd5\uff0c\u5728\u6307\u4ee4\u78bc\u57f7\u884c\u5b8c\u6216session_write_close\u65b9\u6cd5\u547c\u53eb\u4e4b\u5f8c\u547c\u53eb\r\npublic function write($session_id, $data) {\r\n    $this-&gt;redis-&gt;hMSet($this-&gt;prefix.$session_id, array('expires' =&gt; time(), 'data' =&gt; $data));\r\n    $this-&gt;redis-&gt;expire($this-&gt;prefix.$session_id, 86400*30);\r\n    return 1;\r\n}\r\n<\/pre>\n<p>\u8b93 Redis \u81ea\u5df1\u7ba1\u7406\u518d\u89c0\u5bdf\u770b\u770b\u5427<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"11046\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,033&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- PHP \u5982\u679c\u9700\u8981\u4f7f\u7528 S...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"11046\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,033&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11046"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11046"}],"version-history":[{"count":10,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11046\/revisions"}],"predecessor-version":[{"id":11070,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/11046\/revisions\/11070"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}