Skip to content
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

Feature/cache last changed time #2530

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
18 changes: 18 additions & 0 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 );
}

Expand Down
19 changes: 19 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}