Caching client_token
-
Hi!
We are using your plugin on our site and I’ve observed that every call to the /checkout page generates a call to the Braintree API to obtain the client_token. This request is somewhat time-expensive, compared to the time the checkout screen takes to render.
Have you considered the possibility of caching the returned value so we are not constantly asking Braintree for the client_token again and again?
In our site we are using Redis as a persistent object cache, so this change would represent a big improvement in the server performance.
I’ve carried out a local test replacing the contents of the wc_braintree_generated_client_token() function with the following ones:
function wc_braintree_generate_client_token( $env = '' ) { $client_token = ''; try { $args = array(); if ( ( $merchant_account = wc_braintree_get_merchant_account( wc_braintree_get_currency() ) ) ) { $args['merchantAccountId'] = $merchant_account; } $cache_key = 'wc_braintree_client_token_' . md5(json_encode($args) . json_encode( $env ) ); $client_token = wp_cache_get( $cache_key, 'woo-payment-gateway' ); if ( $client_token === false ) { $gateway = new \Braintree\Gateway( wc_braintree_connection_settings( $env ) ); $client_token = $gateway->clientToken()->generate( $args ); wp_cache_set( $cache_key, $client_token, 'woo-payment-gateway', 60 ); } } catch ( \Braintree\Exception $e ) { wc_braintree_log_error( sprintf( __( 'Error creating client token. Exception: %1$s', 'woo-payment-gateway' ), get_class( $e ) ) ); } return $client_token; }
and things seem to keep working, but now much faster.
Am I missing anything?
- The topic ‘Caching client_token’ is closed to new replies.