Shared cache for order items

This commit is contained in:
Mike Jolley 2017-03-21 23:50:05 +00:00
parent 91f109c33d
commit 414e2b9501
2 changed files with 10 additions and 8 deletions

View File

@ -263,7 +263,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
protected function clear_caches( &$order ) {
clean_post_cache( $order->get_id() );
wc_delete_shop_order_transients( $order );
wp_cache_delete( 'items-' . $order->get_id(), 'orders' );
wp_cache_delete( 'order-items-' . $order->get_id(), 'orders' );
}
/**
@ -277,12 +277,15 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
global $wpdb;
// Get from cache if available.
$items = wp_cache_get( 'items-' . $order->get_id(), 'orders' );
$items = wp_cache_get( 'order-items-' . $order->get_id(), 'orders' );
if ( false === $items ) {
$get_items_sql = $wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ORDER BY order_item_id;", $order->get_id() );
$items = $wpdb->get_results( $get_items_sql );
wp_cache_set( 'items-' . $order->get_id(), $items, 'order-items' );
foreach ( $items as $item ) {
wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' );
}
wp_cache_set( 'order-items-' . $order->get_id(), $items, 'orders' );
}
$items = wp_list_filter( $items, array( 'order_item_type' => $type ) );

View File

@ -102,11 +102,11 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$item->set_defaults();
// Get from cache if available.
$data = wp_cache_get( 'data-' . $item->get_id(), 'order-items' );
$data = wp_cache_get( 'item-' . $item->get_id(), 'order-items' );
if ( false === $data ) {
$data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name, order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
wp_cache_set( 'data-' . $item->get_id(), $data, 'order-items' );
$data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
wp_cache_set( 'item-' . $item->get_id(), $data, 'order-items' );
}
if ( ! $data ) {
@ -116,7 +116,6 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$item->set_props( array(
'order_id' => $data->order_id,
'name' => $data->order_item_name,
'type' => $data->order_item_type,
) );
$item->read_meta_data();
}
@ -134,6 +133,6 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
* Clear meta cachce.
*/
public function clear_cache( &$item ) {
wp_cache_delete( 'data-' . $item->get_id(), 'order-items' );
wp_cache_delete( 'item-' . $item->get_id(), 'order-items' );
}
}