samedi 26 décembre 2015

What does this laravel lottery code do?

I was looking for a good spot too. always generate a new cookie value for cartid so the user always has a cart and I dont need too check in my controller methdods, in laravel (webshop). I came across StartSession middleware and found this code:

/**
 * Remove the garbage from the session if necessary.
 *
 * @param  \Illuminate\Session\SessionInterface  $session
 * @return void
 */
protected function collectGarbage(SessionInterface $session)
{
    $config = $this->manager->getSessionConfig();

    // Here we will see if this request hits the garbage collection lottery by hitting
    // the odds needed to perform garbage collection on any given request. If we do
    // hit it, we'll call this handler to let it delete all the expired sessions.
    if ($this->configHitsLottery($config)) {
        $session->getHandler()->gc($this->getSessionLifetimeInSeconds());
    }
}

 /**
 * Determine if the configuration odds hit the lottery.
 *
 * @param  array  $config
 * @return bool
 */
protected function configHitsLottery(array $config)
{
    return mt_rand(1, $config['lottery'][1]) <= $config['lottery'][0];
}

I would like to understand what is happening at configHitsLottery() and why it's used. I understand that they check if one values is greater thant he other and if it is clear the sessions that are expired. But why is this done by random, I always thaught it is a bad thing to leave things to chances when programming. why not just use a local value for example, if ($lastCheckCookies > 2 days) clearExpired();



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1YIQCwl
via IFTTT

Aucun commentaire:

Enregistrer un commentaire