问题
I have a Laravel 4.2 app hosted on Bluehost and I'm trying figure out why the MySql session table never gets cleaned up. I've tried increasing the lottery but still no joy. Any ideas?
Here are the relevant session.php settings:
'driver' => 'database',
'lifetime' => 180,
'expire_on_close' => true,
'lottery' => array(2, 100)
回答1:
On every request Laravel uses the session lottery
value to generate a random number that will determine if it will run the session garbage collection. The exact code it uses is:
mt_rand(1, $config['lottery'][1]) <= $config['lottery'][0];
If this is true garbage collection will run and check for any entries older then the lifetime
setting.
With the default settings this means that mt_rand(1, 100) <= 2
must be true, which will run the garbage collector on average every 50 requests. So just setting the lottery
to let's say (20, 100)
will still require the application to get some requests in before the garbage collector is triggered.
If you want to clear the old sessions from the table just set the lottery
value to (100, 100)
(or any 2 equal values) and refresh the app.
来源:https://stackoverflow.com/questions/26744921/laravel-4-2-session-table-not-getting-cleaned-up