Tag: caching 101

  • WpW: LiteSpeed Caching 101

    WpW: LiteSpeed Caching 101

    WordPress Wednesday: LiteSpeed Caching 101

    Welcome to another installment of WordPress Wednesday!
    Today’s Topic: Understanding LiteSpeed Cache

    Previously in our Cache 101 series, we discussed the basics of caching. Be sure to take a look at that, if you missed it, because today we are going to go more in-depth, and discuss LiteSpeed Cache in particular. We’ll talk about how it works, how it is different than other WP caching solutions available, and what that really means for your WordPress site.

    LiteSpeed Cache

    As we mentioned, LSCache is a server-side page cache. This essentially means we are storing static snapshots of the pages on your WordPress site. We can do this because the dynamically-generated pages are effectively the same each time they are built. Why generate the same page multiple times, when you can do it once and save the results at the server level? Rather than making your visitors wait for WordPress and PHP to assemble the pages for each request, we can serve them the snapshots much faster.

    Filling up and using the cache

    LiteSpeed Web Server is required for the use of the LSCache for WordPress plugin. This is because the basic LSCache functions are actually carried out by the server. The plugin is provided as an easy way for a site admin to communicate with the cache engine, but it doesn’t actually execute any of the caching tasks, and the cache files themselves are not stored within the WordPress file structure.

    LiteSpeed’s cache can be populated in two ways: behind the scenes through the use of a crawler, or through visits to the site. We’ve written a comprehensive overview of our crawler functionality before, so for now let’s just focus on a basic example where a couple of users drop by to check out your site.

    Visitor #1:

    1. lands on your site and requests a dynamic page to view.
    2. The request goes to the server, which checks to see if the page is in the cache.
    3. It is not, so the request goes to WordPress.
    4. The WordPress backend processes all of the PHP code and generates an HTML page
    5. The HTML page is served to the visitor.
    6. The page is also stored in the cache.

    Visitor #2:

    1. lands on your site and requests the same dynamic page to view.
    2. The request goes to the server, which checks to see if the page is in the cache.
    3. This time the server finds the page in cache
    4. The cached page is quickly served to the visitor.

    You can see, when the page was cached after Visitor #1 requested it, that things really improved for Visitor #2 (and presumably, every subsequent visitor as well). That is the benefit of caching, in a nutshell.

    As long as a page remains in cache, all visitors will have the same experience as Visitor #2. Once some event occurs that purges the page from the cache, the process begins again. The next visitor after a purge will have the same experience as Visitor #1 did: waiting for the backend to process the request and store an updated version in the cache.

    Emptying the cache

    Understanding LiteSpeed Cache

    Pages cannot remain in the cache indefinitely, so while visitors keep triggering more and more additions to the cache, LSCache relies on a set of rules to tell it when to remove pages. A page removal (or, purge) occurs when certain activities trigger it, or when the page has been in the cache long enough to expire.

    Purge through expiration

    The most common way for a page to be removed from the cache is for its TTL to expire. Each page has a TTL (Time to Live) defined, and when that time is up, the page is purged from the cache. As site admin, you have control over your pages’ TTLs, and it makes sense to choose a time that is as long as possible without impacting your site’s functionality.

    For example, if you have a site with a few thousand pages, most of which never change, then you can set your TTL to something quite long, like a week, or maybe even a month.

    On the other hand, if your site is frequently being updated, you’ll want a TTL that isn’t very long, like a day or 8 hours.

    Purge through activity

    There are certain events that are built in to LSCache that trigger a purge. If a post is edited, saved, deleted, trashed, or an attachment is deleted, then that post is purged from the cache.

    Additionally, LSCache has a really nifty tagging system that allows pages to be grouped together and purged all at once after a triggering event. Here’s an example that shows the “smart purge” system in action:

    You publish the following post:

    Title: Hello, World!

    Category: Greetings

    People visit your site, the post ends up in the cache.

    You then edit the post and change the Title and add a second Category:

    Title: Hey, World, How Are You?

    Category: Greetings, Questions

    Since there have been changes to the post, the following pages are purged from the cache according to the rules set up in LSCache admin:

    • The post itself
    • The front page and/or home page of your site
    • The author archive page
    • The post type archive page
    • The monthly archive page
    • The “greetings” and “questions” category pages

    You can change the rules to purge even more pages than this (or fewer), if it’s appropriate for your site.

    This purge tag functionality is very powerful. It means that LSCache can do a targeted purge like we saw above, removing the edited post, as well as every other page that is influenced by that post. Without a tagging system, other plugins can either purge too little (just the post) or too much (all of the site’s pages).

    This functionality allows you to specify relatively long TTLs for your pages, knowing that LSCache will step in and purge any pages that are affected by recent activities.

    Some things cannot be cached

    Not every page on your site is cacheable. As of version 1.1.2, these are some of the pages that LiteSpeed Cache considers uncacheable, a few of which are configurable in WP-Admin:

    • Admin pages
    • Post requests
    • Pages where no theme is in use
    • Any page that is specified as “do not cache” in WP-Admin
    • Any post that has a category or tag that is specified as “do not cache” in WP-Admin
    • Any request that has a cookie or user agent that is specified as “do not cache” in WP-Admin

    Public cache vs. private cache

    As of this writing, the LiteSpeed Cache for WordPress plugin only deals with public cache. This will be changing soon, and we’ll update here when it does. But for now, let’s talk a bit about the difference between public and private, and what that means in a caching context.

    [UPDATE: Private cache is available now.]

    Public cache is cache that can be accessed by everyone. A single copy of a publicly-cached page is stored and served to everyone who requests it.

    Private cache, on the other hand, is only accessible to an individual visitor. It usually contains information that is only pertinent to that particular user (for instance, shopping cart contents, or a personalized greeting). When a page is privately-cached, there is a separate copy stored for each user that requests it.

    As previously mentioned, the WP plugin currently only deals in public cache, and so any page that has private data on it, is just not cached at all.

    When a user is logged in, there is the potential for every page to have private data on it, and so pages are never cached for logged-in users.

    We are very close to releasing a new version of the plugin that will support Edge Side Includes (ESI). This is technology that allows us to “punch holes” into publicly-cached pages, and treat those areas differently, whether that be to store them in private cache or to not cache them at all. We will have a lot more to say about this when it’s ready, so stay tuned for that!

    [UPDATE: ESI is available now.]

    LiteSpeed Cache vs. Other Plugins

    Understanding LiteSpeed Cache

    As we discussed earlier, LSCache is an integral part of LiteSpeed Web Server. The plugin is merely the channel through which a WP admin can communicate with the server. This is not the way that other WP cache plugins operate. Other cache plugins don’t have the luxury of such a close relationship with the server. They must sit on top of the server, while LSCache is integrated directly into it.

    This is an important distinction. To understand why this is so, we need to look at how WordPress caching works for just about every plugin (including ours):

    The most effective WordPress caching plugins use rewrite rules to manage the cache.

    The rules tell the server where to look for cached content. Plugins can place pages in the cache by writing to the file system, and they can purge pages from the cache by deleting the files. They can tell whether or not a page is cached by checking for the presence of the file. And they can tell if a cached page has expired by comparing the file time to the page’s TTL.

    Pretty much all cache plugins can do these things, but that is the extent of their ability to manage the cache. If a plugin wants to avoid the expense of invoking PHP, then it cannot “remember” information about the cache entries between user sessions. Either the cached files exist, are fresh and can be served, or they don’t exist and/or cannot be served. That’s it.

    LiteSpeed Cache can go further. Because the cache is integrated into the server, LSCache can remember things about the cache entries that other plugins cannot, and it can analyze dependencies. It can utilize tags to manage the smart purging of the cache as we discussed earlier, and it can use vary cookies to serve multiple versions of cached content based on things like mobile vs. desktop, geographic location, and currencies.

    Through its tight integration with the server, LiteSpeed Cache provides powerful cache management tools that are simply not possible for the other plugins to replicate.

    LiteSpeed Cache’s relationship with LiteSpeed Web Server translates into superior performance, as our benchmarks can attest. Even better, see what other people are saying!

    If your WordPress blog is powered by LiteSpeed Web Server, and you haven’t given our cache plugin a try yet, I hope we’ve convinced you by now that it’s worth a try!

    Conclusion

    And thus ends our Caching 101 series. That wasn’t too painful, was it? You should now understand how LiteSpeed Cache works, and why it’s so unique among the available plugins. Now that we’ve got this basic foundation down, we can start talking about more advanced topics that rely on this knowledge. For example, we’d like to share some tips for plugin authors who want to maintain compatibility with LSCache. You can’t talk about that without understanding a bit about how LSCache does what it does! Keep an eye out for that topic (and others like it) in future installments.

    In the meantime, if you have questions, please don’t hesitate to leave a comment. We’d love to help!

    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:

  • WpW: Caching 101

    WpW: Caching 101

    WordPress Wednesday: Caching 101

    Welcome to another installment of WordPress Wednesday!
    Today’s Topic: What is Caching?

    Today we are going to begin a series-within-a-series, and talk about the basics of caching: what is a cache? Why is it beneficial to cache your WordPress site? What types of caching are available to web applications? And then we’ll get into the basics of our own caching solution: LiteSpeed Cache, affectionately referred to as LSCache for short. We’ll also talk about how LSCache is different than other WP caching solutions available, and what that really means for you.

    It’s a meaty topic, so we’re going to split it up into a few smaller chunks for easier digestion. Today, let’s start with the basics of web app caching, as they pertain to WordPress.

    What is Caching?

    Generally speaking, a cache is a mechanism for storing data in such a way that it is easier or faster to retrieve than the original source.

    WordPress sites consist of dynamic pages that are built with PHP. The pages of a WP site don’t exist anywhere in the file system; they are constructed on-demand through PHP, and then served to the visitor as HTML. Along this path, there are a few places that are resource-intensive and ripe for the caching.

    Types of Cache

    Cache can be stored on the visitor’s own device (client side), on the website’s network (server side), or halfway around the world (via a content delivery network). Some sites may employ all of these methods, while others have the need for very few.

    Client-side caching

    All modern browsers support client-side caching. Your browser knows that certain files rarely change. Files like CSS, Javascript, and images can all be stored locally in your browser’s cache so that they can be accessed quickly and easily without being re-downloaded from the server.

    Server-side caching

    What is Caching

    Most of the work of generating and delivering a web page is done on the server side, so it make sense that more of the opportunities to increase efficiency would also be here. There are four major types of server-side caches available to WordPress sites.

    database cache

    A database cache saves time and resources related to the execution of database queries. WordPress relies on its database for basic information about the site, and it asks for this same information often. A database cache stores the query results so that they may be referenced again and again without the need to repeatedly access the database.

    object cache

    An object cache saves time and resources related to the computation of objects. WordPress has a built in object cache mechanism with a defined class and set of functions that can be used to add, access, remove, or otherwise manipulate the object cache. An object cache doesn’t stick around for long. By default, it’s only relevant for the duration of a single request. It’s used to store data that is needed multiple times in a request, but is considered expensive to re-compute. Often, the object cache stores database queries, which is why it is sometimes mistaken for a synonym of “database cache.”

    opcode cache

    An opcode cache saves time and resources related to the execution of PHP code. Before PHP can be executed it must be compiled. An opcode cache stores this compiled code at the server level so that it may be reused without recompiling. The next time a request is sent that requires that PHP code to be executed, it can save the compilation step and just run the code that was saved in the opcode cache.

    page cache

    LiteSpeed Cache is a page cache. Page caches, too, save time and resources, and they do so by storing the full content of dynamically-generated pages so that static copies may be served to the user. A page cache allows the server to bypass PHP and database queries altogether. The other types of cache can be helpful while building up your page cache, but once there is a fresh, well-managed page cache in place, there is little need for database, object, or opcode caches.

    Content delivery networks

    Content delivery networks (or, CDNs) store static website content remotely in servers all around the world. The content may then be served to a site’s visitor from the server that is physically closest to that visitor’s location. The closer the server, the less time the data takes in transit. CDNs are great when it comes to static content, but are lacking when you have a dynamically-generated site such as WordPress, as the content requests must still hit the WP backend.

    Until Next Time…

    So, there is your overview. At this point, you should understand what types of caching are available for your web apps, and why you might want to use them. Our next article in the Caching 101 series will focus specifically on our solution: LiteSpeed Cache. We’ll talk about how it works, why it’s different than other WordPress page-caching plugins, and what benefits those differences get you. Be sure to meet us back here for that!

    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: