Merge pull request #15357 from woocommerce/fix/15274-meta
Add ability to invalidate cache by object ID
This commit is contained in:
commit
d9e946491f
|
@ -435,7 +435,8 @@ abstract class WC_Data {
|
|||
}
|
||||
|
||||
if ( ! empty( $this->cache_group ) ) {
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . 'object_meta_' . $this->get_id();
|
||||
// Prefix by group allows invalidation by group until https://core.trac.wordpress.org/ticket/4476 is implemented.
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
|
||||
}
|
||||
|
||||
if ( ! $force_read ) {
|
||||
|
@ -483,9 +484,9 @@ abstract class WC_Data {
|
|||
$this->data_store->update_meta( $this, $meta );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $this->cache_group ) ) {
|
||||
WC_Cache_Helper::incr_cache_prefix( $this->cache_group );
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( $this->cache_group ) . WC_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id();
|
||||
wp_cache_delete( $cache_key, $this->cache_group );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,10 @@ class WC_Post_Data {
|
|||
|
||||
// Download permissions
|
||||
add_action( 'woocommerce_process_product_file_download_paths', array( __CLASS__, 'process_product_file_download_paths' ), 10, 3 );
|
||||
|
||||
// Meta cache flushing.
|
||||
add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
|
||||
add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -449,6 +453,17 @@ class WC_Post_Data {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush meta cache for CRUD objects on direct update.
|
||||
* @param int $meta_id
|
||||
* @param int $object_id
|
||||
* @param string $meta_key
|
||||
* @param string $meta_value
|
||||
*/
|
||||
public static function flush_object_meta_cache( $meta_id, $object_id, $meta_key, $meta_value ) {
|
||||
WC_Cache_Helper::incr_cache_prefix( 'object_' . $object_id );
|
||||
}
|
||||
}
|
||||
|
||||
WC_Post_Data::init();
|
||||
|
|
|
@ -99,8 +99,7 @@ function wc_delete_order_item( $item_id ) {
|
|||
function wc_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
|
||||
$data_store = WC_Data_Store::load( 'order-item' );
|
||||
if ( $data_store->update_metadata( $item_id, $meta_key, $meta_value, $prev_value ) ) {
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( 'order-items' ) . 'object_meta_' . $item_id;
|
||||
wp_cache_delete( $cache_key, 'order-items' );
|
||||
WC_Cache_Helper::incr_cache_prefix( 'object_' . $item_id ); // Invalidate cache.
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -119,8 +118,7 @@ function wc_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_valu
|
|||
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
|
||||
$data_store = WC_Data_Store::load( 'order-item' );
|
||||
if ( $meta_id = $data_store->add_metadata( $item_id, $meta_key, $meta_value, $unique ) ) {
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( 'order-items' ) . 'object_meta_' . $item_id;
|
||||
wp_cache_delete( $cache_key, 'order-items' );
|
||||
WC_Cache_Helper::incr_cache_prefix( 'object_' . $item_id ); // Invalidate cache.
|
||||
return $meta_id;
|
||||
}
|
||||
return 0;
|
||||
|
@ -139,8 +137,7 @@ function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = fal
|
|||
function wc_delete_order_item_meta( $item_id, $meta_key, $meta_value = '', $delete_all = false ) {
|
||||
$data_store = WC_Data_Store::load( 'order-item' );
|
||||
if ( $data_store->delete_metadata( $item_id, $meta_key, $meta_value, $delete_all ) ) {
|
||||
$cache_key = WC_Cache_Helper::get_cache_prefix( 'order-items' ) . 'object_meta_' . $item_id;
|
||||
wp_cache_delete( $cache_key, 'order-items' );
|
||||
WC_Cache_Helper::incr_cache_prefix( 'object_' . $item_id ); // Invalidate cache.
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
@ -112,4 +112,24 @@ class WC_Tests_CRUD_Meta_Data extends WC_Unit_Test_Case {
|
|||
$this->assertTrue( in_array( 'random_other', wp_list_pluck( $new_order->get_meta_data(), 'key' ) ) );
|
||||
$this->assertTrue( in_array( 'random_other_pre_crud', wp_list_pluck( $new_order->get_meta_data(), 'key' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the meta data cache gets flushed when update_post_meta updates the object's meta.
|
||||
* @see https://github.com/woocommerce/woocommerce/issues/15274
|
||||
*/
|
||||
function test_get_meta_data_after_update_post_meta() {
|
||||
// Create an object.
|
||||
$object = new WC_Product;
|
||||
$object->save();
|
||||
|
||||
// Update a meta value.
|
||||
update_post_meta( $object->get_id(), '_some_meta_key', 'val1' );
|
||||
$product = wc_get_product( $object->get_id() );
|
||||
$this->assertEquals( 'val1', $product->get_meta( '_some_meta_key', true ) );
|
||||
|
||||
// Update meta to diff value.
|
||||
update_post_meta( $object->get_id(), '_some_meta_key', 'val2' );
|
||||
$product = wc_get_product( $object->get_id() );
|
||||
$this->assertEquals( 'val2', $product->get_meta( '_some_meta_key', true ) );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue