mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added database handler for session
. (#4409)
Co-authored-by: mac <mac@macdeMacBook-Pro.local> Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
3b03facc8e
commit
cb6ba3753f
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
|
||||||
|
- [#4409](https://github.com/hyperf/hyperf/pull/4409) Added database handler for `session`.
|
||||||
- [#4411](https://github.com/hyperf/hyperf/pull/4411) Added `Hyperf\Tracer\Aspect\DbAspect` to log db records when using `hyperf/db`.
|
- [#4411](https://github.com/hyperf/hyperf/pull/4411) Added `Hyperf\Tracer\Aspect\DbAspect` to log db records when using `hyperf/db`.
|
||||||
- [#4420](https://github.com/hyperf/hyperf/pull/4420) Support `SSL` for `Hyperf\Amqp\IO\SwooleIO`.
|
- [#4420](https://github.com/hyperf/hyperf/pull/4420) Support `SSL` for `Hyperf\Amqp\IO\SwooleIO`.
|
||||||
|
|
||||||
|
@ -1846,7 +1846,7 @@ class Builder
|
|||||||
/**
|
/**
|
||||||
* Execute a query for a single record by ID.
|
* Execute a query for a single record by ID.
|
||||||
*
|
*
|
||||||
* @param int $id
|
* @param mixed $id
|
||||||
* @param array $columns
|
* @param array $columns
|
||||||
* @return mixed|static
|
* @return mixed|static
|
||||||
*/
|
*/
|
||||||
|
@ -12,6 +12,8 @@ declare(strict_types=1);
|
|||||||
namespace Hyperf\Session;
|
namespace Hyperf\Session;
|
||||||
|
|
||||||
use Hyperf\Contract\SessionInterface;
|
use Hyperf\Contract\SessionInterface;
|
||||||
|
use Hyperf\Session\Handler\DatabaseHandler;
|
||||||
|
use Hyperf\Session\Handler\DatabaseHandlerFactory;
|
||||||
use Hyperf\Session\Handler\FileHandler;
|
use Hyperf\Session\Handler\FileHandler;
|
||||||
use Hyperf\Session\Handler\FileHandlerFactory;
|
use Hyperf\Session\Handler\FileHandlerFactory;
|
||||||
use Hyperf\Session\Handler\RedisHandler;
|
use Hyperf\Session\Handler\RedisHandler;
|
||||||
@ -31,6 +33,7 @@ class ConfigProvider
|
|||||||
],
|
],
|
||||||
'dependencies' => [
|
'dependencies' => [
|
||||||
FileHandler::class => FileHandlerFactory::class,
|
FileHandler::class => FileHandlerFactory::class,
|
||||||
|
DatabaseHandler::class => DatabaseHandlerFactory::class,
|
||||||
RedisHandler::class => RedisHandlerFactory::class,
|
RedisHandler::class => RedisHandlerFactory::class,
|
||||||
SessionInterface::class => SessionProxy::class,
|
SessionInterface::class => SessionProxy::class,
|
||||||
],
|
],
|
||||||
|
136
src/session/src/Handler/DatabaseHandler.php
Normal file
136
src/session/src/Handler/DatabaseHandler.php
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* This file is part of Hyperf.
|
||||||
|
*
|
||||||
|
* @link https://www.hyperf.io
|
||||||
|
* @document https://hyperf.wiki
|
||||||
|
* @contact group@hyperf.io
|
||||||
|
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
namespace Hyperf\Session\Handler;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Hyperf\Database\Query\Builder;
|
||||||
|
use Hyperf\DbConnection\Db;
|
||||||
|
use Hyperf\Utils\Arr;
|
||||||
|
use Hyperf\Utils\InteractsWithTime;
|
||||||
|
use SessionHandlerInterface;
|
||||||
|
|
||||||
|
class DatabaseHandler implements SessionHandlerInterface
|
||||||
|
{
|
||||||
|
use InteractsWithTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $minutes;
|
||||||
|
|
||||||
|
public function __construct(string $connection, string $table, int $minutes)
|
||||||
|
{
|
||||||
|
$this->table = $table;
|
||||||
|
$this->minutes = $minutes;
|
||||||
|
$this->connection = $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function open($savePath, $sessionName)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function read($sessionId)
|
||||||
|
{
|
||||||
|
$session = (object) $this->getQuery()->find($sessionId);
|
||||||
|
|
||||||
|
if (isset($session->last_activity)
|
||||||
|
&& $session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($session->payload)) {
|
||||||
|
return base64_decode($session->payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function write($sessionId, $data)
|
||||||
|
{
|
||||||
|
$payload = $this->getDefaultPayload($data);
|
||||||
|
|
||||||
|
if ($this->getQuery()->find($sessionId)) {
|
||||||
|
$this->performUpdate($sessionId, $payload);
|
||||||
|
} else {
|
||||||
|
$this->performInsert($sessionId, $payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy($sessionId)
|
||||||
|
{
|
||||||
|
$this->getQuery()->where('id', $sessionId)->delete();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function gc($lifetime)
|
||||||
|
{
|
||||||
|
return (bool) $this->getQuery()
|
||||||
|
->where('last_activity', '<=', $this->currentTime() - $lifetime)
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform an insert operation on the session ID.
|
||||||
|
*/
|
||||||
|
protected function performInsert(string $sessionId, array $payload): bool
|
||||||
|
{
|
||||||
|
return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform an update operation on the session ID.
|
||||||
|
*/
|
||||||
|
protected function performUpdate(string $sessionId, array $payload): int
|
||||||
|
{
|
||||||
|
return $this->getQuery()->where('id', $sessionId)->update($payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the default payload for the session.
|
||||||
|
*
|
||||||
|
* @param string $data
|
||||||
|
*/
|
||||||
|
protected function getDefaultPayload($data): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'payload' => base64_encode($data),
|
||||||
|
'last_activity' => $this->currentTime(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a fresh query builder instance for the table.
|
||||||
|
*/
|
||||||
|
protected function getQuery(): Builder
|
||||||
|
{
|
||||||
|
return Db::connection($this->connection)->table($this->table);
|
||||||
|
}
|
||||||
|
}
|
27
src/session/src/Handler/DatabaseHandlerFactory.php
Normal file
27
src/session/src/Handler/DatabaseHandlerFactory.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* This file is part of Hyperf.
|
||||||
|
*
|
||||||
|
* @link https://www.hyperf.io
|
||||||
|
* @document https://hyperf.wiki
|
||||||
|
* @contact group@hyperf.io
|
||||||
|
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||||
|
*/
|
||||||
|
namespace Hyperf\Session\Handler;
|
||||||
|
|
||||||
|
use Hyperf\Contract\ConfigInterface;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class DatabaseHandlerFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$config = $container->get(ConfigInterface::class);
|
||||||
|
$connection = $config->get('session.options.connection');
|
||||||
|
$table = $config->get('session.options.table');
|
||||||
|
$minutes = $config->get('session.options.lifetime', 1200);
|
||||||
|
return new DatabaseHandler($connection, $table, $minutes);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user