diff --git a/elasticpress.php b/elasticpress.php index b96e94e83c..32509140c0 100644 --- a/elasticpress.php +++ b/elasticpress.php @@ -88,6 +88,11 @@ function( $class ) { define( 'EP_IS_NETWORK', true ); } +/** + * Set global cache groups. + */ +wp_cache_add_global_groups( 'elasticpress-network' ); + /** * Sets up the indexables and features. * diff --git a/includes/classes/Elasticsearch.php b/includes/classes/Elasticsearch.php index 4081c746da..77a72b8d92 100644 --- a/includes/classes/Elasticsearch.php +++ b/includes/classes/Elasticsearch.php @@ -137,6 +137,8 @@ public function index_document( $index, $type, $document, $blocking = true ) { $response_body = wp_remote_retrieve_body( $request ); $return = json_decode( $response_body ); + + Utils\wp_cache_set_ep_last_changed(); } else { $return = false; } @@ -191,6 +193,8 @@ public function refresh_indices() { if ( ! is_wp_error( $request ) ) { if ( isset( $request['response']['code'] ) && 200 === $request['response']['code'] ) { + Utils\wp_cache_set_ep_last_changed(); + return true; } } @@ -560,6 +564,8 @@ public function delete_document( $index, $type, $document_id, $blocking = true ) $response = json_decode( $response_body, true ); if ( ! empty( $response['found'] ) ) { + Utils\wp_cache_set_ep_last_changed(); + return true; } } @@ -660,6 +666,8 @@ public function delete_network_alias( $alias ) { if ( ! is_wp_error( $request ) && ( 200 >= wp_remote_retrieve_response_code( $request ) && 300 > wp_remote_retrieve_response_code( $request ) ) ) { $response_body = wp_remote_retrieve_body( $request ); + Utils\wp_cache_set_ep_last_changed(); + return json_decode( $response_body ); } @@ -767,6 +775,8 @@ public function create_network_alias( $indexes, $network_alias ) { $request = $this->remote_request( $path, $request_args, [], 'create_network_alias' ); if ( ! is_wp_error( $request ) && ( 200 >= wp_remote_retrieve_response_code( $request ) && 300 > wp_remote_retrieve_response_code( $request ) ) ) { + Utils\wp_cache_set_ep_last_changed(); + return true; } @@ -816,6 +826,8 @@ public function put_mapping( $index, $mapping ) { if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) { $response_body = wp_remote_retrieve_body( $request ); + Utils\wp_cache_set_ep_last_changed(); + return true; } @@ -927,6 +939,8 @@ public function update_index_settings( $index, $settings, $close_first = false ) return ( $updated && $opened ); } + Utils\wp_cache_set_ep_last_changed(); + return $updated; } @@ -951,6 +965,8 @@ public function delete_index( $index ) { if ( ! is_wp_error( $request ) && ( 200 === wp_remote_retrieve_response_code( $request ) || 404 === wp_remote_retrieve_response_code( $request ) ) ) { $response_body = wp_remote_retrieve_body( $request ); + Utils\wp_cache_set_ep_last_changed(); + return json_decode( $response_body ); } @@ -1041,6 +1057,8 @@ public function bulk_index( $index, $type, $body ) { return new WP_Error( $response, wp_remote_retrieve_response_message( $request ), $request ); } + Utils\wp_cache_set_ep_last_changed(); + return json_decode( wp_remote_retrieve_body( $request ), true ); } diff --git a/includes/utils.php b/includes/utils.php index 9baab22d43..0a5131a5fa 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -633,3 +633,22 @@ function is_integrated_request( $context, $types = [] ) { */ return apply_filters( 'ep_is_integrated_request', $is_integrated, $context, $types ); } + +/** + * Sets the last changed times for ElasticPress and ElasticPress Network cache groups. + * + * @return void + * + * @since 3.7 + */ +function wp_cache_set_ep_last_changed() { + /** + * Set the `last_changed` for this specific blog / site. + */ + wp_cache_set( 'last_changed', microtime(), 'elasticpress' ); + + /** + * Set the `last_changed` for this specific network (for all blogs / sites). + */ + wp_cache_set( 'last_changed', microtime(), 'elasticpress-network' ); +}