WpW: Understanding Cache Storage

May 2nd, 2018 by LSCache 0 Comments

WordPress Wednesday: Managing LiteSpeed Cache Storage

Welcome to another installment of WordPress Wednesday!
Today’s topic is: Managing LiteSpeed Cache Storage

Today, we’re going behind-the-scenes of the LiteSpeed Cache for WordPress plugin, and we’re going to discuss the physical storage of your cached content. That is, where it lives in your file system, how LiteSpeed Web Server manages it, and how you can manage it yourself, if you wish.

How Caching Works

When a page is cached through the LSCache plugin, a static copy of that dynamically-generated page is saved in the cache root directory. The location of the cache root directory varies from hosting environment to hosting environment, and it may be overridden at setup time. For instance, a cPanel user’s default cache root is /home/username/lscache/. This gives that user control over it, and keeps it out of reach of others.

If you don’t know the location of your cache root, and you’d like to, you should contact your hosting provider.

As your site is cached, either through visitor request or through the crawler, LiteSpeed Web Server fills up the cache root directory with static copies of all of those requested pages, until all of the site’s cacheable pages are stored.

When using LSCache, you don’t have to worry about your cache storage maintenance. LiteSpeed Web Server does all of this for you in a light and efficient way. Once your cache is fully warmed up, the cache directory size will not grow any further.

When Cache is no Longer Valid

Managing LiteSpeed Cache Storage: A cache of buttons in three mason jars

Eventually, every cached page will become invalid. This happens when a page reaches its given TTL (Time to Live), or sooner if it has been purged from the cache by hand or by an LSCache purge rule.

When LSWS receives a purge request, it will simply mark the related cached objects as invalid. In order to avoid heavy disk operation, it does not delete the actual cache files on disk.

Instead, cached pages are only deleted from the directory when they reach their natural TTL. This means that there may sometimes be expired or invalid items stored in the cache directory. This is ok, because LSWS is keeping track of which entries are still good and will only serve those. Invalid pages are never served, even if they are still physically stored in the directory.

How LSWS Maintains Cache Storage

To understand how LiteSpeed Web Server keeps the cache directory cleaned up, it’s helpful to first define some terms:

  • Cached Object: A cache entry stored in LSCache. A cached object maps to a URL plus an optional variation. For example, if one URL has a desktop view and a mobile view, there will be two cached objects for that URL: one for desktop, and one for mobile. A cached object can be full page content or content from an ESI block.
  • Cache Manager SHM: Shared memory that the cache manager uses to store all of the information about the cache entries. It is a memory-mapped file persistent to disk. The default location is /dev/shm/lsws, but you can point it to a RAM disk for faster access. Just keep in mind that the data on a RAM disk will be lost if your physical server reboots. (LSWS restart will not affect RAM disk storage.)
  • LRU: Least Recently Used cache replacement.

Each cached object has a corresponding entry in the cache manager SHM. LSWS will check periodically (triggered by a timer) for expired cache entries based on the entries’ defined TTLs. LSWS will select a list of entries based on LRU, and delete the related cache files on disk. Each time this happens, the batch size is small enough that it will not affect the whole server performance.

The clean-up process is based on TTL only, it does not check for purge events. Cached objects that were purged, will eventually be deleted when their natural TTL occurs, if they are not re-cached in the meantime.

Do It Yourself

Managing LiteSpeed Cache Storage: A cache of buttons in an open mason jar

So, now you know that when you press the Purge All button, you will not see the cache storage folder get emptied. This is ok! You don’t have to worry about the actual files on disk. LSWS takes care that for you constantly and quietly behind the scenes.

However, if you do want to remove the files yourself, you can delete your cache storage folder, or use our utility command:

lsws/admin/misc/cleancache.sh

Never try to re-create the cache storage folder manually. If it’s missing, LSWS will create it for you, in the path you have defined, and assigning it the proper permissions.

Please don’t delete the cache manager SHM file. If you delete that, you will have to restart LiteSpeed Web Server.

Have some of your own ideas for future WordPress Wednesday topics? Leave us a comment!

Don’t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:


Categories:LSCache

Related Posts


Comments