How to Purge Private Cache for a Specific User in LiteSpeed Cache
Content
Managing cached content for logged-in users is a common challenge for WordPress site administrators. A frequent question that arises is how to clear the private cache for a single, specific user without affecting the cache for all other users. This is particularly important on sites where user-specific content, like role-based product visibility or private messages, updates frequently.
Why You Can't Purge a Single User's Cache by Default
Based on community discussions, the LiteSpeed Cache plugin's native functionality does not allow for purging a single user's private cache. The private cache is typically labeled with a general "private" tag. This means the litespeed_purge_private hook purges all private cache for every logged-in user simultaneously. For many sites, this "all or nothing" approach is inefficient and can negatively impact performance if done frequently.
A Workaround: Tagging and Purging by User ID
While not a built-in feature, a solution exists by manually tagging a user's cache and then purging by that specific tag. This method was shared by a community member and involves adding two code snippets to your theme's functions.php file or a custom plugin.
Step 1: Add a User-Specific Cache Tag
The first function adds a unique tag to every page served to a logged-in user. This tag identifies the cache object as belonging to that specific user.
function add_user_cache_tag() {
if ( is_user_logged_in() && defined('LSCWP_V')) {
$user_id = get_current_user_id();
do_action( 'litespeed_tag_add', 'user_id_' . $user_id );
}
}
add_action( 'init', 'add_user_cache_tag' );
Step 2: Purge Cache by the User Tag
When you need to clear the cache for a specific user (e.g., after updating their profile or user role), you can trigger a purge using their unique tag.
$user_id = 123; // Replace 123 with the actual user ID
do_action( 'litespeed_purge', 'user_id_' . $user_id );
You would integrate this purge action into your own code, perhaps hooking it into events like profile_update or set_user_role.
Important Considerations and an Alternative
- Testing is Crucial: This is a community-provided workaround. Always test this code on a staging site before deploying it to production to ensure compatibility with your specific setup.
- Alternative Approach - Disable Caching for Logged-in Users: If purging individual user caches seems too complex for your needs, a simpler alternative is to disable caching for logged-in users entirely. This can be done in the LiteSpeed Cache settings under Cache -> Cache -> Cache Logged-in Users. Alternatively, you can exclude specific user roles from being cached under Cache -> Excludes -> Do not cache roles. This ensures users always see dynamic, uncached content, which is ideal for sites with highly personalized pages like forums or membership portals.
This approach provides a powerful way to manage private cache more granularly, enhancing site performance while ensuring users see up-to-date content.
Related Support Threads Support
-
Purge cache for a single user’s private cachehttps://wordpress.org/support/topic/purge-cache-for-a-single-users-private-cache/
-
Purge cache on user’s edithttps://wordpress.org/support/topic/purge-cache-on-users-edit/
-
Clearning cloudflare when clearing all cache?https://wordpress.org/support/topic/clearning-cloudflare-when-clearing-all-cache/
-
Serving the public cache to logged in usershttps://wordpress.org/support/topic/serving-the-public-cache-to-logged-in-users/
-
Allow cache purging for custom roles without ‘manage_options’ capabilityhttps://wordpress.org/support/topic/allow-cache-purging-for-custom-roles-without-manage_options-capability/
-
show public cache to logged in usershttps://wordpress.org/support/topic/show-public-cache-to-logged-in-users/
-
Selective Purge by Post ID for Public & Privatehttps://wordpress.org/support/topic/selective-purge-by-post-id-for-public-private/
-
Feature Request – Purge cache links on admin list/edit posts etchttps://wordpress.org/support/topic/feature-request-purge-cache-links-on-admin-list-edit-posts-etc/
-
Disabling registered usershttps://wordpress.org/support/topic/disabling-registered-users/
-
Clear cache per user?https://wordpress.org/support/topic/clear-cache-per-user/
-
Private cache problemhttps://wordpress.org/support/topic/private-cache-problem/
-
Suggestion: Clear object cache after significant changes, add clear cache optionhttps://wordpress.org/support/topic/suggestion-clear-object-cache-after-significant-changes-add-clear-cache-option/