Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

laravel - How does the Session table records delete itself?

I'm working with the Session table in Laravel and I just don't see how these records delete themselves after some time.

I haven't found any comprehensive guide on how the Session table works.

Here's my code so far:

SessionHandler.php

<?php

namespace AppExtensions;

class SessionHandler implements SessionHandlerInterface
{
    public function open($savePath, $sessionName) {}
    public function close() {}
    public function read($sessionId) {}
    public function write($sessionId, $data) {}
    public function destroy($sessionId) {}
    public function gc($lifetime) {}
}

AppSession.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;
use CarbonCarbon;

class Session extends Model
{
    protected $hidden = ['payload'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    public $table = 'sessions';

    public $timestamps = false;

    /**
     * Returns the user that belongs to this entry.
     */
    public function user()
    {
        return $this->belongsTo('User');
    }

    /**
     * Returns all the guest users.
     *
     * @param  $query
     * @return IlluminateDatabaseEloquentBuilder
     */
    public function scopeGuests($query)
    {
        return $query->whereNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(25)));
    }

    /**
     * Returns all the registered users.
     *
     * @param  $query
     * @return IlluminateDatabaseEloquentBuilder
     */
    public function scopeRegistered($query)
    {
        return $query->whereNotNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(25)))->with('user');
    }

    /**
     * Updates the session of the current user.
     *
     * @param  $query
     * @return IlluminateDatabaseEloquentBuilder

     */
    public function scopeUpdateCurrent($query)
    {
        return $query->where('id', Session::getId())->update([
            'user_id' => ! empty(Auth::user()) ? Auth::id() : null
        ]);
    }
}

AppServiceProvider.php

Session::extend('handler', function ($app) {
    // Return implementation of SessionHandler
    return new SessionHandler;
});

.env

SESSION_LIFETIME = 1

session.php

'lifetime' => env('SESSION_LIFETIME', 1),

'expire_on_close' => false,

What I'm basically trying to do is show each Guests/Users activity, but once their session/time limit is over -- POP! It deletes from the database too. Can anyone please share some insight on how to achieve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I just had to narrow down my search (or search better xD) --

You can set your session.php 'lifetime' value to however long you want the session to last -- and then for the 'lottery' value you can set it to 1,1 so that it fires every time a request is sent (I'm not sure of the performance effect of setting it to this) instead of the default 2,100. You probably should also change .env file SESSION_LIFETIME value to whenever you want it to expire (i.e. 1 for 1 minute).

So after 1 minute, on a request (via 'lottery' firing everytime), it deletes the session records with an expiry time of over one minute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...