This is an old revision of the document!


LSCache + Craft CMS and the Purge Cache Plugin

LSCache, similar to Varnish cache, is a simpler and more efficient page caching solution built-in to LiteSpeed Web Server. When used in conjunction with Craft CMS, you can expect significant performance gains with a quick and easy setup. Rewrite rules can be used to customize LSCache's behavior.

With the addition of the 3rd party “Purge Cache” plugin, cache management is made even more flexible, allowing you to set longer TTLs for your content.

Learn how to enable LSCache and the Purge Plugin, and get them working with your Craft CMS site(s) below.

Server/VirtualHost-level cache storage needs to be configured properly for your environment. Select your server setup from the Web Server Configuration section of our LiteSpeed Cache Installation Guide and follow the instructions to set the server/virtual-host-level cache root and cache policy.

Rewrite rules should be added to your .htaccess file, located in the document root of your website. It may contain the following:

<IfModule mod_rewrite.c>
      RewriteEngine On
      # Send would-be 404 requests to Craft
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
      RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

Place rewrite rules above the existing rules to cache all URLs. The following rules will cache all pages for 8 hours (28800 seconds) with the exception of /admin URLs:

########## Begin - Litespeed cache
<IfModule LiteSpeed>
  RewriteEngine On
  RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
  RewriteCond %{ORG_REQ_URI} !/admin
  RewriteCond %{ORG_REQ_URI} !/index.php/admin    
  RewriteRule .* - [E=Cache-Control:max-age=28800]
</IfModule>
########## End - Litespeed cache

Open your browser's inspector by right-clicking and selecting “Inspector” or by pressing the F12 key, and refresh the page. Under the “Network” tab look for the HTML page you just loaded and click on it to view its response header.

In the response header you should see X-LiteSpeed-Cache: hit to indicate that the page was served from cache successfully. If you see X-LiteSpeed-Cache: miss, reload the page and check again.

The ability to configure an LSCache purge allows you to specify longer caching periods for your content. For example, you can confidently set max-age to several hours, if you know that pages will be automatically cleared from cache if they are changed in the CMS. The ability to purge the cache eliminates the fear of serving stale content.

This is a 3rd Party plugin. Our thanks to Thoughtful, the developer!

Requirements

Craft CMS version 3.0.0 or later

Installation

To install the plugin, search for LiteSpeed Cache on the Plugin store, or install manually with the following instructions:

Open your terminal and go to your Craft project:

cd /path/to/project

Then tell Composer to require the plugin:

composer require thoughtfulweb/lite-speed-cache

In the Control Panel, go to Settings → Plugins and click the “Install” button for LiteSpeed Cache.

Usage

There are two ways to purge the cache. You can configure it to purge automatically when pages are saved in the CMS, or you can press a button to purge the entire cache at once.

On Page Save

Choose whether or not to clear caches by URL, and set the directory where your LSCache folder is located in the plugin settings. If you do not select the per-URL option, all of the cached content in the LSCache folder will be removed on every page save.

Manually

If you just want to purge the entire cache folder at once, you can Navigate to Force Clear LiteSpeed Cache in the plugin's CP section. Clicking the Clear button will remove all of the cached content in the LSCache folder.

We recommend checking the Github page for examples.

I set rewrite rules according to the wiki, but my admin pages are cached anyway

LSCache could be incorrectly enabled globally at the server level. You can either correct this by setting “Enable Public Cache” to “not set” or “no” according to this recommendations, or you can disable the global setting for a particular virtual host in .htaccess first before the rewrite rules, like so:

########## Begin - Litespeed cache
<IfModule LiteSpeed>
 RewriteEngine On
 CacheDisable public /
 RewriteCond %{REQUEST_METHOD} ^HEAD|GET$
 RewriteCond %{ORG_REQ_URI} !/admin
 RewriteCond %{ORG_REQ_URI} !/index.php/admin    
 RewriteRule .* - [E=Cache-Control:max-age=28800]
</IfModule>
########## End - Litespeed cache

Note: The CacheDisable public / directive is used to help protect against globally enabled caching. The rewrite rules will enable caching for your site.

  • Admin
  • Last modified: 2018/05/24 14:26
  • by Lisa Clarke