Only set cache when neccessary.

This creates a tradeoff in optimizing repeated queries vs on off queries.

Another tradeoff is making more cache get calls as opposed to more SQL calls.
This commit is contained in:
vedanshujain 2020-09-16 18:37:03 +05:30
parent ead379cf31
commit bc4e9a7ae2
1 changed files with 60 additions and 10 deletions

View File

@ -925,10 +925,25 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
if ( isset( $query_vars['fields'] ) && is_array( $query_vars['fields'] ) && ! in_array( 'refunds', $query_vars['fields'] ) ) { if ( isset( $query_vars['fields'] ) && is_array( $query_vars['fields'] ) && ! in_array( 'refunds', $query_vars['fields'] ) ) {
return; return;
} }
$cache_keys_mapping = array();
foreach ( $order_ids as $order_id ) {
$cache_keys_mapping[ $order_id ] = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refunds' . $order_id;
}
$non_cached_ids = array();
$cache_values = wp_cache_get_multiple( array_keys( $cache_keys_mapping ), 'orders' );
foreach ( $order_ids as $order_id ) {
if ( false === $cache_values[ $cache_keys_mapping[ $order_id ] ] ) {
$non_cached_ids[] = $order_id;
}
}
if ( empty( $non_cached_ids ) ) {
return;
}
$refunds = wc_get_orders( $refunds = wc_get_orders(
array( array(
'type' => 'shop_order_refund', 'type' => 'shop_order_refund',
'post_parent__in' => $order_ids, 'post_parent__in' => $non_cached_ids,
'limit' => - 1, 'limit' => - 1,
) )
); );
@ -943,13 +958,12 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
}, },
array() array()
); );
foreach ( $order_ids as $order_id ) { foreach ( $non_cached_ids as $order_id ) {
$cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refunds' . $order_id;
$refunds = array(); $refunds = array();
if ( isset( $order_refunds[ $order_id ] ) ) { if ( isset( $order_refunds[ $order_id ] ) ) {
$refunds = $order_refunds[ $order_id ]; $refunds = $order_refunds[ $order_id ];
} }
wp_cache_set( $cache_key, $refunds, 'orders' ); wp_cache_set( $cache_keys_mapping[ $order_id ], $refunds, 'orders' );
} }
} }
@ -975,15 +989,34 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
return; return;
} }
} }
$order_ids = esc_sql( $order_ids );
$order_id_string = implode( ',', $order_ids ); $cache_keys = array_map(
function ( $order_id ) {
return 'order-items-' . $order_id;
},
$order_ids
);
$cache_values = wp_cache_get_multiple( $cache_keys, 'orders' );
$non_cached_ids = array();
foreach ( $order_ids as $order_id ) {
if ( false === $cache_values[ 'order-items-' . $order_id ] ) {
$non_cached_ids[] = $order_id;
}
}
if ( empty( $non_cached_ids ) ) {
return;
}
$non_cached_ids = esc_sql( $non_cached_ids );
$non_cached_ids_string = implode( ',', $non_cached_ids );
$order_items = $wpdb->get_results( $order_items = $wpdb->get_results(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id in ( $order_id_string ) ORDER BY order_item_id;" "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id in ( $non_cached_ids_string ) ORDER BY order_item_id;"
); );
foreach ( $order_items as $item ) { if ( empty( $order_items ) ) {
wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' ); return;
} }
$order_items_for_all_orders = array_reduce( $order_items_for_all_orders = array_reduce(
$order_items, $order_items,
function ( $order_items_collection, $order_item ) { function ( $order_items_collection, $order_item ) {
@ -997,6 +1030,9 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
foreach ( $order_items_for_all_orders as $order_id => $items ) { foreach ( $order_items_for_all_orders as $order_id => $items ) {
wp_cache_set( 'order-items-' . $order_id, $items, 'orders' ); wp_cache_set( 'order-items-' . $order_id, $items, 'orders' );
} }
foreach ( $order_items as $item ) {
wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' );
}
$order_item_ids = wp_list_pluck( $order_items, 'order_item_id' ); $order_item_ids = wp_list_pluck( $order_items, 'order_item_id' );
update_meta_cache( 'order_item', $order_item_ids ); update_meta_cache( 'order_item', $order_item_ids );
} }
@ -1009,7 +1045,21 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
*/ */
private function prime_raw_meta_cache_for_orders( $order_ids, $query_vars ) { private function prime_raw_meta_cache_for_orders( $order_ids, $query_vars ) {
global $wpdb; global $wpdb;
$order_ids = esc_sql( $order_ids ); $cache_keys_mapping = array();
foreach ( $order_ids as $order_id ) {
$cache_keys_mapping[ $order_id ] = WC_Order::generate_meta_cache_key( $order_id, 'orders' );
}
$cache_values = wp_cache_get_multiple( $cache_keys_mapping, 'orders' );
$non_cached_ids = array();
foreach ( $order_ids as $order_id ) {
if ( false === $cache_values[ $cache_keys_mapping[ $order_id ] ] ) {
$non_cached_ids[] = $order_id;
}
}
if ( empty( $non_cached_ids ) ) {
return;
}
$order_ids = esc_sql( $non_cached_ids );
$order_ids_in = "'" . implode( $order_ids, "', '" ) . "'"; $order_ids_in = "'" . implode( $order_ids, "', '" ) . "'";
$raw_meta_data_array = $wpdb->get_results( $raw_meta_data_array = $wpdb->get_results(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared