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.
- so I'm not making large queries that need to be stored and I could make my transients stick around for maybe a minute or two
- the penalty for making a 2nd query occasionally if the transient isn't there will not be high, we still do want to prevent it though
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_subscribersinstead of justsubscribersin most of my examples here, wherecss_tis my imaginary prefix for css-tricks.com.
- need to prefix my transients with my "short" client name to avoid any conflicts
- for plugins maybe include the version number of your plugin in your constant name so that as you update any cache is invalidated by the update. Though that would mean that your old transients won't be called again and thus won't be invalidated.