Skip to content

Commit 7f2abe2

Browse files
committed
improve cache
1 parent c727661 commit 7f2abe2

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

includes/class-wc-product-tables-query.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,17 @@ public function prime_product_table_caches( $posts ) {
270270

271271
// Prime attribute values.
272272
$rows = $wpdb->get_results( '
273-
SELECT `product_attribute_id`, `value`, `is_default`
273+
SELECT `product_id`, `product_attribute_id`, `value`, `is_default`
274274
FROM ' . $wpdb->prefix . 'wc_product_attribute_values
275275
WHERE product_id IN (' . $prime_non_variations_sql . ');
276276
' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
277277
$cache = array_fill_keys( $prime_ids, array() );
278278

279279
foreach ( $rows as $row ) {
280-
$cache[ $row->product_attribute_id ][] = $row;
280+
if ( ! isset( $cache[ $row->product_id ] ) ) {
281+
$cache[ $row->product_id ] = array();
282+
}
283+
$cache[ $row->product_id ][ $row->product_attribute_id ][] = $row;
281284
}
282285

283286
foreach ( $prime_ids as $id ) {

includes/data-stores/class-wc-product-data-store-custom-table.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,7 @@ public function delete( &$product, $args = array() ) {
595595
* @param WC_Product $product The product object.
596596
*/
597597
protected function clear_caches( &$product ) {
598-
foreach ( $product->get_attributes( 'edit' ) as $attribute ) {
599-
wp_cache_delete( 'woocommerce_product_attribute_values_' . $attribute->get_product_attribute_id(), 'product' );
600-
}
598+
wp_cache_delete( 'woocommerce_product_attribute_values_' . $product->get_id(), 'product' );
601599
wp_cache_delete( 'woocommerce_product_' . $product->get_id(), 'product' );
602600
wp_cache_delete( 'woocommerce_product_relationships_' . $product->get_id(), 'product' );
603601
wp_cache_delete( 'woocommerce_product_downloads_' . $product->get_id(), 'product' );
@@ -1348,8 +1346,10 @@ public function read_attributes( &$product ) {
13481346
}
13491347

13501348
if ( ! empty( $product_attributes ) ) {
1351-
$attributes = array();
1352-
$default_attributes = array();
1349+
$attributes = array();
1350+
$default_attributes = array();
1351+
$cached_attr_value_data = wp_cache_get( 'woocommerce_product_attribute_values_' . $product->get_id(), 'product' );
1352+
13531353
foreach ( $product_attributes as $attr ) {
13541354
$attribute = new WC_Product_Attribute();
13551355
$attribute->set_attribute_id( $attr->attribute_id ); // This is the attribute taxonomy ID, or 0 for local attributes.
@@ -1359,20 +1359,17 @@ public function read_attributes( &$product ) {
13591359
$attribute->set_visible( $attr->is_visible );
13601360
$attribute->set_variation( $attr->is_variation );
13611361

1362-
$attr_value_data = wp_cache_get( 'woocommerce_product_attribute_values_' . $attr->product_attribute_id, 'product' );
1363-
1364-
if ( false === $attr_value_data ) {
1365-
$attr_value_data = $wpdb->get_results(
1362+
if ( ! isset( $cached_attr_value_data[ $attr->product_attribute_id ] ) ) {
1363+
$cached_attr_value_data[ $attr->product_attribute_id ] = $wpdb->get_results(
13661364
$wpdb->prepare(
13671365
"SELECT value, is_default FROM {$wpdb->prefix}wc_product_attribute_values WHERE product_attribute_id = %d",
13681366
$attr->product_attribute_id
13691367
)
13701368
);
1371-
1372-
wp_cache_set( 'woocommerce_product_attribute_values_' . $attr->product_attribute_id, $attr_value_data, 'product' );
13731369
}
13741370

1375-
$attr_values = wp_list_pluck( $attr_value_data, 'value' );
1371+
$attr_value_data = $cached_attr_value_data[ $attr->product_attribute_id ];
1372+
$attr_values = wp_list_pluck( $attr_value_data, 'value' );
13761373

13771374
// If this is a taxonomy, we're reading numeric term IDs.
13781375
if ( $attr->attribute_id ) {
@@ -1389,6 +1386,9 @@ public function read_attributes( &$product ) {
13891386
}
13901387
}
13911388
}
1389+
1390+
wp_cache_set( 'woocommerce_product_attribute_values_' . $product->get_id(), $cached_attr_value_data, 'product' );
1391+
13921392
$product->set_attributes( $attributes );
13931393
$product->set_default_attributes( $default_attributes );
13941394
}

0 commit comments

Comments
 (0)