Skip to content

Instantly share code, notes, and snippets.

@trantrongbinh
Forked from JuanDMeGon/Kernel.php
Created July 6, 2021 03:12
Show Gist options
  • Save trantrongbinh/9dfd35d1a021f49bee228a80ea05b3f6 to your computer and use it in GitHub Desktop.
Save trantrongbinh/9dfd35d1a021f49bee228a80ea05b3f6 to your computer and use it in GitHub Desktop.

Revisions

  1. @JuanDMeGon JuanDMeGon revised this gist Apr 29, 2020. 2 changed files with 50 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions Kernel.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?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();

    //...
    }
    //...
    }
    22 changes: 22 additions & 0 deletions session.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?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],

    //...
    ];
  2. @JuanDMeGon JuanDMeGon renamed this gist Apr 29, 2020. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    use Illuminate\Console\Command;

    class SessionGarbageCollection extends Command
    class SessionGarbageCollector extends Command
    {
    /**
    * The name and signature of the console command.
  3. @JuanDMeGon JuanDMeGon created this gist Apr 29, 2020.
    52 changes: 52 additions & 0 deletions SessionGarbageCollection.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php

    namespace App\Console\Commands;

    use Illuminate\Console\Command;

    class SessionGarbageCollection 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;
    }
    }