Last active
September 7, 2024 17:49
-
-
Save JuanDMeGon/f086e5b6e4f71ae1a1b5e66ff08814e9 to your computer and use it in GitHub Desktop.
A small Laravel command to collect the sessions garbage if applicable to the current session driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console; | |
use Illuminate\Console\Scheduling\Schedule; | |
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; | |
class Kernel extends ConsoleKernel | |
{ | |
//... | |
/** | |
* Define the application's command schedule. | |
* | |
* @param \Illuminate\Console\Scheduling\Schedule $schedule | |
* @return void | |
*/ | |
protected function schedule(Schedule $schedule) | |
{ | |
//... | |
$schedule->command('session:gc') | |
->hourly(); | |
//... | |
} | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
return [ | |
//... | |
/* | |
|-------------------------------------------------------------------------- | |
| Session Sweeping Lottery | |
|-------------------------------------------------------------------------- | |
| | |
| Some session drivers must manually sweep their storage location to get | |
| rid of old sessions from storage. Here are the chances that it will | |
| happen on a given request. By default, the odds are 2 out of 100. | |
| | |
*/ | |
'lottery' => [0, 1], | |
//... | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class SessionGarbageCollector extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'session:gc'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Clears the sessions garbage if applicable to the current driver'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
session()->getHandler()->gc($this->getSessionLifetimeInSeconds()); | |
} | |
/** | |
* Get the session lifetime in seconds. | |
* | |
* @return int | |
*/ | |
protected function getSessionLifetimeInSeconds() | |
{ | |
return (config('session.lifetime', null)) * 60; | |
} | |
} |
Here is the article that shows this session problem in detail, to understand the usage of this gist: https://ma.ttias.be/disable-http-sessions-in-laravel-to-speed-up-your-api/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be sure to "disable" the session lottery to avoid the random garbage collection of session: