CDN - Cloud Network.

How to Create Cache Files Quickly on WordPress Hosting Server and CDN

In this article, I am giving a tip to how to create cache files quickly on the WordPress hosting server and CDN. No matter which cache plugin you are using or which CDN service.

Problem

You are using a cache plugin in WordPress to create static files for your dynamic content to increase performance. Also, maybe you are using a CDN for your WordPress blog. But suppose you have made some changes in your theme and now you need to clear all the cache files on your WordPress hosting and on your CDN as well so that users can see the changes you have made.

When I had to do this, it really gives pain to me, because it decreases the performance of my website for a while or for a few days. Because a cache file will be created especially on CDN when the first time a user opens a specific page. Then the next user will be served by that cache page if he/she requests the same page.

Solution

Then I was trying to find the way to how to create cache files quickly so that every user served fastly by the cache page. And what I found is the curl command.

To test, after deleting the cache from hosting server and from the CDN, I fetched a page from my website using the following command:

curl -svo /dev/null https://www.vinish.ai/dbms-output-package-in-oracle

First time I got the cf-cache-status: MISS. Then when I executed the same command again, I got the cf-cache-status: HIT. It means the cache file for the above page has been created on the WordPress hosting server and the CDN as well.

Then I thought that if I execute this command for all URLs (means all pages of my WordPress blog), then the cache files could be created quickly. So I used the following approach to do this.

Create Cache Files Quickly on WordPress Hosting Server and CDN

Create a text file containing all your website URLs. How to do this? The answer is to use your sitemap. Open your sitemap on a browser, for example, https://www.yoursite.com/post-sitemap.xml. Then copy all the URLs and paste it into a text file. Make sure that your text files should look like as shown in the below example:

myurls.txt

A text file screenshot.

Now on windows, create a batch file as shown in the below example:

create_cache.bat

@echo off
for /f "tokens=*" %%a in (myurls.txt) do (
  echo %%a
  curl -svo /dev/null %%a
)
pause

Now run the file by using the following steps:

  1. Open the Command Prompt on Windows.
  2. Move to your folder in which you created above two files — for example, CD C:\mysite\.
  3. Then type the batch file name to execute it — for example, create_cache.bat.

It will run the above program and will fetch every URL exists in the text file myurls.txt.

While the program is running, you can check your cache files location on your WordPress hosting server. For example:

/home/yourdomain/public_html/wp-content/cache/all

You will start finding all the files which are being crawled by the curl command.

Script for the Mac and Linux Systems

To run the curl command on Mac or Linux system, create the shell script as shown in the below example:

#!/bin/bash
input="myurls.txt"
while IFS= read -r var
do
  curl -svo /dev/null "$var"
done < "$input"

See also: