Ecommerce Shopify WordPress Discussion

Is it efficient to access/update an array using stored in site options using get_option and update_option in a loop?

I have an array stored in the site options in WordPress which will potentially have 3000 email addresses in it. Every night I need to process this list with some checks on those email addresses and I'm worried that if it fails I could be in trouble. I decided for safety I should do this in batches. At midnight I create a scheduled event which runs every 2 minutes in batches of 50. Currently, in the function, this is what happens. I make a new array of 50 items which I then use in a loop to run the various processing bits I need to do. function tw_process_daily_list_chunk(){ $tw = get_option("tw_settings"); $daily_list_chunk = array_splice($tw["daily_list"], -50, count($tw["daily_list"])); foreach ($daily_list_chunk as $playeremail){ do_various_things(); } if (count($tw["daily_list"]) == 0 ){ do_stop_processing_stuff(); } } I'm now concerned that if this failed then the next time the scheduled event runs it thinks it processed the last batch and it might not have. Is there any downside to directly accessing the larger array from get_option, and then also updating updating that option instead? function tw_process_daily_list_chunk(){ $tw = get_option("tw_settings"); $iteration = 0; foreach ($tw["daily_list"] as $playeremail){ do_various_things(); $key = array_search($playeremail, $tw_settings['daily_list']); if ($key !== false) { unset($tw_settings['daily_list'][$key]); } update_option("tw_settings", $tw_settings); $iteration ++; if ($iteration == $chunksize){ break; } } if (count($tw["daily_list"]) == 0 ){ do_stop_processing_stuff(); } }
You just need to change what you are doing ever so slightly, and then you can process the entire array efficiently at once, assuming do_various_things() isn't too involved. Do not call update_option inside the loop! Instead, before the loop, create an empty array (maybe called $processed_array). Inside the loop, assign to this array cleaned values (or skip values that shouldn't be in the array). AFTER the loop completes, call update_option and pass in your processed array. After this slight rewrite to your function, if you are worried about this process timing out, you should comment out the update_option line and test it to see to see how long it takes. If do_various_things() is just some string comparisons and if/then statements, this should run very, very quickly.

January 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.