Fix traffic boost suggestion cache refresh#3332
Fix traffic boost suggestion cache refresh#3332alecgeatches merged 2 commits intoadd/traffic-boostfrom
Conversation
📝 WalkthroughWalkthroughThe changes update cache expiration durations for smart link-related data, reducing them from one month to either one week or one day in two model classes. Additionally, a capability check is added before flushing cache groups to ensure compatibility. No public APIs or method signatures were altered. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant SmartLinkModel
participant Cache
User->>SmartLinkModel: Request smart link data
SmartLinkModel->>Cache: Check for cached data (shorter expiration)
alt Cache hit
Cache-->>SmartLinkModel: Return cached data
else Cache miss
SmartLinkModel->>SmartLinkModel: Generate data
SmartLinkModel->>Cache: Store data with new expiration
SmartLinkModel-->>User: Return generated data
end
sequenceDiagram
participant System
participant SmartLinkModel
participant Cache
System->>SmartLinkModel: Request cache flush by post ID
SmartLinkModel->>SmartLinkModel: Check if cache group flush is supported
alt Supported
SmartLinkModel->>Cache: Flush cache group
else Not supported
SmartLinkModel-->>System: Skip group flush
end
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure 📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (1)`**/*.{html,php}`: "Perform a detailed review of the provided code with following key aspects in mind: - Review the HTML and PHP code to ensure it is well-structured and adheres ...
⏰ Context from checks skipped due to timeout of 90000ms (2)
🔇 Additional comments (5)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
acicovic
left a comment
There was a problem hiding this comment.
Thank you so much for the detailed explanation. Works well in my testing!
Description
This fixes a regression introduced by the caching changes in #3293 (comment) that caused generated suggestions to improperly save due to a cache emptying error.
In short, we were using the
wp_cache_flush_group()function without the proper checks, which resulted in a stale cache value for suggestions. A more in-depth explanation is below.Generating suggestions flow (new post)
Here's how a request runs to generate suggestions:
User clicks "Boost Traffic" button on a new post.
The browser calls into the
traffic-boost/<post-id>/get-suggestionsendpoint to determine if suggestions already exist:This calls into the
get_existing_suggestions()REST handler.This goes into
Inbound_Smart_Link::get_existing_suggestions(), which eventually calls down intoSmart_Link::get_smart_links().No cache currently exists for the new post, so the code queries for pending smart links and saves the result to cache. As this is a new post, there are no pending links, so an empty array
[]is saved to cache.Because there are no existing suggestions, the browser makes a call to
traffic-boost/<post-id>/generate:This calls into the
generate_link_suggestions()REST handler.After generating new suggestions, the code calls into
Inbound_Smart_Link::delete_pending_suggestions(), because$discard_previousistrueby default.delete_pending_suggestions()then deletes any existing pending links (of which there are none) and callsself::flush_cache_by_post_id().Finally,
flush_cache_by_post_id()callswp_cache_flush_group()on the group cache for the post, which should clear out any existing cached data.The bug
The final step is where the bug happens. Looking at the documentation for
wp_cache_flush_group(), there's also this important caveat:Our code was not doing this step properly, and even though the
wp_cache_flush_group()function exists,wp_cache_supports( 'flush_group' )returnsfalseon WPVIP. Note that the prior code was correctly using this check, and this bug was introduced during the caching refactor.In step 2.3, the code saves an empty array to the post's suggestion cache, and step 3.4 doesn't actually clear that cache. In subsequent requests,
get-suggestionshas a cached value of[]which is returned. When the cache is properly cleared on step 3.4, the nextget-suggestionsrequest will make a new query and correctly return all of the existing suggestions.By properly clearing out cached post values one at a time, we bypass the caching issue and fix the bug.
Other changes
I reduced the
wp_cache_set()times from a maximum ofMONTH_IN_SECONDStoWEEK_IN_SECONDSorDAY_IN_SECONDS. This isn't strictly necessary, but I think a month of cache time is probably too long in retrospect for most of these queries. Shorter cache times also means that cache-related bugs will clear up quicker.How has this been tested?
This has been tested locally, and by pushing to a sandbox environment and ensuring caching works. To test locally, use this branch and clear your existing cache:
Alternatively, to test on a production or child environment on VIP, use this command:
Summary by CodeRabbit