* Refactor report datastore cache key generation.

* Add transient-backed Cache class for Report DataStores.

* Bump reports cache version when data changes.

* Rollover cache version number after a limit and expire report transients every day.

* Use caching strategy like WC core.

Store cache version in value, not in key. Increase expiration to 1 week.
This commit is contained in:
Jeff Stieler 2019-10-01 09:36:05 -07:00 committed by GitHub
parent c5034567fc
commit ac9f3f4680
17 changed files with 323 additions and 161 deletions

View File

@ -0,0 +1,81 @@
<?php
/**
* REST API Reports Cache.
*
* Handles report data object caching.
*
* @package WooCommerce Admin/API
*/
namespace Automattic\WooCommerce\Admin\API\Reports;
defined( 'ABSPATH' ) || exit;
/**
* REST API Reports Cache class.
*
* @package WooCommerce Admin/API
*/
class Cache {
/**
* Cache version. Used to invalidate all cached values.
*/
const VERSION_OPTION = 'woocommerce_reports';
/**
* Invalidate cache.
*/
public static function invalidate() {
\WC_Cache_Helper::get_transient_version( self::VERSION_OPTION, true );
}
/**
* Get cache version number.
*
* @return string
*/
public static function get_version() {
$version = \WC_Cache_Helper::get_transient_version( self::VERSION_OPTION );
return $version;
}
/**
* Get cached value.
*
* @param string $key Cache key.
* @return mixed
*/
public static function get( $key ) {
$transient_version = self::get_version();
$transient_value = get_transient( $key );
if (
isset( $transient_value['value'], $transient_value['version'] ) &&
$transient_value['version'] === $transient_version
) {
return $transient_value['value'];
}
return false;
}
/**
* Update cached value.
*
* @param string $key Cache key.
* @param mixed $value New value.
* @return bool
*/
public static function set( $key, $value ) {
$transient_version = self::get_version();
$transient_value = array(
'version' => $transient_version,
'value' => $value,
);
$result = set_transient( $key, $transient_value, WEEK_IN_SECONDS );
return $result;
}
}

View File

@ -25,6 +25,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'categories';
/**
* Order by setting used for sorting categories data.
*
@ -246,8 +253,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -319,19 +330,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -12,6 +12,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Coupons\DataStore.
@ -25,6 +26,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_coupon_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'coupons';
/**
* Mapping columns to data type to return correct response types.
*
@ -244,8 +252,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -336,22 +348,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
/**
* Create or update an an entry in the wc_order_coupon_lookup table for an order.
*
@ -440,6 +442,8 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_reports_delete_coupon', 0, $order_id );
ReportsCache::invalidate();
}
/**
@ -459,6 +463,8 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$wpdb->prefix . self::TABLE_NAME,
array( 'coupon_id' => $post_id )
);
ReportsCache::invalidate();
}
/**

View File

@ -42,6 +42,13 @@ class DataStore extends CouponsDataStore implements DataStoreInterface {
'orders_count' => 'COUNT(DISTINCT order_id) as orders_count',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'coupons_stats';
/**
* Constructor
*/
@ -114,8 +121,12 @@ class DataStore extends CouponsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -220,19 +231,9 @@ class DataStore extends CouponsDataStore implements DataStoreInterface {
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -12,6 +12,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* Admin\API\Reports\Customers\DataStore.
@ -25,6 +26,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_customer_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'customers';
/**
* Mapping columns to data type to return correct response types.
*
@ -358,8 +366,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -429,7 +441,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
@ -676,6 +688,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param int $customer_id Customer ID.
*/
do_action( 'woocommerce_reports_update_customer', $customer_id );
ReportsCache::invalidate();
return $results;
}
@ -724,14 +739,4 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
do_action( 'woocommerce_reports_delete_customer', $customer_id );
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -40,6 +40,13 @@ class DataStore extends CustomersDataStore implements DataStoreInterface {
'avg_avg_order_value' => 'AVG( avg_order_value ) as avg_avg_order_value',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'customers_stats';
/**
* Constructor.
*/
@ -69,8 +76,12 @@ class DataStore extends CustomersDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -117,19 +128,9 @@ class DataStore extends CustomersDataStore implements DataStoreInterface {
$data = (object) $this->cast_numbers( $report_data[0] );
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -67,6 +67,44 @@ class DataStore {
*/
private $order = '';
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return implode(
'_',
array(
'wc_report',
$this->cache_key,
md5( wp_json_encode( $params ) ),
)
);
}
/**
* Wrapper around Cache::get().
*
* @param string $cache_key Cache key.
* @return mixed
*/
protected function get_cached_data( $cache_key ) {
return Cache::get( $cache_key );
}
/**
* Wrapper around Cache::set().
*
* @param string $cache_key Cache key.
* @param mixed $value New value.
* @return bool
*/
protected function set_cached_data( $cache_key, $value ) {
return Cache::set( $cache_key, $value );
}
/**
* Compares two report data objects by pre-defined object property and ASC/DESC ordering.
*
@ -370,6 +408,7 @@ class DataStore {
$new_start_date->setTimestamp( $new_start_date_timestamp );
}
}
// @todo - Do this without modifying $query_args?
$query_args['adj_after'] = $new_start_date;
$query_args['adj_before'] = $new_end_date;
$adj_after = $new_start_date->format( TimeInterval::$sql_datetime_format );
@ -391,6 +430,7 @@ class DataStore {
$intervals_query['limit'] = 'LIMIT ' . $offset . ',' . $count;
}
// Otherwise no change in limit clause.
// @todo - Do this without modifying $query_args?
$query_args['adj_after'] = $query_args['after'];
$query_args['adj_before'] = $query_args['before'];
}

View File

@ -25,6 +25,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_download_log';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'downloads';
/**
* Mapping columns to data type to return correct response types.
*
@ -325,8 +332,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -391,22 +402,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*

View File

@ -36,6 +36,13 @@ class DataStore extends DownloadsDataStore implements DataStoreInterface {
'download_count' => 'COUNT(DISTINCT download_log_id) as download_count',
);
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'downloads_stats';
/**
* Constructor
*/
@ -69,8 +76,12 @@ class DataStore extends DownloadsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$selections = $this->selected_columns( $query_args );
@ -173,22 +184,12 @@ class DataStore extends DownloadsDataStore implements DataStoreInterface {
}
$this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
}
/**
* Normalizes order_by clause to match to SQL query.
*

View File

@ -24,6 +24,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_stats';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'orders';
/**
* Mapping columns to data type to return correct response types.
*
@ -183,8 +190,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -258,7 +269,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
@ -422,14 +433,4 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
return $coupons;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -12,6 +12,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Orders\Stats\DataStore.
@ -30,6 +31,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const CRON_EVENT = 'wc_order_stats_update';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'orders_stats';
/**
* Type for each column to cast values correctly later.
*
@ -227,8 +235,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -276,7 +288,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$unique_products = $this->get_unique_product_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['products'] = $unique_products;
$segmenter = new Segmenter( $query_args, $this->report_columns );
$segmenter = new Segmenter( $query_args, $this->report_columns );
$unique_coupons = $this->get_unique_coupon_count( $totals_query['from_clause'], $totals_query['where_time_clause'], $totals_query['where_clause'] );
$totals[0]['coupons_count'] = $unique_coupons;
$totals[0]['segments'] = $segmenter->get_totals_segments( $totals_query, $table_name );
@ -359,7 +371,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
@ -529,6 +541,8 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_reports_delete_order_stats', $order_id );
ReportsCache::invalidate();
}
@ -630,14 +644,4 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
)
);
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -12,6 +12,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Products\DataStore.
@ -25,6 +26,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'products';
/**
* Mapping columns to data type to return correct response types.
*
@ -271,8 +279,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -367,22 +379,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
/**
* Create or update an entry in the wc_admin_order_product_lookup table for an order.
*
@ -499,5 +501,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_reports_delete_product', 0, $order_id );
ReportsCache::invalidate();
}
}

View File

@ -125,10 +125,14 @@ class DataStore extends ProductsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
$cache_key = $this->get_cache_key( $query_args );
$product_data = wp_cache_get( $cache_key, $this->cache_group );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = $this->get_cached_data( $cache_key );
if ( false === $product_data ) {
if ( false === $data ) {
$selections = $this->selected_columns( $query_args );
$totals_query = array();
$intervals_query = array();

View File

@ -12,6 +12,7 @@ defined( 'ABSPATH' ) || exit;
use \Automattic\WooCommerce\Admin\API\Reports\DataStore as ReportsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\DataStoreInterface;
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* API\Reports\Taxes\DataStore.
@ -25,6 +26,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_tax_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'taxes';
/**
* Mapping columns to data type to return correct response types.
*
@ -181,8 +189,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -275,22 +287,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
/**
* Maps ordering specified by the user to columns in the database/fields in the data.
*
@ -386,5 +388,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @param int $order_id Order ID.
*/
do_action( 'woocommerce_reports_delete_tax', 0, $order_id );
ReportsCache::invalidate();
}
}

View File

@ -25,6 +25,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_tax_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'taxes_stats';
/**
* Mapping columns to data type to return correct response types.
*
@ -165,8 +172,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -279,19 +290,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_stats_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -25,6 +25,13 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*/
const TABLE_NAME = 'wc_order_product_lookup';
/**
* Cache identifier.
*
* @var string
*/
protected $cache_key = 'variations';
/**
* Mapping columns to data type to return correct response types.
*
@ -248,8 +255,12 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
$query_args = wp_parse_args( $query_args, $defaults );
$this->normalize_timezones( $query_args, $defaults );
/*
* We need to get the cache key here because
* parent::update_intervals_sql_params() modifies $query_args.
*/
$cache_key = $this->get_cache_key( $query_args );
$data = wp_cache_get( $cache_key, $this->cache_group );
$data = $this->get_cached_data( $cache_key );
if ( false === $data ) {
$data = (object) array(
@ -348,20 +359,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
'page_no' => (int) $query_args['page'],
);
wp_cache_set( $cache_key, $data, $this->cache_group );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Returns string to be used as cache key for the data.
*
* @param array $params Query parameters.
*
* @return string
*/
protected function get_cache_key( $params ) {
return 'woocommerce_' . self::TABLE_NAME . '_' . md5( wp_json_encode( $params ) );
}
}

View File

@ -14,6 +14,7 @@ use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDa
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Products\DataStore as ProductsDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore as TaxesDataStore;
use \Automattic\WooCommerce\Admin\API\Reports\Cache as ReportsCache;
/**
* ReportsSync Class.
@ -488,6 +489,8 @@ class ReportsSync {
)
);
ReportsCache::invalidate();
// If all updates were either skipped or successful, we're done.
// The update methods return -1 for skip, or a boolean success indicator.
if ( 4 === absint( $result ) ) {
@ -817,6 +820,8 @@ class ReportsSync {
CustomersDataStore::delete_customer( $customer_id );
}
ReportsCache::invalidate();
wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => 'customer' ) );
}
@ -859,6 +864,8 @@ class ReportsSync {
OrdersStatsDataStore::delete_order( $order_id );
}
ReportsCache::invalidate();
wc_admin_record_tracks_event( 'delete_import_data_job_complete', array( 'type' => 'order' ) );
}
}