Caching frequent queries
Describe your question
Hi
I'm guessing that 'ep_wp_query_cached_posts' can be used to return a pre-saved cached result. I was planning to save the wp_query as a hash key so that the hash could be compared in 'ep_wp_query_cached_posts' but all of the hooks I've tried don't have the exact same wp_query so I cannot compare them. For example in 'ep_wp_query' the wp_query isn't exactly the same as in 'ep_wp_query_cached_posts' so they can't be compared.
Thanks for any info.
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
@jzzaj, that likely happens because we set a couple of attributes to the $query object (see here), namely found_posts, num_posts, max_num_pages, suggested_terms and elasticsearch_success. Unsetting those before generating the hash should do it. Did you try that already?
Hi filipeelia
I also had to remove 'ep_aggregations' and I've now matching wp_queries in 'ep_wp_query_non_cached_search' and 'ep_wp_query_cached_posts'.
At the moment I'm just testing, but the plan is to cache frequent queries in Redis.
If not already saved, I'm saving $new_posts in 'ep_wp_query_non_cached_search' and then retrieving them in 'ep_wp_query_cached_posts'. The problem is that when doing this no results are displayed on the web pages when using cached $new_posts.
I've compared a non-cached final version of $new_posts in get_es_posts() (in QueryIntegration.php) to a cached version and they are exactly the same. But the cached version doesn't display any results on web pages. The debug bar also shows no ES queries when using cached $new_posts.
Thanks for any advice.
It has been 3 days since more information was requested from you in this issue and we have not heard back. This issue is now marked as stale and will be closed in 3 days, but if you have more information to add then please comment and the issue will stay open.
@jzzaj do you mind sharing your code?
@felipeelia Yes but before I'm going to test on a new install of wordpress using the default theme. If it's still the same I'll share the code. Thanks.
@felipeelia I have now tested on a clean install of Wordpress, WooCommerce and downloaded some themes (without any customizations). Some themes work and some don't. I am testing on a test WooCommerce category (/product-category/test-category/). To demonstrate a simple plugin is attached below: (had to attach as .txt so just need to change to .php) ep_cache_test2.txt For testing purposes the plugin saves to a Wordpress option.
Themes that work
- Twenty Twenty-Two
- Twenty Twenty-Three
- Twenty Twenty-Four
Themes that don't work
- Twenty Nineteen
- Twenty Twenty
- Twenty Twenty-One
- GeneratePress
- Storefront
Using Query Monitor all of the themes that work have the queries below, the themes that don't work, don't have them:
SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND wp_posts.post_name IN ('taxonomy-product_cat-test-category','taxonomy-product_cat','taxonomy','archive-product') AND ( wp_term_relationships.term_taxonomy_id IN (16) ) AND wp_posts.post_type = 'wp_template' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4
and
SELECT wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND wp_posts.post_name IN ('taxonomy-product_cat-test-category','taxonomy-product_cat','taxonomy','archive-product') AND ( wp_term_relationships.term_taxonomy_id IN (16) ) AND ((wp_posts.post_type = 'wp_template' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
It seems that a hook isn't being called in some of the themes?
Thanks for any info.
A corrected version of the plugin is attached, the check in 'ep_wp_query_non_cached_search' should be on a cloned version of $query.
When using the cached version any filters (e.g meta) aren't displayed, so I probably won't use the cached versions.
Thanks
Yeah, after digging a bit, we rely on the ES response to get aggregations instead of getting it from the WP_Query object itself. I'll close the ticket, but will keep it in mind when we implement #3352.