Add caching to product factory

This commit is contained in:
Mike Jolley 2016-12-08 17:17:23 +00:00
parent 767a1e500e
commit 709d314ca7
2 changed files with 10 additions and 1 deletions

View File

@ -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;
}

View File

@ -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() );
}
/*