0

I have few transient options in my plugin with an expiry time of 45 minutes. After that, they hit an API to get the response. Now in some cases, the server throws an exception or throttles the user in case there is a limit on request per second. Now in such, scenario I still want to render the response and the only way seems to me right now is to somehow restore the expired transient in case of throttling exception from the server. Any thoughts on ways to restore expired transients or any other better approach?

1
  • 1
    Short answer - no. get_transient() automatically deletes expired transients. Check out WP-TLC-Transients which provides an interface for "soft-expiring" transients allowing you to use an expired value while updating it in a background process.
    – ssnepenthe
    Nov 7 '17 at 2:05
0

If you are coding this, probably the best approach is not to use the transient for the value you are displaying, but a normal custom field that's updated .

In that situation you have the transients, which will time-out and trigger the request. The result you get is checked by your code and then stored elsewhere.

You can use that field to display it on the front at all times. Whenever a transient request completes you update that field.

0

Depending on site set up Transients may be stored in memory cache avoiding need for DB lookup. So it is probably best to continue using them but also store your last good API response elsewhere for fallback.

I would use Wordpress Options API (get_option and update_option) to do this and just add relevant code below where you use transient functions. N.B. Transients are also created by WP using Option functions.

  1. when you set transient also do an update_option (this will also create the option if id doesn't exist). update_option('my_fallback_data', $my_good_api_response);

  2. when transient expired and API response throttled then:

    $fallback_data = get_option('my_fallback_data'); // NB. if option doesn't currently exist $fallback_data === false

If there is a maximum acceptable age for this fallback then you could create an extra "option" in 1. above to store the time e.g.

    update_option('my_fallback_data_expiry', time() + 86400); // now + 1 day

and then check 'my_fallback_data_expiry' against current time in 2 above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.