Merge pull request #13698 from woocommerce/remove-factory-cache

Remove factory cache
This commit is contained in:
Mike Jolley 2017-03-22 19:02:54 +00:00 committed by GitHub
commit 56d55517cc
6 changed files with 26 additions and 35 deletions

View File

@ -469,7 +469,6 @@ abstract class WC_Data {
if ( ! empty( $this->cache_group ) ) {
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
wp_cache_delete( 'object-' . $this->get_id(), $this->cache_group );
}
}

View File

@ -44,15 +44,7 @@ class WC_Order_Factory {
}
try {
// Try to get from cache, otherwise create a new object,
$order = wp_cache_get( 'object-' . $order_id, 'orders' );
if ( ! is_a( $order, 'WC_Order' ) ) {
$order = new $classname( $order_id );
wp_cache_set( 'object-' . $order_id, $order, 'orders' );
}
return $order;
return new $classname( $order_id );
} catch ( Exception $e ) {
return false;
}
@ -107,15 +99,7 @@ class WC_Order_Factory {
if ( $classname && class_exists( $classname ) ) {
try {
// Try to get from cache, otherwise create a new object,
$item = wp_cache_get( 'object-' . $id, 'order-items' );
if ( ! is_a( $item, 'WC_Order_Item' ) ) {
$item = new $classname( $id );
wp_cache_set( 'object-' . $id, $item, 'order-items' );
}
return $item;
return new $classname( $id );
} catch ( Exception $e ) {
return false;
}

View File

@ -43,15 +43,7 @@ class WC_Product_Factory {
$classname = $this->get_product_classname( $product_id, $product_type );
try {
// Try to get from cache, otherwise create a new object,
$product = wp_cache_get( 'product-' . $product_id, 'products' );
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = new $classname( $product_id, $deprecated );
wp_cache_set( 'product-' . $product_id, $product, 'products' );
}
return $product;
return new $classname( $product_id, $deprecated );
} catch ( Exception $e ) {
return false;
}

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( 'object-' . $order->get_id(), 'orders' );
wp_cache_delete( 'order-items-' . $order->get_id(), 'orders' );
}
/**
@ -276,8 +276,19 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
public function read_items( $order, $type ) {
global $wpdb;
$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 AND order_item_type = %s ORDER BY order_item_id;", $order->get_id(), $type );
$items = $wpdb->get_results( $get_items_sql );
// Get from cache if available.
$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 );
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 ) );
if ( ! empty( $items ) ) {
$items = array_map( array( 'WC_Order_Factory', 'get_order_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $items ) );
@ -303,6 +314,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
}
$this->clear_caches( $order );
}
/**

View File

@ -101,7 +101,13 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$item->set_defaults();
$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() ) );
// Get from cache if available.
$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 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 ) {
throw new Exception( __( 'Invalid order item.', 'woocommerce' ) );
@ -110,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();
}
@ -128,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( 'object-' . $item->get_id(), 'order-items' );
wp_cache_delete( 'item-' . $item->get_id(), 'order-items' );
}
}

View File

@ -695,7 +695,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
*/
protected function clear_caches( &$product ) {
wc_delete_product_transients( $product->get_id() );
wp_cache_delete( 'object-' . $product->get_id(), 'products' );
}
/*