From 709d314ca738977ba7e289ea768002b908c148ea Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 8 Dec 2016 17:17:23 +0000 Subject: [PATCH] Add caching to product factory --- includes/class-wc-product-factory.php | 10 +++++++++- .../data-stores/class-wc-product-data-store-cpt.php | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-product-factory.php b/includes/class-wc-product-factory.php index 8b642dec093..2dd03964527 100644 --- a/includes/class-wc-product-factory.php +++ b/includes/class-wc-product-factory.php @@ -45,7 +45,15 @@ class WC_Product_Factory { } try { - return new $classname( $product_id ); + // Try to get from cache, otherwise create a new object, + $product = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'product-' . $product_id ), 'products' ); + + if ( ! is_a( $product, 'WC_Product' ) ) { + $product = new $classname( $product_id ); + wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'product-' . $product_id ), $product, 'products' ); + } + + return $product; } catch ( Exception $e ) { return false; } diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 75eae17717c..e7c69fef4f6 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -615,6 +615,7 @@ 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() ); + WC_Cache_Helper::incr_cache_prefix( 'product-' . $product->get_id() ); } /*