-
Notifications
You must be signed in to change notification settings - Fork 72
Description
Describe the bug
Currently, we are using Database_Cache
, and WordPress options in extension, to cache customer payment methods here:
woocommerce-payments/includes/class-wc-payments-customer-service.php
Lines 276 to 280 in 2587dd7
$payment_methods = $this->payments_api_client->get_payment_methods( $customer_id, $type )['data']; | |
if ( $cache_payment_methods ) { | |
$this->database_cache->add( $cache_key, $payment_methods ); | |
} | |
return $payment_methods; |
The database cache is meant for individual items, ex. account cache with a fixed key.
Once we start using Database_Cache
with semi-random keys, ex. {$customer_id}_{$type}
, we are abusing the cache and things start getting out of control. There is no garbage collection mechanism of any sort, and expired options do not get removed from the database, leading to a bloat of untracked data in wp_options
.
Considering that we are caching customer payment methods just to avoid an API call during follow-up page loads, we should treat them as short-lived data, and use another mechanism to store it. Transients come to mind, but we could consider a custom post type, combined with a cleanup mechanism.
To Reproduce
- Start with a fresh site/clean DB.
- As a logged in user, purchase something, and navigate to the Payment Methods page in the front-end.
- Observe that there is a new entry in
wp_options
. - Repeat with another customer, and check the DB.
- Imagine repeating this a few hundred times.
Actual behavior
The database is filled with cached payment methods.
Expected behavior
Cached payment methods should either never reach the options table, or should get cleaned up shortly after.
Additional context
pMz3w-lLu-p2