PHP IMAP连接速度 [英] PHP IMAP connection speed

查看:58
本文介绍了PHP IMAP连接速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在php中使用imap进行实验.

this is my first experimentations with imap in php.

我正在为我的公司制作一个面板,在该面板上我上传了带有名称和电子邮件的客户,并向用户分配信息,

I am making a panel for my company, where I upload the clientes, with name and email, and asignit to users,

因此,当用户进入面板并在那里有客户时,面板会检查用户电子邮件帐户并从客户电子邮件中搜索电子邮件,

so when an user enters the panel and have a client there, the panel check the user email account and search for emails from the client email,

我的问题是,第一次加载面板时,它会变得很慢,加载需要5到15秒(有时甚至更长时间),但是第一次加载后,它很快,但是如果我离开了,让我们说10分钟,然后重新加载面板,这又是第一次变慢.

my problem is that the first time I load the panel it gets very slow, it takes 5 sec to 15 secs to load (or more sometimes) but after the first load, it is fast, but if I go away for let's say 10 minutes and reload the panel it gets slow again for the first time.

这是我的代码;对于面板中的每个用户,我将其称为:

this is my code; for each user in the panel I call this function:

function mostrarCorreosRelacionados($remitente){
$hostname = '{localhost:143}INBOX';
$correo = explode("|||",$_SESSION['correo']);
$username = $correo[0];
$password = $correo[1];

/* Intento de conexión */
$inbox = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());

/* Recuperamos los emails */
$emails = imap_search($inbox,'FROM "'.$remitente.'"');


/* Si obtenemos los emails, accedemos uno a uno... */
if($emails) {

    /* variable de salida */
    $output = '';

    /* Colocamos los nuevos emails arriba */
    rsort($emails);

    /* por cada email... */
    $ii=0;
    foreach($emails as $email_number) {

        /* Obtenemos la información específica para este email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        //$body = imap_fetchbody($inbox,$email_number,4);

        $uid = imap_uid($inbox, $email_number);

        $header = imap_header($inbox,$email_number);
        $fromInfo = $header->from[0];
        $replyInfo = $header->reply_to[0];


        $details = array(
            "fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
                ? $fromInfo->mailbox . "@" . $fromInfo->host : "",
            "fromName" => (isset($fromInfo->personal))
                ? $fromInfo->personal : "",
            "replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
                ? $replyInfo->mailbox . "@" . $replyInfo->host : "",
            "replyName" => (isset($replyTo->personal))
                ? $replyto->personal : "",
            "subject" => (isset($header->subject))
                ? $header->subject : "",
            "udate" => (isset($header->udate))
                ? $header->udate : ""
        );

        //$message = mail_mime_to_array($inbox,$email_number);



        /* Mostramos la información de la cabecera del email */

        $output.= '<li>';

        $output.= '<div class="encabezadoMail '.($overview[0]->seen ? 'read' : 'unread').'">';

        $output.= '<span class="subject">Asunto: '.decodificarTexto($details["subject"]).'</span><br />';

        $output.= '<a href="mailto:'.$fromInfo->mailbox.'@'.$fromInfo->host.'" class="from" title="'.$fromInfo->mailbox.'@'.$fromInfo->host.'">'.decodificarTexto($fromInfo->personal).'</a><br />';

        $output.= '<span class="textoChico">'.$overview[0]->date.'</span>';

        $output.= '</div>';



        /* Mostramos el mensaje del email */

        $output.= '<div class="cuerpoMail" id="msg_'.$i.'" style="display:none;">'.utf8_encode(getBody($uid,$inbox)).'</div>';

        $output.= '</li>';




        $ii++;
    }



    print '<ul class="emails"><li class="encabezadoMail"><strong>E-mails</strong></li>'.$output.'</ul>';

} 



/* Cerramos la connexión */

imap_close($inbox);
}

在imap_open中,我尝试了localhost,domain和ip,但是所有方法似乎都一样.

in the imap_open I have tried localhost, domain and ip, but all seems to work the same.

任何想法?

更新为MONKEYZEUS

UPDATE TO MONKEYZEUS

我做了你告诉我的,这就是我得到的:

I did what you told me and here is what I got:

拳头客户端

确认:0.0309870243073秒 Recuperamos:26.7151398659秒

conexión: 0.0309870243073 seconds Recuperamos: 26.7151398659 seconds

第二客户

conexión:0.102792978287秒 Recuperamos:0.0511429309845秒

conexión: 0.102792978287 seconds Recuperamos: 0.0511429309845 seconds

第三客户

conexión:0.00676202774048秒 Recuperamos:0.0503911972046秒

conexión: 0.00676202774048 seconds Recuperamos: 0.0503911972046 seconds

它只是在第一次发生,如果我按F5键,我在第一个客户端上获得的长时间没有发生,它会像其他客户端一样快速加载,如果我等待10分钟,它将在第一个客户端中再次变慢

it just happens in the first time, and if I press F5 the long time I got on the first client does not happends, it load fast as the others, if I wait 10 minutes it gets slow again in the first client

推荐答案

您将必须弄清楚花费时间最长的是什么.

You will have to figure out what is taking the longest.

我想这是这部分:

/* Intento de conexión */
$inbox = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());

/* Recuperamos los emails */
$emails = imap_search($inbox,'FROM "'.$remitente.'"');

所以尝试对其进行基准测试:

So try benchmarking it:

$start = microtime(true); // start timer

/* Intento de conexión */
$inbox = imap_open($hostname,$username,$password) or die('No se pudo conectar con: usuario: '.$username.' y clave: '.$password.' ' . imap_last_error());

echo 'conexión: '.(microtime(true)-$start).' seconds<br>'; // show results

// ---------------------------------------------------------------------

$start = microtime(true); // start timer again

/* Recuperamos los emails */
$emails = imap_search($inbox,'FROM "'.$remitente.'"');

echo 'Recuperamos: '.(microtime(true)-$start).' seconds<br>'; // show new results

conexión可能是最慢的部分.这可能是因为您的邮件服务器.

conexión is probably the slowest part. This could be because of your mail server.

根据您的基准测试,很明显imap_search()是最慢的部分.

According to your benchmarks it is clear that imap_search() is the slowest part.

这是因为它正在执行数据库搜索,并且您遇到以下可能的问题:

This is because it is performing a database search and you have these possible issues:

  • 您的邮件服务器的硬件很弱
  • 您的邮件服务器上有100 GB的数据/电子邮件,因此查询自然会变慢
  • 邮件服务器上的硬盘空间已用完

第二次快速运行的原因是因为数据库执行了某种称为缓存"的操作,因此查询结果已加载到内存(RAM)中,但是10分钟后数据库要么清除了缓存,要么数据发生了更改因此查询需要重新缓存.

The reason it goes fast the second time is because databases perform something called caching so the results of the query were loaded into memory (RAM) but after 10 minutes that cache is either cleared by the database or a change occurred to the data so the query needs to be re-cached.

这篇关于PHP IMAP连接速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆