How to use Multithreading in PHP's Foreach Loop 60,000 times

Source: Internet
Author: User
Tags bulk insert
Background: I use a foreach loop to generate a cache in the thinkphp framework and then downgrade the cache with other controls and methods.

The problem is to first exclude on-demand generation, because it is not certain that more than 60,000 data can be used, so you need to generate all the content, and because the resulting content time is too long to ask for advice, cache each data has 6 fields 4 of which are string,2 int

It is now run as a direct foreach loop and then use the S method to generate the file cache. This generates a file cache of approximately 15M, resulting in a length of 5-7s, with database query time.

foreach($data as $v){    $arr[$v['id']]=$v;}S('cache',$arr);

1, if the use of multi-threaded way, how should it run?
Or tell me how the principle of multi-threading can be
"Resolved" 2, using the Phpredis cache, using the hash type should be how to BULK INSERT key values
I mean, write the array straight in, but not serialize him.

$cache=new Redis();$cache->connect(......)foreach($data as $v){    $arr[$v['id']]=$v;    foreach($v as $k=>$val){        $cache->set($k,$val);    }}

Found in Phpredis that there is a hmset (key, $array) that can be set in bulk

Reply content:

Background: I use a foreach loop to generate a cache in the thinkphp framework and then downgrade the cache with other controls and methods.

The problem is to first exclude on-demand generation, because it is not certain that more than 60,000 data can be used, so you need to generate all the content, and because the resulting content time is too long to ask for advice, cache each data has 6 fields 4 of which are string,2 int

It is now run as a direct foreach loop and then use the S method to generate the file cache. This generates a file cache of approximately 15M, resulting in a length of 5-7s, with database query time.

foreach($data as $v){    $arr[$v['id']]=$v;}S('cache',$arr);

1, if the use of multi-threaded way, how should it run?
Or tell me how the principle of multi-threading can be
"Resolved" 2, using the Phpredis cache, using the hash type should be how to BULK INSERT key values
I mean, write the array straight in, but not serialize him.

$cache=new Redis();$cache->connect(......)foreach($data as $v){    $arr[$v['id']]=$v;    foreach($v as $k=>$val){        $cache->set($k,$val);    }}

Found in Phpredis that there is a hmset (key, $array) that can be set in bulk

Or with a multi-process bar, PHP seems to support the thread is not very good, on some systems can not be used.
You look at the fork function and then you know how to use multiple processes.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.