{"id":35873,"date":"2024-03-15T02:16:20","date_gmt":"2024-03-15T02:16:20","guid":{"rendered":"https:\/\/www.wpstaq.com\/?post_type=knowledgebase&#038;p=35873"},"modified":"2024-07-03T06:08:48","modified_gmt":"2024-07-03T06:08:48","slug":"enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching","status":"publish","type":"knowledgebase","link":"https:\/\/www.wpstaq.com\/au\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/","title":{"rendered":"Staq Cache &#8211; Caching Dynamic Information"},"content":{"rendered":"<h2>Background<\/h2>\n<p>When you enable the Staq Cache, the default behaviour is to create one static HTML file per URL.<\/p>\n<p>The file will then include the cached version of the URL and be saved at a unique path to that URL, which can recognized by the Nginx.<\/p>\n<p>Whenever there is a visit to your website, the Nginx will look for a cached file in the unique path, and if it finds it, it will serve it to the user.<\/p>\n<p>This saves the Nginx from initiating a PHP worker to process the HTTP request, which is time consuming.<\/p>\n<h2>Dynamic Content Caching Challenge<\/h2>\n<p>If you want to serve a cached version based variables other than the ones in the URL, e.g. Cookies or Query strings, then a custom solution must be implemented.<\/p>\n<p>Luckily, Staq offers you the right tools to do such thing.<\/p>\n<p>A common example is country based cache. For example, let&#8217;s take this problem:<\/p>\n<ul>\n<li>Website myshop.com has an option to select a country, and based on the selected country, it shows different content.<\/li>\n<li>The country is detected by the system using &#8220;<strong>mycountry_code<\/strong>&#8221; cookie parameter.<\/li>\n<\/ul>\n<p>If user A selects &#8220;AU&#8221; (Australia) and user B selects &#8220;US&#8221; (United States), we cannot apply the default behaviour, due to the fact that they both request the same URL.<\/p>\n<p>So we must add more variables to the saved URL.<\/p>\n<h2>Custom Caching with Staq<\/h2>\n<p>In order to resolve the issue, we need to create a custom location while taking into consideration the new parameter, in our case, &#8220;$_COOKIE[&#8216;mycountry_code&#8217;]&#8221;.<\/p>\n<p>Note that this will not be recognised by Nginx and a PHP worker has to be triggered. However, Staq will search for the new file as early as possible, so the request will not be heavy as it will not initiate any WordPress dependencies and will not process the whole process to serve the file. This can be done using our <strong>wpstaq-cache.php<\/strong> drop-in that is found inside\u00a0<strong>advanced-cache.php<\/strong> (found under the wp-content folder).<\/p>\n<p>If you take a look inside the advanced-cache.php generated by Staq, you will see:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\" wp-image-35875 alignleft\" src=\"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png\" alt=\"\" width=\"276\" height=\"143\" srcset=\"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png 407w, https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache-300x156.png 300w\" sizes=\"auto, (max-width: 276px) 100vw, 276px\" \/><\/p>\n<p>Now we want to create wpstaq-cache.php and let the Page Cache do the rest!<\/p>\n<p>First, we want to create a unique path for saving the file that takes into consideration the <strong>mycountry_code\u00a0<\/strong>cookie.<\/p>\n<p>For this purpose, we want to hook into the Staq filter <strong>wpsh_page_cache_html_file_path,\u00a0<\/strong>or better referred to via the constant <strong>StaqConstant::FILTER_PAGE_CACHE_HTML_FILE_PATH\u00a0<\/strong>as follows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-35878 alignleft\" src=\"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-cache-custom-code-sample.png\" alt=\"\" width=\"972\" height=\"259\" srcset=\"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-cache-custom-code-sample.png 972w, https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-cache-custom-code-sample-300x80.png 300w, https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-cache-custom-code-sample-768x205.png 768w\" sizes=\"auto, (max-width: 972px) 100vw, 972px\" \/><\/p>\n<div><\/div>\n<p>The first time the user visits the URL with their country cookie in place, a custom path will be generated.<\/p>\n<p>The next time this happens, the Staq Page Cache will detect the file in the custom path and serve it as soon as the request starts.<\/p>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<div>Happy coding!<\/div>\n","protected":false},"featured_media":0,"parent":0,"template":"","class_list":["post-35873","knowledgebase","type-knowledgebase","status-publish","hentry","categories-caching","categories-caching-woocommerce"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress\" \/>\n<meta property=\"og:description\" content=\"Background When you enable the Staq Cache, the default behaviour is to create one static HTML file per URL. The file will then include the cached version of the URL and be saved at a unique path to that URL, which can recognized by the Nginx. Whenever there is a visit to your website, the&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/\" \/>\n<meta property=\"og:site_name\" content=\"Managed Hosting Platform for WordPress\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/staqhosting\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-03T06:08:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/\",\"url\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/\",\"name\":\"Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.wpstaq.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/wpstaq-advanced-cache.png\",\"datePublished\":\"2024-03-15T02:16:20+00:00\",\"dateModified\":\"2024-07-03T06:08:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/#breadcrumb\"},\"inLanguage\":\"en-au\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-au\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.wpstaq.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/wpstaq-advanced-cache.png\",\"contentUrl\":\"https:\\\/\\\/www.wpstaq.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/wpstaq-advanced-cache.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/knowledgebase\\\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.wpstaq.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Staq Cache &#8211; Caching Dynamic Information\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#website\",\"url\":\"https:\\\/\\\/www.wpstaq.com\\\/\",\"name\":\"Managed Hosting Platform for WordPress\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.wpstaq.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-au\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#organization\",\"name\":\"Staq\",\"alternateName\":\"WPStaq\",\"url\":\"https:\\\/\\\/www.wpstaq.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-au\",\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.wpstaq.com\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/cropped-LogoNoText.png\",\"contentUrl\":\"https:\\\/\\\/www.wpstaq.com\\\/wp-content\\\/uploads\\\/2023\\\/05\\\/cropped-LogoNoText.png\",\"width\":512,\"height\":512,\"caption\":\"Staq\"},\"image\":{\"@id\":\"https:\\\/\\\/www.wpstaq.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/staqhosting\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/wpstaq\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/","og_locale":"en_US","og_type":"article","og_title":"Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress","og_description":"Background When you enable the Staq Cache, the default behaviour is to create one static HTML file per URL. The file will then include the cached version of the URL and be saved at a unique path to that URL, which can recognized by the Nginx. Whenever there is a visit to your website, the&hellip;","og_url":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/","og_site_name":"Managed Hosting Platform for WordPress","article_publisher":"https:\/\/www.facebook.com\/staqhosting\/","article_modified_time":"2024-07-03T06:08:48+00:00","og_image":[{"url":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/","url":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/","name":"Staq Cache - Caching Dynamic Information - Managed Hosting Platform for WordPress","isPartOf":{"@id":"https:\/\/www.wpstaq.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/#primaryimage"},"image":{"@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png","datePublished":"2024-03-15T02:16:20+00:00","dateModified":"2024-07-03T06:08:48+00:00","breadcrumb":{"@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/#breadcrumb"},"inLanguage":"en-au","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/"]}]},{"@type":"ImageObject","inLanguage":"en-au","@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/#primaryimage","url":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png","contentUrl":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2024\/03\/wpstaq-advanced-cache.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.wpstaq.com\/knowledgebase\/enhancing-site-performance-with-staq-cache-a-guide-to-multi-parameter-caching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.wpstaq.com\/"},{"@type":"ListItem","position":2,"name":"Staq Cache &#8211; Caching Dynamic Information"}]},{"@type":"WebSite","@id":"https:\/\/www.wpstaq.com\/#website","url":"https:\/\/www.wpstaq.com\/","name":"Managed Hosting Platform for WordPress","description":"","publisher":{"@id":"https:\/\/www.wpstaq.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wpstaq.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-au"},{"@type":"Organization","@id":"https:\/\/www.wpstaq.com\/#organization","name":"Staq","alternateName":"WPStaq","url":"https:\/\/www.wpstaq.com\/","logo":{"@type":"ImageObject","inLanguage":"en-au","@id":"https:\/\/www.wpstaq.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2023\/05\/cropped-LogoNoText.png","contentUrl":"https:\/\/www.wpstaq.com\/wp-content\/uploads\/2023\/05\/cropped-LogoNoText.png","width":512,"height":512,"caption":"Staq"},"image":{"@id":"https:\/\/www.wpstaq.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/staqhosting\/","https:\/\/www.linkedin.com\/company\/wpstaq"]}]}},"_links":{"self":[{"href":"https:\/\/www.wpstaq.com\/au\/wp-json\/wp\/v2\/knowledgebase\/35873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wpstaq.com\/au\/wp-json\/wp\/v2\/knowledgebase"}],"about":[{"href":"https:\/\/www.wpstaq.com\/au\/wp-json\/wp\/v2\/types\/knowledgebase"}],"wp:attachment":[{"href":"https:\/\/www.wpstaq.com\/au\/wp-json\/wp\/v2\/media?parent=35873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}