{"id":224,"date":"2024-08-13T06:52:25","date_gmt":"2024-08-13T06:52:25","guid":{"rendered":"http:\/\/3.84.21.125\/?p=224"},"modified":"2024-08-13T06:53:55","modified_gmt":"2024-08-13T06:53:55","slug":"cach-magento-luu-data-vao-redis-cache","status":"publish","type":"post","link":"https:\/\/blog.hnk90.com\/?p=224","title":{"rendered":"C\u00e1ch Magento L\u01b0u Data V\u00e0o Redis Cache"},"content":{"rendered":"\n<p>C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 xem l\u1ea1i c\u00e1ch Magento l\u1ea5y data t\u1eeb cache redis trong b\u00e0i n\u00e0y <a href=\"http:\/\/3.84.21.125\/?p=212\">http:\/\/3.84.21.125\/?p=212<\/a><\/p>\n\n\n\n<p>V\u1edbi b\u00e0i n\u00e0y th\u00ec th\u1eed t\u00ecm hi\u1ec3u Magento save cache m\u1edbi nh\u01b0 n\u00e0o nh\u00e9, \u0111\u1ea7u ti\u00ean h\u00e3y flush cache \u0111i <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"bin\/magento cache:flush\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">bin<\/span><span style=\"color: #81A1C1\">\/<\/span><span style=\"color: #D8DEE9\">magento<\/span><span style=\"color: #D8DEE9FF\"> cache<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9\">flush<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><br>Ti\u1ebfp theo load frontend, gi\u1ea3 s\u1eed m\u00ecnh load l\u1ea1i m\u1ed9t page product detail b\u1ea5t k\u1ef3 tr\u00ean site localhost c\u1ee7a m\u00ecnh.<\/p>\n\n\n\n<p>File debug \u0111\u1ea7u ti\u00ean t\u1ea5t nhi\u00ean l\u00e0 \\Magento\\Framework\\App\\Http::launch, \u0111\u1ec3 \u00fd request URI ph\u1ea3i \u0111\u00fang l\u00e0 request t\u1eeb ph\u00eda frontend m\u00e0 m\u00ecnh \u0111ang mu\u1ed1n debug nh\u00e9<\/p>\n\n\n\n<p>V\u00ed d\u1ee5, \u0111\u1ec3 \u00fd URL request s\u1ebd t\u01b0\u01a1ng \u1ee9ng v\u1edbi request uri khi debug<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"705\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-10.png\" alt=\"\" class=\"wp-image-229\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-10.png 1025w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-10-300x206.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-10-768x528.png 768w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/figure>\n\n\n\n<p><br>Ti\u1ebfp theo l\u00e0 \\Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin::aroundDispatch<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"938\" height=\"536\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-8.png\" alt=\"\" class=\"wp-image-227\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-8.png 938w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-8-300x171.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-8-768x439.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/figure>\n\n\n\n<p><br>do kh\u00f4ng c\u00f3 cache n\u00ean $result s\u1ebd l\u00e0 false n\u00ean h\u1ec7 th\u1ed1ng ti\u1ebfp t\u1ee5c x\u1eed l\u00fd \u0111\u1ec3 tr\u1ea3 ra k\u1ebft qu\u1ea3, v\u00e0 sau \u0111\u00f3 save l\u1ea1i<br>v\u00e0o cache \u1edf function <br>\\Magento\\PageCache\\Model\\Controller\\Result\\BuiltinPlugin::afterRenderResult<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"851\" height=\"513\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-11.png\" alt=\"\" class=\"wp-image-230\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-11.png 851w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-11-300x181.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-11-768x463.png 768w\" sizes=\"auto, (max-width: 851px) 100vw, 851px\" \/><\/figure>\n\n\n\n<p><br>Ti\u1ebfp theo, cache \u0111\u01b0\u1ee3c save nh\u01b0 sau \\Magento\\Framework\\App\\PageCache\\Kernel::process<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"312\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-12.png\" alt=\"\" class=\"wp-image-231\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-12.png 840w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-12-300x111.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-12-768x285.png 768w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p>\u1ede \u0111\u00e2y l\u01b0u \u00fd h\u00e0m save c\u1ee7a fullPageCache nh\u1eadn v\u00e0o m\u1ed9t s\u1ed1 tham s\u1ed1 quan tr\u1ecdng nh\u01b0<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>tham s\u1ed1 \u0111\u1ea7u ti\u00ean l\u00e0 data &#8211; ch\u00ednh l\u00e0 data \u0111\u01b0\u1ee3c save v\u00e0o cache<\/li>\n\n\n\n<li>cache key &#8211; m\u1ed7i data \u0111\u01b0\u1ee3c save v\u00e0o cache \u0111\u1ec1u c\u00f3 cache key ri\u00eang<\/li>\n\n\n\n<li>tags &#8211; data save trong cache s\u1ebd c\u00f3 nhi\u1ec1u tag<\/li>\n<\/ul>\n\n\n\n<p>\u1edf \u0111\u00e2y t\u00ecm hi\u1ec3u qua xem cache key \u0111\u01b0\u1ee3c t\u1ea1o ra nh\u01b0 th\u1ebf n\u00e0o, c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y function<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"$this-&gt;identifier-&gt;getValue()\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9\">$this<\/span><span style=\"color: #81A1C1\">-&gt;<\/span><span style=\"color: #D8DEE9\">identifier<\/span><span style=\"color: #81A1C1\">-&gt;<\/span><span style=\"color: #88C0D0\">getValue<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><br>function n\u00e0y \u0111\u01b0\u1ee3c process \u1edf \\Magento\\Framework\\App\\PageCache\\Identifier::getValue<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1023\" height=\"691\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-14.png\" alt=\"\" class=\"wp-image-233\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-14.png 1023w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-14-300x203.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-14-768x519.png 768w\" sizes=\"auto, (max-width: 1023px) 100vw, 1023px\" \/><\/figure>\n\n\n\n<p><br>\u0110\u01a1n gi\u1ea3n m\u1ea3ng data 3 ph\u1ea7n t\u1eed: secure, uri, vary string \u0111\u01b0\u1ee3c json encode l\u1ea1i v\u00e0 \u0111\u01b0\u1ee3c m\u00e3 h\u00f3a b\u1eb1ng h\u00e0m sha1<\/p>\n\n\n\n<p>Ngo\u00e0i ra s\u1ebd c\u00f3 th\u00eam m\u1ed9t v\u00e0i after plugin tham v\u00e0o qu\u00e1 tr\u00ecnh generate ra cache key, <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"320\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-13-134127.png\" alt=\"\" class=\"wp-image-234\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-13-134127.png 940w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-13-134127-300x102.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-13-134127-768x261.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<p><br>\u1edf \u0111\u00e2y cache key c\u00f3 d\u1ea1ng ki\u1ec3u nh\u01b0 sau: 7a9_7C7EA5EDCDDEF27535143BF11244D808A9E5685E<\/p>\n\n\n\n<p>Cu\u1ed1i c\u00f9ng l\u00e0 data \u0111\u01b0\u1ee3c save v\u00e0o Redis Cache \u1edf function \\Cm_Cache_Backend_Redis::save &#8211; trong file path<br>vendor\/colinmollenhour\/cache-backend-redis\/Cm\/Cache\/Backend\/Redis.php<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"884\" height=\"286\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-15.png\" alt=\"\" class=\"wp-image-235\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-15.png 884w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-15-300x97.png 300w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-15-768x248.png 768w\" sizes=\"auto, (max-width: 884px) 100vw, 884px\" \/><\/figure>\n\n\n\n<p><br>Sau khi ch\u1ea1y xong debug th\u00ec c\u00e1c b\u1ea1n c\u00f3 v\u00e0o Redis ki\u1ec3m tra, \u1edf \u0111\u00e2y m\u00ecnh s\u1eed d\u1ee5ng tool phpstorm \u0111\u1ec3 xem cho nhanh, ho\u1eb7c c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng command line \u0111\u1ec3 xem nh\u00e9<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"413\" src=\"http:\/\/3.84.21.125\/wp-content\/uploads\/2024\/08\/image-16.png\" alt=\"\" class=\"wp-image-236\" srcset=\"https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-16.png 663w, https:\/\/blog.hnk90.com\/wp-content\/uploads\/2024\/08\/image-16-300x187.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n\n\n\n<p>B\u00e0i n\u00e0y t\u1edbi \u0111\u00e2y th\u00f4i, b\u00e0i ti\u1ebfp theo ch\u00fang ta th\u1eed xem Magento x\u00f3a m\u1ed9t v\u00e0i d\u1eef li\u1ec7u c\u1ee5 th\u1ec3 t\u1eeb Redis Cache nh\u01b0 n\u00e0o nh\u00e9. <\/p>\n\n\n\n<p>Bye Bye !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C\u00e1c b\u1ea1n c\u00f3 th\u1ec3 xem l\u1ea1i c\u00e1ch Magento l\u1ea5y data t\u1eeb cache redis trong b\u00e0i n\u00e0y http:\/\/3.84.21.125\/?p=212 V\u1edbi b\u00e0i n\u00e0y th\u00ec th\u1eed t\u00ecm hi\u1ec3u Magento save cache m\u1edbi nh\u01b0 n\u00e0o nh\u00e9, \u0111\u1ea7u ti\u00ean h\u00e3y flush cache \u0111i Ti\u1ebfp theo load frontend, gi\u1ea3 s\u1eed m\u00ecnh load l\u1ea1i m\u1ed9t page product detail b\u1ea5t k\u1ef3 tr\u00ean site localhost [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=224"}],"version-history":[{"count":4,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":238,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions\/238"}],"wp:attachment":[{"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hnk90.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}