Skip to content

Stop using options for caching customer data #10617

@RadoslavGeorgiev

Description

@RadoslavGeorgiev

Describe the bug

Currently, we are using Database_Cache, and WordPress options in extension, to cache customer payment methods here:

$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

  1. Start with a fresh site/clean DB.
  2. As a logged in user, purchase something, and navigate to the Payment Methods page in the front-end.
  3. Observe that there is a new entry in wp_options.
  4. Repeat with another customer, and check the DB.
  5. 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

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions