The program optimization of automatic e-mail in the simulated landing library

Source: Internet
Author: User
Keywords PHP analog login algorithm optimization
With the PHP CI framework (this does not matter, mainly want to ask a thought) write a test library book borrowing time, automatically send mail function.

Process

1. Querying the Database
2, access to user accounts and passwords, simulated landing
3, grasp the book information, to obtain the user all book expiry time, judge if the time is less than two days, send mail.
4, repeat 2, 3 steps until all users are traversed.

When the number of users reached 30,000, the program will not collapse, should be how to optimize. Want to ask, optimize the idea.

public function email()    {         $data = $this->lib_model->M_getUserAndEmail();//取得用户账户         foreach ($data as $keys){              $bookData = $this->imitateLogin($keys['sdutnum'],$keys['lib_pwd']);//模拟登陆             for($i=1;$i<=$bookData['bookNum'];$i++) //遍历用户所有图书             {                $returnTime = substr($bookData['bookArray']['0'][$i*8+4],65,-13);//获取还书时间                $day = floor((strtotime($returnTime) - strtotime(date('Y-m-d')))/86400); //计算图书距离归还的时间                $bookName = substr($bookData['bookArray']['0'][$i*8+1],111,-10);                $title = "山东理工图书到期提醒";                $content = $keys['sdutnum'].",您的".$bookName."将于".$returnTime."到期";/*add link about address in here.*/                if($day<=2){                    $this->sendEmail($keys['email'],$title,$content); //发送邮件                    echo "邮件发送完成";                }            }         }    }

Reply content:

With the PHP CI framework (this does not matter, mainly want to ask a thought) write a test library book borrowing time, automatically send mail function.

Process

1. Querying the Database
2, access to user accounts and passwords, simulated landing
3, grasp the book information, to obtain the user all book expiry time, judge if the time is less than two days, send mail.
4, repeat 2, 3 steps until all users are traversed.

When the number of users reached 30,000, the program will not collapse, should be how to optimize. Want to ask, optimize the idea.

  public Function email () {$data = $this->lib_model->m_getusera Ndemail ();//Get User account foreach ($data as $keys) {$bookData = $this->imitatelogin ($keys [' Sdutnum '), $key s[' lib_pwd ');//Analog login for ($i =1; $i <= $bookData [' Booknum ']; $i + +)//traverse all books of the user {$retur Ntime = substr ($bookData [' Bookarray '] [' 0 '] [$i *8+4],65,-13];//get book Time $day = Floor ((Strtotime ($returnTime)- Strtotime (Date (' y-m-d ')))/86400);                Calculate the time of the book distance return $bookName = substr ($bookData [' Bookarray '] [' 0 '] [$i *8+1],111,-10);                $title = "Shandong Polytechnic book Expiry reminder"; $content = $keys [' Sdutnum ']. ", your. $bookName." Will be in ". $returnTime." /*add link about the address in here.*/if ($day <=2) {$this->sendemail ($keys [' Email '], $title, $content);                Send mail echo "Mail sent complete"; }            }         }    }

Regardless of your other business, suppose you now have user information, and the time it takes to return the books that they borrowed
Say the optimization of this piece.
First of all, the shortcomings of your business now, you said to traverse all the data, which is very inappropriate data a lot, slow not to say, and inefficient, time complexity is O (n).
If I did, I would have two scripts
A script collects the user's borrowing information every 3 hours, inserts the user's mailbox, the return date into the Redis's ordered collection, score the date

A script runs in the wee hours of the day, obtaining user data from the Redis ordered collection for two days, with the condition that

score <  time()+两天秒数

No, the user volume 3W, in fact, in two days need to return the book will not be 3W, even if the saturation of 3W, the amount of data is not very

You say the query database to get the user password, I do not know whether the library to find you to do or you do it yourself, if it is a library project, you can check the database, but also what the simulation login. If you do it yourself, first you have to think about whether the first two steps can be done.

If you have database permissions, it is completely unnecessary to impersonate a login

Optimization Recommendations:

1. User Impersonation Login: can cache cookies?

2. Scan optimization: Can cache the book information according to the user? Speed up scanning?

3. E-mail: Can I use asynchronous mode?

  • 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.