0

I have some code running on my website that uses API calls to pull events from a calendar and show them on my website. The code is fairly simple overall, and works well, however to prevent the code from running every time the page loads, I'm using PHP Redis to save the key data to a Redis List, then a cronjob to run the php code that uses the Redis List to fetch the information from the calendar API, and save the information to Redis.

Everything works fine, except I am using foreach to run through each instance of the Redis List; and it keeps running through all the entries but saving only the last one. What can I do to fix this?

My code:

<?php
function redis_calendar_fetch() {
    $redisObj1 = new Redis(); 
    $redisObj1 -> connect('localhost', 6379, 2.5, NULL, 150 );
    date_default_timezone_set('America/Edmonton');
    $fetcharray = $redisObj1-> smembers('fetch_list');
    $effectiveDate = date('Y-m-d', strtotime('+12 months'));
    $application_id = "REDACTED";
    $secret = "REDACTED";
    $fafull = array();
    foreach($fetcharray as $faraw) {
        $fa1 = json_decode($faraw);
        $fa2 = json_decode(json_encode($fa1), true);
        $fafull[] = $fa2;
    }
    $redisObj1 -> close(); // This code all works perfectly, and returns the Redis List results in an array that can be used by foreach
    foreach($fafull as $fa) {
        $redisObj = new Redis(); 
        $redisObj -> connect('localhost', 6379, 2.5, NULL, 150 );
        // After this, I run through all the array data, pull data & process it properly. I have omitted this from this question  because it is long and arduous, and runs perfectly fine.
        // Right before this, an array called $redisarray is created that contains all the relevant event data //
        $redisarrayfixed = json_encode($redisarray);
        $redisObj->set($key, $redisarrayfixed);
        $redisObj -> close();
        // If I put a line here saying 'return = $redisarrayfixed', the code runs only the first instance of the array and stops. If I omit this, it runs through all of them, but only saves the last one
    }
}
redis_calendar_fetch();

As mentioned, I then use a cronjob to run this code every 30 minutes, and I have a separate piece of php code that handles the shortcode & fetches the proper saved events for the proper page.

My concern solely is with the foreach($fafull as $fa), which only saves the final result to Redis. Is there a better way to force each array instance to save?

1 Answers1

0

For performance, you might want to keep just one instance of a redis connection active.

Secondly, it's only going to save the final result because on each iteration, you are using the same $key. It seems like what you want to do is iterate and push to an array, then at the end, save it entirely.

Example of how I'm understanding this;

$redisObj = new Redis(); 
$redisObj -> connect('localhost', 6379, 2.5, NULL, 150 );

$someArray = array();

foreach($fafull as $fa) {
    $redisarrayfixed = json_encode($redisarray);
    array_push($someArray, $redisarrayfixed);
}

$redisObj->set($key, $someArray);
$redisObj -> close();
Owen
  • 76
  • 10
  • The two redis connections are leftover from a test I did to see if that would fix my issue, but noted - I will fix that. Secondly, that's not exactly what I'm looking to do; on each instance of the foreach loop I'm hoping to save the results to Redis (using a unique key that I excluded from the code here), not just at the end as an array. In terms of using a different $key for the foreach, how would you recommend I do that? – Darien Chaffart Jun 17 '22 at 02:45
  • Ah so $key is unique. It's hard to gauge if it was some static value from the code posted. So just so I understand, this line "$redisObj->set($key, $redisarrayfixed);" - seems to only set one key in Redis? If so, is the loop only iterating once before returning? Have you put any logs in to see what it's doing? – Owen Jun 17 '22 at 21:37