June 1st, 2020

Work Journal - Stopping Two Calls in Short Order for The Same Data to an External API

I have a scenario where I need to flag a user on an external API when order status changes. This currently works but we also need to flag them with the same flag if users are granted a complimentary membership/subscription inside WooCommerce Subscriptions. Investigating how to ensure we don't hit that external API twice, once for the Membership becoming active and once for Order completing. Under normal operation that is what would happen.

It's been a while since I looked at the docs on transients and current best practices so we'll start there since transients can be stored for a limited time.

Summary

Transients can work for this, but maybe I don't need to worry about it since it's not a long query or a lot of data. We're literaly sending an array of 3 keys with two letters in each. The only issue really is with our endpoint that may reject a few quick calls in a short time.

Transient Resources

The Deal with WordPress Transients

Transients are for any chunk of information that takes a long time to generate. They will improve server latency for any routine more complex than retrieving a single database cell. That said, they are more code, and more code means more bugs. Therefore, I tend to reserve them for either remote calls or really large queries.

It’s helpful to be able to identify all of the transients that pertain to your plugin. The way to do this is to prefix them with your plugin namespace. This is also crucial for preventing collisions with other transients. That’s why you see me doing css_t_subscribers instead of just subscribers in most of my examples here, where css_t is my imaginary prefix for css-tricks.com.

<?php

  // This is the version number for our plugin.
  CSS_T_VERSION = '3.1.4';

  function css_t_subscribers() {

    // Do we have this information in our transients already?
    $transient = get_transient( 'css_t_subscribers' . CSS_T_VERSION );
  
    // Do things...
    
    // Save the API response so we don't have to call again until tomorrow.
    set_transient( 'css_t_subscribers' . CSS_T_VERSION, $out, DAY_IN_SECONDS );
    
  }
  
?>
<?php

function css_t_subscribers( $limit = '50' ) {
  
  // The namespace for our plugin.  
  $namespace = css_t_namespace(); // Let's say this gives us the slug name, 'css_tricks';
  
  // Cut it down to a max of 7 chars.
  $namespace = substr($namespace, 0, 7 );

  // The base url for getting subscribers.
  $url = 'https://api.example.com/subscribers';
  
  // Sanitize the limit variable.
  $limit = absint( $limit );
  
  // Add the limit variable to the url.
  $url = add_query_arg( array( 'limit', $limit ), $url );

  // Build a transient name that is guarenteed to carry all the uniqueness we might want, and also be less than 40 chars.
  $transient_name = $namespace . md5( $url );

}

A Guide to Transients in WordPress

Additionally, although they can improve your project’s performance, transients are best reserved for large queries and remote calls. If it will require more code to create a transient than it will to simply make a fresh request for the resource each time it’s needed, you’re better off going without.

10up Engineering Best Practices

Locking Mechanisms in WooCommerce

If you're looking to build a Membership Site on WordPress, I've been doing that since 2009. Shoot me an email to talk.