Skip to content

Commit 64b8115

Browse files
committed
Prime caches
1 parent fae55d1 commit 64b8115

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

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

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,100 @@ public function custom_price_filter_post_clauses( $args, $wp_query ) {
226226
* @return array
227227
*/
228228
public function prime_product_table_caches( $posts ) {
229-
$prime_ids = array();
229+
$prime_ids = array();
230+
$variation_ids = array();
230231

231232
foreach ( $posts as $post ) {
232233
if ( ! in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
233234
continue;
234235
}
235236
$prime_ids[] = (int) $post->ID;
237+
238+
if ( 'product_variation' === $post->post_type ) {
239+
$variation_ids[] = (int) $post->ID;
240+
}
236241
}
237242

238243
if ( ! empty( $prime_ids ) ) {
239244
global $wpdb;
240245

241-
$products = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'wc_products WHERE product_id IN (' . implode( ',', $prime_ids ) . ');' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
246+
$prime_id_sql = implode( ',', $prime_ids );
247+
$prime_non_variations_sql = implode( ',', array_diff( $prime_ids, $variation_ids ) );
248+
$prime_variations_sql = implode( ',', $variation_ids );
249+
250+
// Prime product rows.
251+
$rows = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'wc_products WHERE product_id IN (' . $prime_id_sql . ');' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
252+
253+
foreach ( $rows as $row ) {
254+
wp_cache_set( 'woocommerce_product_' . $row->product_id, $row, 'product' );
255+
}
256+
257+
// Prime attribute rows.
258+
if ( $prime_non_variations_sql ) {
259+
$rows = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'wc_product_attributes WHERE product_id IN (' . $prime_non_variations_sql . ');' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
260+
$cache = array_fill_keys( $prime_ids, array() );
261+
262+
foreach ( $rows as $row ) {
263+
$cache[ $row->product_id ][] = $row;
264+
}
265+
266+
foreach ( $prime_ids as $id ) {
267+
wp_cache_set( 'woocommerce_product_attributes_' . $id, $cache[ $id ], 'product' );
268+
}
269+
}
270+
271+
// Prime attribute values.
272+
$rows = $wpdb->get_results( '
273+
SELECT `product_attribute_id`, `value`, `is_default`
274+
FROM ' . $wpdb->prefix . 'wc_product_attribute_values
275+
WHERE product_id IN (' . $prime_non_variations_sql . ');
276+
' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
277+
$cache = array_fill_keys( $prime_ids, array() );
278+
279+
foreach ( $rows as $row ) {
280+
$cache[ $row->product_attribute_id ][] = $row;
281+
}
282+
283+
foreach ( $prime_ids as $id ) {
284+
wp_cache_set( 'woocommerce_product_attribute_values_' . $id, $cache[ $id ], 'product' );
285+
}
286+
287+
// Prime variation attribute values.
288+
if ( $prime_variations_sql ) {
289+
$rows = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'wc_product_variation_attribute_values WHERE product_id IN (' . $prime_variations_sql . ');' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
290+
$cache = array_fill_keys( $prime_ids, array() );
291+
292+
foreach ( $rows as $row ) {
293+
$cache[ $row->product_id ][] = $row;
294+
}
295+
296+
foreach ( $prime_ids as $id ) {
297+
wp_cache_set( 'woocommerce_product_variation_attribute_values_' . $id, $cache[ $id ], 'product' );
298+
}
299+
}
300+
301+
// Prime relationships.
302+
$rows = $wpdb->get_results( 'SELECT `product_id`, `relationship_id`, `object_id`, `type` FROM ' . $wpdb->prefix . 'wc_product_relationships WHERE product_id IN (' . $prime_id_sql . ') ORDER BY `priority` ASC;' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
303+
$cache = array_fill_keys( $prime_ids, array() );
304+
305+
foreach ( $rows as $row ) {
306+
$cache[ $row->product_id ][] = $row;
307+
}
308+
309+
foreach ( $prime_ids as $id ) {
310+
wp_cache_set( 'woocommerce_product_relationships_' . $id, $cache[ $id ], 'product' );
311+
}
312+
313+
// Prime downloads.
314+
$rows = $wpdb->get_results( 'SELECT `product_id`, `download_id`, `name`, `file`, `priority` FROM ' . $wpdb->prefix . 'wc_product_downloads WHERE product_id IN (' . $prime_id_sql . ') ORDER BY `priority` ASC;' ); // WPCS: db call ok, cache ok, unprepared SQL OK.
315+
$cache = array_fill_keys( $prime_ids, array() );
316+
317+
foreach ( $rows as $row ) {
318+
$cache[ $row->product_id ][] = $row;
319+
}
242320

243-
foreach ( $products as $product ) {
244-
wp_cache_set( 'woocommerce_product_' . $product->product_id, $product, 'product' );
321+
foreach ( $prime_ids as $id ) {
322+
wp_cache_set( 'woocommerce_product_downloads_' . $id, $cache[ $id ], 'product' );
245323
}
246324
}
247325

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function update( &$product ) {
331331
public function read_attributes( &$product ) {
332332
global $wpdb;
333333

334-
$product_attributes = wp_cache_get( 'woocommerce_product_attributes_' . $product->get_id(), 'product' );
334+
$product_attributes = wp_cache_get( 'woocommerce_product_variation_attribute_values_' . $product->get_id(), 'product' );
335335

336336
if ( false === $product_attributes ) {
337337
$product_attributes = $wpdb->get_results(
@@ -341,7 +341,7 @@ public function read_attributes( &$product ) {
341341
)
342342
); // WPCS: db call ok, cache ok.
343343

344-
wp_cache_set( 'woocommerce_product_attributes_' . $product->get_id(), $product_attributes, 'product' );
344+
wp_cache_set( 'woocommerce_product_variation_attribute_values_' . $product->get_id(), $product_attributes, 'product' );
345345
}
346346

347347
if ( ! empty( $product_attributes ) ) {
@@ -669,6 +669,7 @@ protected function update_attributes( &$product, $force = false ) {
669669
*/
670670
protected function clear_caches( &$product ) {
671671
wp_cache_delete( 'woocommerce_product_children_stock_status_' . $product->get_parent_id(), 'product' );
672+
wp_cache_delete( 'woocommerce_product_variation_attribute_values_' . $product->get_id(), 'product' );
672673
parent::clear_caches( $product );
673674
}
674675
}

0 commit comments

Comments
 (0)