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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire