Nasıl daha hızlı çalıştırmak ve daha küçük bir ayak izi ile bu kod parçacığını optimize edebilirsiniz?

0 Cevap php

Biz müşteri, oturumu "yenilemek" ve aynı zamanda çevrimiçi kullanıcıların son sayısını almak için yeni bir istek, her 10 saniyede bir-verme ile, eşzamanlı oturumları izlemek için memcache kullanan küçük bir komut dosyası var.

$session    = $_GET['session'];
$streamid   = $_GET['streamid'];

if(!is_null($session) && !is_null($streamid)) {
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211);

    $data   = $memcache->get($streamid);

    if($data === false) {
        $data   = array($session => time()+10);

        $memcache->add($streamid,$data,0,10);
    } else {
        $now            = time();
        $streamCount    = count($data);

        for($i=0;$i<$streamCount;$i++) {
            if($data[$i] > $now) {    
                    unset($data[$i]);
            }
        }

        $data[$session] = time()+10;
    }

    echo count($data);
} else {
    echo 'no session or stream specified';
}

0 Cevap