New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement]: Enable forcing a refresh of cached analytics data #33221
Comments
This seems easier, so I would probably lean toward this option. |
I am not a huge fan of this one, which makes me lean towards number two, but that does feel like a big gradual overhaul. For simplicity sake could we actually partly use this point:
But instead of calling We will probably need this logic at some point anyway, so it won't cause to much tech debt. If we aren't going the DataStore restructure route. |
I like this! Although, the place where the query args are normalized and the cache key is generated is still down in the datastore, and |
Can we just bypass the cache retrieval here?
So we force the report to be generated even if the cache exists, which will then update the cache entry. |
Yep. The question is, how do we get the boolean value specifying that bypass from the API controller to that point in the logic? |
Thinking about this more, I suspect option 1 is the safest approach. If we start changing interfaces or the visibility of methods, and some plugin is extending the datastore class, that would potentially cause fatal errors (see #33039). |
I was thinking we could add the
I would be fine with this, as it is the safest option. But I get a sense we might end up changing this down the road, as we will always have to make sure to unset it for it to actually work. (especially if our plan is to do more cache related work and optimizations). |
I think that's a good call @coreymckrill, especially since part of the goal here is to avoid doing too much refactoring. We can always plan out a larger refactor later if we need to. |
I think we can modify the method This way we're not introducing brittle logic around unsetting query params. I'm not sure if |
…3325) Adds a new collection parameter to all Reports API endpoints that utilize caching, `force_cache_refresh`, which will cause the current request to bypass the cache, re-run the queries for the requested data, and overwrite the previous cache entry with the new results. Note that this doesn't invalidate the entire cache, only the entry for the particular set of collection parameters and values specified in the request. This also adds a way to include debugging information related to the cache in the API response. Modeled after the way the Query Monitor plugin adds such information, you can get this by including an `_envelope` parameter in your API request. The debugging info includes whether the cache has been disabled via filter (`should_use_cache`), whether the `force_cache_refresh` parameter was used, whether the returned data was a `cache_hit` or not, and an array of the query parameters that were actually used to create the cache key. Closes #33221
Describe the solution you'd like
The WooCommerce mobile app displays order-related analytics data on the home screen for the current day, current week, current month, and current year. While viewing this data, you can "pull to refresh", causing the app to send a new request to the API to retrieve the latest version of the data. If nothing has changed on the store since the last request, the API will theoretically grab the data from the cache instead of re-running the queries, and this is how we usually want it to work. However, if for some reason the cached data is stale, it would be useful to have a way to force a refresh, meaning re-run the queries and then replace the stale cached data with the new data.
This would require adding a parameter to each analytics API endpoint, something like
force_cache_refresh=true
, which would trigger some kind of mechanism for skipping the retrieval of the cached data.The
force_cache_refresh
flag needs to find its way from the API endpoint controller to theget_data
method in the relatedDataStore
class (example). Ideally, since this flag doesn't actually affect the query itself, it would be separate from the$query_args
array. However, that would require changing the method signature, and thusDataStoreInterface
, which would necessitate changing the method signature in every class that extends that interface. Instead, I think we could either:$query_args
array, and potentially unset it before it actually gets fed into the query object (to avoid it becoming part of the cache key).ReportsDataStoreInterface
and defineget_data
with an expanded signature.Once the flag has made its way into
get_data
it's just a matter of checking for its presence before running theget_cached_data
method. If that gets skipped, we just continue on to run the necessary queries, compile the data, save it to the cache, and return it, thus "refreshing" the data.Describe alternatives you've considered
woocommerce_analytics_report_should_use_cache
filter hook to prevent pulling the data from the cache. This gives us a simple way to toggle the cache off, but the problem is that there wouldn't be an opportunity to toggle the cache back on in time to save the re-queried data. So this would allow us to skip the cache, but the data in the cache would continue to be stale.per_page={random int}
), which generated a new cache key. While this worked in a pinch, it's certainly a non-ideal hack, and shouldn't be necessary.force_cache_refresh
flag from the API controller down into the datastore, we could simply use it to conditionally callCache::invalidate()
, which would wipe the entire reports cache. However, this seems like an efficiency killer, as not all cached data becomes stale at the same time.Additional context
The text was updated successfully, but these errors were encountered: