Merge pull request #13698 from woocommerce/remove-factory-cache
Remove factory cache
This commit is contained in:
commit
56d55517cc
|
@ -469,7 +469,6 @@ abstract class WC_Data {
|
||||||
|
|
||||||
if ( ! empty( $this->cache_group ) ) {
|
if ( ! empty( $this->cache_group ) ) {
|
||||||
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
|
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
|
||||||
wp_cache_delete( 'object-' . $this->get_id(), $this->cache_group );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,15 +44,7 @@ class WC_Order_Factory {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to get from cache, otherwise create a new object,
|
return new $classname( $order_id );
|
||||||
$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;
|
|
||||||
} catch ( Exception $e ) {
|
} catch ( Exception $e ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -107,15 +99,7 @@ class WC_Order_Factory {
|
||||||
|
|
||||||
if ( $classname && class_exists( $classname ) ) {
|
if ( $classname && class_exists( $classname ) ) {
|
||||||
try {
|
try {
|
||||||
// Try to get from cache, otherwise create a new object,
|
return new $classname( $id );
|
||||||
$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;
|
|
||||||
} catch ( Exception $e ) {
|
} catch ( Exception $e ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,15 +43,7 @@ class WC_Product_Factory {
|
||||||
$classname = $this->get_product_classname( $product_id, $product_type );
|
$classname = $this->get_product_classname( $product_id, $product_type );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to get from cache, otherwise create a new object,
|
return new $classname( $product_id, $deprecated );
|
||||||
$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;
|
|
||||||
} catch ( Exception $e ) {
|
} catch ( Exception $e ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -263,7 +263,7 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
||||||
protected function clear_caches( &$order ) {
|
protected function clear_caches( &$order ) {
|
||||||
clean_post_cache( $order->get_id() );
|
clean_post_cache( $order->get_id() );
|
||||||
wc_delete_shop_order_transients( $order );
|
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 ) {
|
public function read_items( $order, $type ) {
|
||||||
global $wpdb;
|
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 );
|
// Get from cache if available.
|
||||||
$items = $wpdb->get_results( $get_items_sql );
|
$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 ) ) {
|
if ( ! empty( $items ) ) {
|
||||||
$items = array_map( array( 'WC_Order_Factory', 'get_order_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $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 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() ) );
|
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
|
||||||
}
|
}
|
||||||
|
$this->clear_caches( $order );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -101,7 +101,13 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
|
||||||
|
|
||||||
$item->set_defaults();
|
$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 ) {
|
if ( ! $data ) {
|
||||||
throw new Exception( __( 'Invalid order item.', 'woocommerce' ) );
|
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(
|
$item->set_props( array(
|
||||||
'order_id' => $data->order_id,
|
'order_id' => $data->order_id,
|
||||||
'name' => $data->order_item_name,
|
'name' => $data->order_item_name,
|
||||||
'type' => $data->order_item_type,
|
|
||||||
) );
|
) );
|
||||||
$item->read_meta_data();
|
$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.
|
* Clear meta cachce.
|
||||||
*/
|
*/
|
||||||
public function clear_cache( &$item ) {
|
public function clear_cache( &$item ) {
|
||||||
wp_cache_delete( 'object-' . $item->get_id(), 'order-items' );
|
wp_cache_delete( 'item-' . $item->get_id(), 'order-items' );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -695,7 +695,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
|
||||||
*/
|
*/
|
||||||
protected function clear_caches( &$product ) {
|
protected function clear_caches( &$product ) {
|
||||||
wc_delete_product_transients( $product->get_id() );
|
wc_delete_product_transients( $product->get_id() );
|
||||||
wp_cache_delete( 'object-' . $product->get_id(), 'products' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue