Cache items

This commit is contained in:
Mike Jolley 2017-02-01 14:54:18 +01:00
parent 31308403d4
commit 3529d7cafd
10 changed files with 43 additions and 27 deletions

View File

@ -51,13 +51,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @since 2.7.0
* @var array
*/
protected $items = array(
'line_items' => null,
'coupon_lines' => null,
'shipping_lines' => null,
'fee_lines' => null,
'tax_lines' => null,
);
protected $items = array();
/**
* Order items that need deleting are stored here.
@ -642,6 +636,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Remove all line items (products, coupons, shipping, taxes) from the order.
*
* @param string $type Order item type. Default null.
*/
public function remove_order_items( $type = null ) {
@ -649,22 +644,17 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
$this->data_store->delete_items( $this, $type );
if ( $group = $this->type_to_group( $type ) ) {
$this->items[ $group ] = null;
unset( $this->items[ $group ] );
}
} else {
$this->data_store->delete_items( $this );
$this->items = array(
'line_items' => null,
'coupon_lines' => null,
'shipping_lines' => null,
'fee_lines' => null,
'tax_lines' => null,
);
$this->items = array();
}
}
/**
* Convert a type to a types group.
*
* @param string $type
* @return string group
*/
@ -681,6 +671,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Return an array of items/products within this order.
*
* @param string|array $types Types of line items to get (array or string).
* @return Array of WC_Order_item
*/
@ -690,7 +681,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
foreach ( $types as $type ) {
if ( $group = $this->type_to_group( $type ) ) {
if ( is_null( $this->items[ $group ] ) ) {
if ( ! isset( $this->items[ $group ] ) ) {
$this->items[ $group ] = $this->data_store->read_items( $this, $type );
}
// Don't use array_merge here because keys are numeric
@ -703,6 +694,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Return an array of fees within this order.
*
* @return array
*/
public function get_fees() {
@ -711,6 +703,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Return an array of taxes within this order.
*
* @return array
*/
public function get_taxes() {
@ -719,6 +712,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Return an array of shipping costs within this order.
*
* @return array
*/
public function get_shipping_methods() {
@ -727,6 +721,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Gets formatted shipping method title.
*
* @return string
*/
public function get_shipping_method() {
@ -739,6 +734,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Get coupon codes only.
*
* @return array
*/
public function get_used_coupons() {
@ -770,6 +766,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Get an order item object, based on it's type.
*
* @since 2.7.0
* @param int $item_id
* @return WC_Order_Item
@ -780,6 +777,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Get key for where a certain item type is stored in _items.
*
* @since 2.7.0
* @param $item object Order item (product, shipping, fee, coupon, tax)
* @return string
@ -802,6 +800,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Remove item from the order.
*
* @param int $item_id
*/
public function remove_item( $item_id ) {
@ -818,6 +817,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Adds an order item to this order. The order item will not persist until save.
*
* @since 2.7.0
* @param WC_Order_Item Order item object (product, shipping, fee, coupon, tax)
*/
@ -827,7 +827,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
}
// Make sure existing items are loaded so we can append this new one.
if ( is_null( $this->items[ $items_key ] ) ) {
if ( ! isset( $this->items[ $items_key ] ) ) {
$this->items[ $items_key ] = $this->get_items( $item->get_type() );
}
@ -845,6 +845,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Add a product line item to the order. This is the only line item type with
* it's own method because it saves looking up order amounts (costs are added up for you).
*
* @param \WC_Product $product
* @param int $qty
* @param array $args

View File

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

View File

@ -196,8 +196,9 @@ class WC_Order_Item_Product extends WC_Order_Item {
* Set meta data for backordered products.
*/
public function set_backorder_meta() {
if ( $this->get_product()->backorders_require_notification() && $this->get_product()->is_on_backorder( $this->get_quantity() ) ) {
$this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ) ), $this->get_quantity() - max( 0, $this->get_product()->get_stock_quantity() ), true );
$product = $this->get_product();
if ( $product && $product->backorders_require_notification() && $product->is_on_backorder( $this->get_quantity() ) ) {
$this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ) ), $this->get_quantity() - max( 0, $product->get_stock_quantity() ), true );
}
}

View File

@ -242,6 +242,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( 'order-' . $order->get_id(), 'orders' );
}
/**

View File

@ -45,6 +45,7 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$this->save_item_data( $item );
$item->save_meta_data();
$item->apply_changes();
$this->clear_cache( $item );
do_action( 'woocommerce_new_order_item', $item->get_id(), $item, $item->get_order_id() );
}
@ -67,6 +68,7 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
$this->save_item_data( $item );
$item->save_meta_data();
$item->apply_changes();
$this->clear_cache( $item );
do_action( 'woocommerce_update_order_item', $item->get_id(), $item, $item->get_order_id() );
}
@ -126,6 +128,6 @@ abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP i
* Clear meta cachce.
*/
public function clear_cache( &$item ) {
WC_Cache_Helper::incr_cache_prefix( 'orders' );
wp_cache_delete( 'order-item-' . $item->get_id(), 'order-items' );
}
}

View File

@ -51,6 +51,5 @@ class WC_Order_Item_Coupon_Data_Store extends Abstract_WC_Order_Item_Type_Data_S
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
$this->clear_cache( $item );
}
}

View File

@ -55,6 +55,5 @@ class WC_Order_Item_Fee_Data_Store extends Abstract_WC_Order_Item_Type_Data_Stor
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
$this->clear_cache( $item );
}
}

View File

@ -62,7 +62,6 @@ class WC_Order_Item_Product_Data_Store extends Abstract_WC_Order_Item_Type_Data_
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
$this->clear_cache( $item );
}
/**

View File

@ -53,6 +53,5 @@ class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
$this->clear_cache( $item );
}
}

View File

@ -56,6 +56,5 @@ class WC_Order_Item_Tax_Data_Store extends Abstract_WC_Order_Item_Type_Data_Stor
foreach ( $save_values as $key => $value ) {
update_metadata( 'order_item', $id, $key, $value );
}
$this->clear_cache( $item );
}
}