1

I have a project which displays information from about 60 stock tickers. The issue is, that the project is making requests every time the page loads for these 60 stocks.

This is kind of how it looks right now:

    $elements = array('Stock1', 'Stock2' , 'Stock3', 'Stock4' , 'Stock5',  'Stock6', 'Stock7', 'Stock8', );

foreach ( $elements as $index => $element ) {
    $ticker = $element;

$url = 'https://api.myapi.com/chart?ticker=' . $ticker . '';

$response = wp_remote_get($url, $args);
    $body     = wp_remote_retrieve_body( $response );
    $http_code = wp_remote_retrieve_response_code( $response );
    $formatted_json = json_decode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

} ?>

I can't make a combined (for more than 1 stocks information) request from the API at one time. What would be the best way to cache this? The stock information is not in real time (delayed by 15 min), so I guess if I were able to cache results for 5 minutes each time before downloading them again, that would be sufficient. Any tips are appreciated.

2
  • 1
    What have you researched so far / how comfortable are you with WordPress development? I think fetching the data via a Cron and saving it somewhere in the database (either custom table or options with autoload off) should be enough
    – kero
    Dec 20, 2022 at 9:33
  • Thanks for the tip. I had some trouble with the CRON method, but I managed to do it with transients. Does the CRON method offer better performance?
    – Pbalazs89
    Dec 20, 2022 at 12:49

1 Answer 1

3
$body = get_transient( 'my_api_response_value' );

if ( false === $body ) 
{
    $url = 'https://api.myapi.com/chart?ticker=' . $ticker . '';
    $response = wp_remote_get($url);

    if (200 !== wp_remote_retrieve_response_code($response)) {
        return;
    }

    $body = wp_remote_retrieve_body($response);
    set_transient( 'my_api_response_value', $body, 5*MINUTE_IN_SECONDS );
}

$formatted_json = json_decode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

We submit a request at the beginning because the transient doesn't yet exist, and then we save the $body as a transitory value.

More info about the transient go to this Link : https://developer.wordpress.org/reference/functions/set_transient/

3
  • 2
    +1 but obviously you'll need a different transient per $ticker so you'll need to append that to the name.
    – Rup
    Dec 20, 2022 at 12:28
  • Thanks, I managed to do it like this and loading down is down by 1.5 sec, so thanks a bunch for that!
    – Pbalazs89
    Dec 20, 2022 at 12:49
  • 1
    If you have a high traffic site, it might make sense to use WP Redis or Redis Object Cache, because by default transients are stored in the database, whereas the mentioned plugins offload this to a Redis instance
    – kero
    Dec 20, 2022 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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