From 414e2b9501cf5467d2043618275788a35b4d1696 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 21 Mar 2017 23:50:05 +0000 Subject: [PATCH] Shared cache for order items --- .../data-stores/abstract-wc-order-data-store-cpt.php | 9 ++++++--- .../abstract-wc-order-item-type-data-store.php | 9 ++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/data-stores/abstract-wc-order-data-store-cpt.php b/includes/data-stores/abstract-wc-order-data-store-cpt.php index a5c4919a839..560be2b1247 100644 --- a/includes/data-stores/abstract-wc-order-data-store-cpt.php +++ b/includes/data-stores/abstract-wc-order-data-store-cpt.php @@ -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 ) ); diff --git a/includes/data-stores/abstract-wc-order-item-type-data-store.php b/includes/data-stores/abstract-wc-order-item-type-data-store.php index b9ea0c7594e..747403345e4 100644 --- a/includes/data-stores/abstract-wc-order-item-type-data-store.php +++ b/includes/data-stores/abstract-wc-order-item-type-data-store.php @@ -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' ); } }