瓶颈使我的脚本的某些部分变慢了
|
我有一个执行以下步骤的脚本
用户登录,添加SMS消息并指定收件人。该信息被添加到“排队消息表”中;
某些进程使用API发送消息,并将消息移至“已发送消息表”;
传递报告后,邮件将从“已发送邮件表”中删除,并将引用已发送邮件的日志条目添加到“已发送邮件日志表”中。
当“排队的消息表”中有大量消息排队时,步骤(2)和(3)需要很长时间,
在将消息推送到API之前,会为每个收件人生成一个随机的唯一ID,以供以后在检索报告时参考,该ID用于表“已发送消息日志表”。
以下是示例脚本
<?php
class Message {
/*
* random unique id for mobile number
*/
protected $mobile_ids = array();
public function add_unique_id($id, $mobile)
{
$this->mobile_ids[] = array($id, $mobile);
}
public function get_unique_id()
{
return $this->mobile_ids;
}
// The method that generated the xml for API
public function makeXML($param,$multi_part=false)
{
$xmlString =
\"<SMS>
<authentification>
<username>sss</username>
<password>sss</password>
</authentification>
<message>
<sender>sender</sender>\";
if($multi_part == \"longSMS\") $xmlString .= \"<type>longSMS</type>\";
$xmlString .= \"<text>{$param[\'text\']}</text></message><recipients>\";
// Array of mobile numbers came from $param
$phone_numbers = $param[\'numbers\'];
// Loop through the array and generate <gsm messageId=\'0001\'>mobile</gsm>
foreach($phone_numbers as $mobile) {
// Generate id for mobile
$msg_id = $this->make_random_int();
/**
* This is the slow part of the script,
* IDs are added to the array for logging into the database
* When message is sent, i looped through the id and created a log for this message
**/
$this->add_unique_id($msg_id, $mobile);
$xmlString .= \"<gsm messageId=\\\"{$msg_id}\\\">{$mobile}</gsm>\";
}
$xmlString .= \"</recipients></SMS>\";
return $xmlString;
}
/**
* This is the method that created the log
* Log the sms
* You will need to call $msgid = $this->update_db(\'the sms\')
* return value of $msgid is last_insert_id
*/
public function log_sms($msgid) {
// Log the currently sent message
$userData = array();
$now = date(\'Y-m-d H:i:s\');
foreach ($this->mobile_ids as $data) {
$userData[] = \"(\'{$msgid}\', \'{$data[0]}\', \'{$data[1]}\', \'QUEUED\', \'0000-00-00\', \'0000-00-00\', \'{$now}\')\";
}
$query = \'INSERT INTO sent_sms_log (txtId,msgID,mobile,status,sentdate_time,deliver_date_time,sysdate_time) VALUES\' . implode(\',\', $userData);
$this->ci->db->query($query);
$this->mobile_ids = array(); // reset the array
}
// Make random int
protected function make_random_int() {
$this->ci->load->helper(\'string\');
$int = random_string(\'numeric\', 12);
return $int;
}
/**
* Update database after sms sent
* @return int
*/
public function update_db($msg, $owner, $qid=0) {
$data = array(\'qid\'=> $qid, \'sms\' => $msg, \'date_time\' => date(\'Y-m-d H:i:s\'), \'owner\' => $owner);
$this->ci->db->insert(\'f_sent_sms\', $data);
return $this->ci->db->insert_id();
}
}
没有找到相关结果
已邀请:
2 个回复
旦彤
奥李