Getting Started

Deployment Deployment - Diagnose Migrations Staging

Site Management

Backups Client Reporting Passwords Plugin Automation Plugin Management - Global Plugins & Themes - Diagnose Plugins & Themes - General Plugins & Themes - Git Plugins & Themes - Logs Must Install White Label WordPress Updates

Caching & Performance

Caching Caching - Blogs/Articles Caching - Diagnose Caching - Git Caching - WooCommerce Redis Optimize & Scale

Security

Security Security - Firewall

SMTP, CDN & DNS

SMTP SendGrid CDN CDN - AWS CDN - Cloudflare CDN - Diagnose Domains & DNS

Server & Tools

Analytics & Logs CRON Jobs Database Debug Tool Git Monitoring PHP Settings Redirects SEO Tools Server Errors sFTP SSL

Staq Billing

Staq Billing > Account Staq Billing > Client Staq Billing > Setup

Media

Media Media - Diagnose Media - Optimize

Accounts & Billing

Accounts & Billing

General

WordPress Hosting Website Diagnose Troubleshoot - Other

Staq Cache – Caching Dynamic Information


On this page

    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 Nginx will look for a cached file in the unique path, and if it finds it, it will serve it to the user.

    This saves the Nginx from initiating a PHP worker to process the HTTP request, which is time consuming.

    Dynamic Content Caching Challenge

    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.

    Luckily, Staq offers you the right tools to do such thing.

    A common example is country based cache. For example, let’s take this problem:

    • Website myshop.com has an option to select a country, and based on the selected country, it shows different content.
    • The country is detected by the system using “mycountry_code” cookie parameter.

    If user A selects “AU” (Australia) and user B selects “US” (United States), we cannot apply the default behaviour, due to the fact that they both request the same URL.

    So we must add more variables to the saved URL.

    Custom Caching with Staq

    In order to resolve the issue, we need to create a custom location while taking into consideration the new parameter, in our case, “$_COOKIE[‘mycountry_code’]”.

    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 wpstaq-cache.php drop-in that is found inside advanced-cache.php (found under the wp-content folder).

    If you take a look inside the advanced-cache.php generated by Staq, you will see:

    Now we want to create wpstaq-cache.php and let the Page Cache do the rest!

    First, we want to create a unique path for saving the file that takes into consideration the mycountry_code cookie.

    For this purpose, we want to hook into the Staq filter wpsh_page_cache_html_file_path, or better referred to via the constant StaqConstant::FILTER_PAGE_CACHE_HTML_FILE_PATH as follows:

    The first time the user visits the URL with their country cookie in place, a custom path will be generated.

    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.

     

    Happy coding!

    Need some help?

    We all do sometimes. Please reach out to our support team by dropping us a support ticket. We will respond fast.