WpW: Vary Groups

September 13th, 2017 by Cache , WordPress 0 Comments

Welcome to another installment of WordPress Wednesday!

It’s been a busy few weeks for LiteSpeed Cache for WordPress! We’ve released updates that have enabled private cache and ESI, both of which have been high on our customers’ wish lists. Today we’re going to talk about another feature that was enabled recently: Vary Groups.

The Vary Group functionality combines the concepts of “cache varies” and “user roles.” Let’s explore each of those ideas and then see what happens when you put them together.

Cache Varies

Sometimes you need to have multiple publicly-cached versions of a single page. We call this “varying,” and it can be used on many types of criteria. One popular vary built into the plugin allows you to specify a list of mobile user agents so that we can cache one view for mobile users and a different view for those on desktop.

User Roles

WordPress comes with a handful of built-in user roles: administrator, author, editor, contributor, and subscriber. Various plugins may create other roles as necessary.

Each of these roles gives a user certain permissions. Editors, for instance, can publish and manage the posts on a site, but they don’t have access to all of the site-management features that an Administrator does.

Vary Groups

With Vary Groups you can have multiple publicly-cached versions of a single page, based on the permissions of the users who view the page.

 

For example:

An Administrator has all kinds of abilities that the other users do not. They can edit and delete posts. They can manage categories, remove users, switch themes, and so on. In some themes, administrator functions will appear right on the public pages (like an “edit” link at the end of a post). If you create a Vary Group for administrators, then LSCache will save two public copies of the page: one with all of the editing permissions displayed on it for anyone in the administrator group, and the default copy of the page with none of the editing links for everyone else.

Here’s a more complicated example:

You have a shop running on your site, and through the shop admin, you’ve defined two user roles: retail_customer and wholesale_customer. You have two sets of prices, and three different ways that your site can be viewed: users in the group retail_customers will see your highest prices. Users in the group wholesale_customers will see your lowest prices. Users who are not yet customers will see the default page with no pricing whatsoever. To ensure that the three different views would be saved separately in the public cache, you would create two Vary Groups, one for each customer role.

An important distinction:

With Vary Groups, we’re not changing the behavior of your application at all. We are simply allowing you to have separate cached copies for each of the public views that are already being generated. Before we had Vary Group functionality, you would only have been able to cache the default view, which usually meant that you couldn’t cache pages at all for logged-in users. This new ability to cache all of your user-role-based content is a nice perk!

Setting it up

From your WordPress Admin, navigate to LiteSpeed Cache > Settings > ESI. Scroll down to the Vary Group setting.

You should see a list of the user roles that are defined on your site. Your list of user roles may be different than our example site’s list above, but don’t let that confuse you. It’s normal.

To create a vary group for any user role shown, enter a non-zero value into the box next to that user role. If a user role has a 0 next to it, then it will be served the default cached copy.

There is no significance to the numbers other than the fact that unique views should have unique values.

If two user roles share the same view, put them in the same group by using the same number. In other words, if author and editor both have the same view, but it’s different from everyone else’s view, author and editor could both be given the number 22, while everyone else remained at 0.

Why?

You may be wondering, why do this when you can just use ESI to adjust what displays on different people’s pages? Or cache the whole page in private cache?

While ESI is handy for situations where you want to insert a little bit of private content onto a mostly-public page, it can be resource-intensive, particularly if several blocks are required on the page.

Additionally, the situations that call for Vary Groups are not quite the same as those that benefit from ESI or private caching.

Use Vary Groups when your pages have multiple public versions that have been generated based on user permissions. The differences might be subtle, like on a shop page where the design is the same but the prices vary per user group. Or the differences might be drastic, going so far as to not show some content at all for certain user roles.

With Vary Groups, we cache one copy of the page per group. This is different than privately-cached content, which is stored as one copy of the page for each user that requests it. When you privately cache a page, you can potentially be caching thousands of copies of the same page! Reserve private cache for pages that truly serve up individualized content that varies from user to user.

Vary Groups are a useful tool in your cache toolbox. They use far fewer resources than private caches do, and are easier on the server than ESI is. If you have situations where different public content is being generated based on user roles, Vary Group functionality is your most efficient caching option.

If you have any questions about this, or are wondering what type of caching would benefit your unique situation, leave a comment and ask us!

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:Cache , WordPress

Related Posts


Comments