How the listing search cache works (3.6)
Description #
From Easy Property Listings 3.6, the options in the listing search (suburbs, cities, categories, features and so on) are worked out once and saved, instead of being worked out again from every listing on every page load. On sites with hundreds of listings this makes pages with a search widget much faster. The saved results refresh by themselves when a listing changes, so visitors see the current options.
What's cached #
- Search dropdown values read from listing details: city, state, postcode and country (
epl_get_unique_post_meta_values()), and the house, rural, land and commercial categories (epl_get_meta_values()). - The location (suburb) list, and the feature and business category lists, which only show terms that have published listings (
epl_get_available_locations()andepl_get_available_terms()).
These are used by the Listing Search Widget and the [listing_search] shortcode. Each query is saved on its own, so a rentals search and a sales search each have their own saved results. Add-ons and custom code can cache their own listing queries the same way with epl_listing_query_cache().
How long it's kept #
Each result is saved as a WordPress transient for 12 hours (filter epl_listing_query_cache_expiration), then the next visitor who needs it runs the query again. Within one page load, a query asked for twice runs once. With a persistent object cache (Redis, Memcached) the transients are kept there instead of in the database.
When it refreshes #
The cache refreshes by itself when a listing (any EPL listing type, including add-on types) is:
- saved, published, trashed or changes status;
- permanently deleted;
- given a new value for one of its fields (post meta added, changed or removed);
- given different terms (locations, features, business categories).
Changes during one request are collected and the refresh starts once, at the end of the request, so an import that changes hundreds of fields on a listing only starts one refresh. Changes to pages, posts and other non-listing content are ignored.
Hidden fields, whose meta key starts with an underscore, are ignored too. WordPress saves _edit_lock every minute while a listing is open in the editor, and that would otherwise refresh the cache every minute. Filter: epl_listing_query_cache_ignores_meta_key.
The background rebuild #
When a refresh starts, visitors keep getting the saved results while WP-Cron builds new ones in the background, so no visitor waits:
- EPL marks the cache as out of date, schedules the one-off WP-Cron event
epl_listing_query_cache_rebuildand starts WP-Cron straight away (a background request). - The rebuild runs every query visitors have used in the last 14 days (up to 300 queries) and saves the results as a new copy.
- The site switches to the new copy and the old copy is deleted.
- If listings changed again while it ran, it runs again.
Queries nobody has used for 14 days aren't rebuilt; they run again the next time a visitor needs them.
The 10-minute stale limit #
If the rebuild hasn't finished 10 minutes after the first change (for example WP-Cron is switched off with DISABLE_WP_CRON and no server cron job runs it, or the host blocks the background request), the next page load stops using the old results. Each query then runs again the first time a visitor needs it and is saved again. Filter: epl_listing_query_cache_stale_limit. If your site runs WP-Cron from a server cron job, see Hosting Cron Commands.
Clearing it after direct database changes #
The refresh only sees changes made with WordPress functions such as wp_insert_post(), wp_update_post(), update_post_meta(), wp_set_object_terms() and wp_delete_post(), which is what the dashboard and the REST API use. If you change listings with SQL ($wpdb->query(), phpMyAdmin, a database import), call epl_flush_listing_query_cache() afterwards:
global $wpdb;
// Mark three listings as sold, straight in the database.
$wpdb->query( "UPDATE {$wpdb->postmeta} SET meta_value = 'sold' WHERE meta_key = 'property_status' AND post_id IN ( 101, 102, 103 )" );
// Refresh the listing search cache in the background.
epl_flush_listing_query_cache();
To clear it straight away, from the command line with WP-CLI:
wp eval 'epl_flush_listing_query_cache( true );'
Saving any listing in the dashboard also starts a refresh.
Turning it off #
add_filter( 'epl_listing_query_cache_enabled', '__return_false' );
The queries then run on every page load, as before 3.6. See epl_listing_query_cache_enabled.
What it stores #
Useful when checking a site: the options epl_listing_query_cache_version (the current copy), epl_listing_query_cache_queries (the queries visitors have used, for the rebuild) and epl_listing_query_cache_stale (only set while a refresh is waiting), and transients named epl_lqc_ followed by a hash.
Filters and functions #
- epl_listing_query_cache_enabled: turn the cache on or off.
- epl_listing_query_cache_expiration: how long results are kept (12 hours).
- epl_listing_query_cache_stale_limit: how long old results show while the rebuild runs (10 minutes).
- epl_listing_query_cache_ignores_meta_key: which listing fields don't refresh the cache.
- epl_flush_listing_query_cache(): refresh the cache after changing listings in the database.
- epl_listing_query_cache(): cache your own listing query.
Source #
lib/includes/functions.php, in epl_listing_query_cache() and the functions after it. Used in lib/includes/functions.php and lib/widgets/widget-functions.php. Since 3.6.
Inside the cache (for developers) #
These run on their own; you don't call them. epl_listing_query_run() runs a query without the cache. epl_listing_query_cache_transient() names a cached result. epl_listing_query_cache_remember() keeps a list of queries visitors use (up to 300) for the rebuild. epl_listing_query_cache_changed() marks the cache out of date when a listing changes. epl_rebuild_listing_query_cache() is the WP-Cron rebuild. epl_delete_listing_query_cache_version() deletes the old copies once a rebuild finishes. epl_get_hidden_listing_ids() (withdrawn, off market and deleted listings) is cached here too.