September 28, 2017
Static Cache: What it is, and How it Works

Caching strategies are tricky, so our Support Manager, Ryan Belisle, took some time to explain one way to use Static Caching to deliver a faster site.

Keep reading to learn:

In ecommerce, few things are more miserable than paying for a promotional campaign and watching it sparkle, only to see your site to crash and burn during what should be your finest hour. Sometimes, promotions outperform your expectations, and your proactive measures aren’t enough. What then?

One tactic for squeezing more performance out of your site at the last minute is a method called static caching.

Heads up! We are not recommending taking a last-minute approach to deal with anticipated spikes in web traffic. This is for when you’ve already applied best practices of optimizing your infrastructure or app, and your marketing results exceed any expectations.

What is Cache Static Content?

While many forms of caching are available, static caching is a method for converting the page generated by a user’s request into an HTML document to serve any subsequent requests to that same page.

Rather than the server interpreting the site code itself, querying the database, and returning an HTML document back to the customer and finally loading the page, static caching saves a single result from the first two steps and provides that document to anyone else making the request.

Benefits of Static Caching

While basic, static caching can have a profound effect, specifically in the following areas:

  • Reduce overhead: Rather than needing to generate PHP processes to handle requests to pages on the site, the web server will just serve the HTML document directly back to the request on the statically cached page.
  • Provide a “landing page” for all click-through traffic: Instead of spending resources for anyone who visits your homepage or follows a link to a specific URL, this provides the cached page by default without interfering with normal operation for other pages on the site.

Limitations of Static Caching

I do want to point out, however, that static caching has limitations and should generally only be used as a temporary solution. Keep in mind:

  • A static cache is just that: static. Any changes made to the site will not be reflected on pages cached in this manner.
  • Information such as “items in cart” or other pieces of data stored in your customers’ session files will not function on these pages.

How to Implement Static Caching

While there are a few different ways to implement static caching, here are the basic steps to do it manually:

1. Log into your server with SSH.

2. Create a separate directory in your webroot to store the HTML documents used in the static cache.

3. Using cURL or Wget, make requests to the target pages and store them in the directory under their respective URI names (index, products, etc)

4. Add rewrites to the top of your .htaccess file to redirect requests to the static cache files, such as the following:

[code]
RewriteCond %{REQUEST_URI} ^/?example_category$ [NC]
RewriteRule .* https://www.domain.tld/static/example_category.html [R=302,L]
[/code]

Alternatively, you could also write a script to handle this process. The below example allows you to create a file with a separate URL on each line. These URLs will then be read to generate your static cache:

[code language=”bash”]
#!/bin/bash
# Basic argument validation
if [ “$#” -ne 2 ]; then
echo -e “Must pass two arguments to script.\n\nUsage: ./nexcess_static_cache_helper.sh domain url_list_file\n”
exit 1
fi
# Get some data about the domain
domain=$1
pages_file=$2
# Create directory for static pages in current directory
cache_dir=./static_cache
if ! [[ -d $cache_dir ]]; then
mkdir $cache_dir
fi
# Download static pages into HTML files
URL_list=$(cat $pages_file)
for URL in $URL_list; do
request_uri=$(echo $URL | cut -d/ -f 4-)
if [[ -z “$request_uri” ]]; then
cache_page_name=”index”
request_uri=”/”
else
cache_page_name=$(echo $request_uri | perl -pe ‘s,/|\.,_,g’)
fi
wget -q $URL -O $cache_dir/$cache_page_name.html
echo “$request_uri $cache_page_name” >> $HOME/temp_url.file
done
# Format a block for the htaccess
echo -e “########”
echo -e “# START: Static cache URL Rewrites generated on $(date)”
echo -e “########”
echo -e “RewriteEngine On”
cat $HOME/temp_url.file | while read uri page; do
echo “RewriteCond %{REQUEST_URI} ^/?${uri}$ [NC]\n”
echo “RewriteRule .* https://www.${domain}/static_cache/$page [R=302,L]\n”
done
echo -e “######”
echo -e “# END: Static cache URL Rewrites generated on $(date)”
echo -e “######”
rm $HOME/temp_url.file
[/code]

To use the above script:

1. Navigate to the webroot of your site, then create a file named cache_list.txt, or whatever you prefer.

2. Open the file. Add the URL for each page that needs to be static cached, using a new line for each URL entry. This should look like the following:

[code]
https://www.example.com/
https://www.example.com/sale
https://www.example.com/products
[/code]

Save the file, then create a file with the above script, and save it as nexcess_static_cache_helper.sh, or whatever name you prefer.

Make the file executable:
[code]chmod +x nexcess_static_cache_helper.sh[/code]

When ready, run the script, but replace use your own file name and actual domain name:
[code]./nexcess_static_cache_helper.sh example.com cache_list.txt[/code]

This will create a static_cache directory under your webroot containing the HTML documents to be used for the static cache. Additionally, your terminal will show output resembling:
[code]
########
# START: Static cache URL Rewrites generated on Mon Sep 18 15:49:11 EDT 2017
########
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?/$ [NC]\n
RewriteRule .* https://www.example.com/static_cache/index [R=302,L]\n
RewriteCond %{REQUEST_URI} ^/?sale$ [NC]\n
RewriteRule .* https://www.example.com/static_cache/sale [R=302,L]\n
RewriteCond %{REQUEST_URI} ^/?products$ [NC]\n
RewriteRule .* https://www.example.com/static_cache/products [R=302,L]\n
######
# END: Static cache URL Rewrites generated on Mon Sep 18 15:49:11 EDT 2017
######
[/code]

Copy the above block and paste it at the top of your .htaccess file for the site, which should generally be in your current directory.

The static cache should now be enabled. In the spirit of good housekeeping, delete the files we generated in previous steps:
[code]rm nexcess_static_cache_helper.sh cache_list.txt[/code]

How to Disable Static Caching

To disable static caching, remove the block of code placed within the .htaccess file.

Additionally, if you need to regenerate the files, run the script again and it will replace the HTML documents with newer versions. I’d recommend replacing the .htaccess code block as well, just to update the timestamp for future reference.

Now You Know

As stated previously, this is only a temporary method and should not be used as your primary performance optimization strategy. For long-term solutions, check out our optimization guide, implement a CDN, or contact our Support Team to explore additional options such as hosting Magento, WooCommerce, or WordPress.

Or, check out Nexcess' managed WordPress packages to see how you can optimize your site’s performance.

Nexcess
Nexcess

Nexcess, the premium hosting provider for WordPress, WooCommerce, and Magento, is optimized for your hosting needs. Nexcess provides a managed hosting infrastructure, curated tools, and a team of experts that make it easy to build, manage, and grow your business online. Serving SMBs and the designers, developers, and agencies who create for them, Nexcess has provided fully managed, high-performance cloud solutions for more than 22 years.


We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.