A critical part of any software development process is the maintenance and enhancement of web application performance. It’s not something that’s done every now and then or considered at the end – it’s a never-ending process of optimizing and striving to build the most efficient and lean codebase. Throughout this process, the use of optimization technologies such as dynamic content acceleration (DCA) tools is prevalent.

Web developers commonly employ DCA solutions to significantly improve the performance of web applications. These solutions streamline the workload of web servers by providing a more efficient, reliable, and faster network infrastructure.

In the case of LiteSpeed servers, a built-in DCA known as LSCache has the features you need to reduce the loading time of your web pages.

What is LSCache?

LiteSpeed Cache, or LSCache, is a built-in, high-performance dynamic content acceleration feature available on the LiteSpeed server stack. It works by eliminating extra layers of reverse-proxy in websites, thus, optimizing the speed of accessing web content.

LSCache Module vs. LSCache Plugin

When developers mention the word LSCache, they are generally referring to the module, which, as mentioned before, is built-in to the LiteSpeed server stack itself. While you are busy building all your Laravel web app functionalities, the LSCache module works behind the scenes in caching your web resources to improve its speed.

The automatic caching of the LSCache module may sound pretty convenient, but what if you want to control the module? Say, for instance, you want to instruct it on what resources to cache and how long you intend to cache them? Well, that’s where the LSCache plugin may come into action.

The LSCache plugin is an add-on interface that allows you to utilize your module. Through this plugin, you’ll be able to directly manage how your web resources are cached.

Why Should You Use LSCache Plugin for Your Laravel Web Applications?

Web applications, like Laravel apps, are recreated every time users visit them, resulting in an increased round-trip time (RTT). RTT is a metric that determines how long a request gets submitted from a client to its server and back. 

An increase in RTT means a slower loading time. To prevent this aftermath, developers utilize the LSCache plugin. This highly customizable plugin offers developers an interface to easily configure website caches, thereby optimizing web apps and reducing the RTT.

What is the Server-level Requirement of LSCache Plugin for Laravel?

The LSCache plugin is simply an interface that will communicate to the LSCache module of your web server. For this plugin to work, ensure that your Laravel web application is running on a LiteSpeed server.

How to Install the LSCache Plugin to your Laravel Application?

To install the LSCache plugin in your Laravel application, follow the steps below.

Step 1: Obtain a LightSpeed-powered hosting or server for your Laravel application.

LSCache plugin will not work unless your Laravel app is powered by LiteSpeed. View your options through LightSpeed’s official hosting providers. You can also obtain your own LiteSpeed server via LiteSpeed’s download page. 

Step 2: Configure your server’s cache policy.

Once you’ve acquired your server, you will need to configure its cache root and cache policy. Cache root refers to the storage path of the object files the module will cache. Cache policy, on the other hand, is a group of settings that manages the cache behavior. 

You will also need to enable the cache engine of your virtual hosts by including the following code in your vhost’s config file:

<IfModule Litespeed> 
CacheEngine on
</IfModule>

Step 3: Verify if LSCache is Working.

Now that you’ve configured the cache settings, you’ll need to verify if your Laravel website is being cached. You can accomplish this by using your browser developer tools. To do so, follow the steps below:

  1. Navigate to your website using your browser and access the Network tab from your developer tools. This tab can be viewed by right-clicking and choosing Inspect.
  2. Refresh the web page and you’ll notice that requests get submitted.
  3. From the list of requests, click the first resource and you should see x-litespeed-cache: hit in your response header. This means that LSCache is correctly configured on your web page. An example is shown below for your reference:
How to configure LScache in laravel apps

Step 4: Install the LSCache plugin for Laravel using Composer.

When you’re certain that LSCache is working properly, it’s time to install the LSCache package for Laravel using Composer. You can easily require this package through this script:

composer require litespeed/lscache-laravel

In later versions of Laravel, an auto-discovery feature was made available, so you don’t need to make changes when you install the LSCache plugin. For earlier versions, particularly between 5.1 and 5.4, the auto-discovery feature is not yet present, so you’ll need to perform the following additional steps:

  1. Add the code below in the aliases section of the config/app.php file:
'aliases' => [
   ...
   'LSCache'   => Litespeed\LSCache\LSCache::class,
],
  1. You will also need to include the following middlewares under $middleware and $routeMiddleware of the app/Http/Kernel.php:
protected $middleware = [
   ...
   \Litespeed\LSCache\LSCacheMiddleware::class,
   \Litespeed\LSCache\LSTagsMiddleware::class,
];

protected $routeMiddleware = [
   ...
   'lscache' => \Litespeed\LSCache\LSCacheMiddleware::class,
   'lstags' => \Litespeed\LSCache\LSTagsMiddleware::class,
];
  1. After including all the scripts above, you’ll need to copy the config/lscache.php file into your config/ directory.

The last step that you must do is to enable the CacheLookup. In your .htaccess level or vhost, include the following codes:

<IfModule Litespeed>
   CacheLookup on
</IfModule>

You should now be able to configure your LSCache after successfully installing the plugin.

How to Configure LSCache Plugin for Laravel?

The LSCache plugin for Laravel comes with three functionalities—setting cache control headers, setting specific tags, and purging.

Step 1. Setting the Cache Control

You can configure the settings of your LSCache in the config/lscache.php file. From here, you can control the TTL (Time to Live), ESI (Edge Side Includes), and the default cacheability:

  • LSCACHE_DEFAULT_TTL – Set how long a cache lasts in seconds. The default for this setting is 0.
  • LSCACHE_ESI_ENABLED – This setting allows you to enable ESI. ESI lets you fragment web pages, providing a faster and more efficient way of caching them. You can set either true or false values to enable or disable this setting. By default, this setting is configured to be false.
  • LSCACHE_DEFAULT_CACHEABILITY – In this setting, you can set the default cache setting such as  private, public, no-cache, or no-vary.
  • LSCACHE_GUEST_ONLY – You may also want to configure the cache if it can be enabled for guests only through this setting. It also accepts true or false, with the latter as the default value.

Middleware is used to configure the cache-control header. You can override the settings in your Laravel routes similar to the codes below.

The first route ‘/’ uses the default cache-control header in your config/lscache.php. For the ‘/account’ route, the ‘lscache:no-cache’ header prevents the route from being cached. 

In the case of the ‘/contact’ route, notice that three settings have been overridden. The route has been set to have a max-age of 10 seconds (TTL), a private cacheability, and an enabled ESI.

Step 2. Setting Specific Tags

Sometimes, it is a good practice to assign tags to web pages so that you can easily target them the moment you want to purge the cache. You just need to include the lstags middleware in your route, as shown in the example below.

Step 3. Purging

Suppose data gets updated in your server and you want to remove an existing cache to recache the new data. In which case, you just need to purge the existing cache first. You can do this by configuring your controller to use the purge method:

The purge() method lets you purge a specific page. If you want to purge all pages, you can use the purgeAll() or purge(*) instead. You can also purge pages with a specific tag by including the tag keyword inside the purge method, as in:

LSCache::purge(‘tag=products’);

Conclusion 

Configuring LSCache in your Laravel applications may be a bit tricky, but you’ll get the hang of it eventually, especially when you fully understand why it is needed. Throughout this article, we provided all the essentials of the LSCache plugin, how to configure it, and, more importantly, its impact on your Laravel apps.

There are numerous tools out there that you can use to cache and optimize your web pages, and it might thrill you to know that LSCache is among the best!

Recommended read: What is Laravel and how to use it?