Skip to content

Commit

Permalink
update some for session manage
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 15, 2019
1 parent cd38ccd commit 0cf1062
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/framework/src/Contract/SessionStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function read(string $sessionId): string;
* @param string $sessionId The session id.
* @param string $sessionData The encoded session data. This data is a serialized
* string and passing it as this parameter.
* Please note sessions use an alternative serialization method.
*
* @return bool
* The return value (usually TRUE on success, FALSE on failure).
Expand Down
97 changes: 97 additions & 0 deletions src/framework/src/Session/ArrayStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);

namespace Swoft\Session;

use Swoft\Contract\SessionStorageInterface;

/**
* Class ArrayStorageHandler
*
* @since 2.0.8
*/
class ArrayStorage implements SessionStorageInterface
{
/**
* Storage all sessions
* eg [sid0 => session data, ...]
*
* @var array
*/
private $map = [];

/**
* Read session data
*
* @param string $sessionId The session id to read data for.
*
* @return string
* Returns an encoded string of the read data.
* If nothing was read, it must return an empty string.
* Note this value is returned internally to PHP for processing.
*/
public function read(string $sessionId): string
{
return $this->map[$sessionId] ?? '';
}

/**
* Write session data
*
* @param string $sessionId The session id.
* @param string $sessionData The encoded session data. This data is a serialized
* string and passing it as this parameter.
* Please note sessions use an alternative serialization method.
*
* @return bool
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
*/
public function write(string $sessionId, string $sessionData): bool
{
$this->map[$sessionId] = $sessionData;

return true;
}

/**
* Destroy a session
*
* @param string $sessionId The session ID being destroyed.
*
* @return bool
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
*/
public function destroy(string $sessionId): bool
{
if (isset($this->map[$sessionId])) {
unset($this->map[$sessionId]);
return true;
}

return false;
}

/**
* Whether the session exists
*
* @param string $sessionId
*
* @return bool
*/
public function exists(string $sessionId): bool
{
return isset($this->map[$sessionId]);
}

/**
* Clear all session
*
* @return bool
*/
public function clear(): bool
{
$this->map = [];
return true;
}
}
80 changes: 41 additions & 39 deletions src/framework/src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Swoft\Session;

use Swoft;
use Swoft\Co;
use Swoft\Contract\SessionInterface;
use Swoft\Exception\SessionException;
use Swoft\WebSocket\Server\Connection;

/**
* Class Session - Global long connection session manager(use for ws,tcp)
*
* @since 2.0
*/
class Session
final class Session
{
// The global session manager and storage bean name
public const ManagerBean = 'gSessionManager';
public const StorageBean = 'gSessionStorage';

/**
* The map for coroutineID to SessionID
*
Expand All @@ -22,21 +26,12 @@ class Session
private static $idMap = [];

/**
* Session connection list
*
* @var SessionInterface[]
*
* @example
* [
* // Such as webSocket connection
* 'fd' => SessionInterface,
* 'fd2' => SessionInterface,
* 'fd3' => SessionInterface,
* // Such as http session
* 'sess id' => SessionInterface,
* ]
* @return SessionManager
*/
private static $sessions = [];
public static function getManager(): SessionManager
{
return Swoft::getBean(self::ManagerBean);
}

/*****************************************************************************
* SID and CID relationship manage
Expand Down Expand Up @@ -94,20 +89,21 @@ public static function getBoundedSid(): string
*/
public static function has(string $sid): bool
{
return isset(self::$sessions[$sid]);
return self::getManager()->has($sid);
}

/**
* Get session by FD
*
* @param string $sid If not specified, return the current corresponding session
*
* @return SessionInterface|Connection
*/
public static function get(string $sid = ''): ?SessionInterface
{
$sid = $sid ?: self::getBoundedSid();

return self::$sessions[$sid] ?? null;
return self::getManager()->get($sid);
}

/**
Expand All @@ -118,28 +114,23 @@ public static function get(string $sid = ''): ?SessionInterface
public static function current(): SessionInterface
{
$sid = self::getBoundedSid();
if (isset(self::$sessions[$sid])) {
return self::$sessions[$sid];
}

throw new SessionException('session information has been lost of the SID: ' . $sid);
return self::getManager()->mustGet($sid);
}

/**
* Get connection by FD. if not found will throw exception.
* NOTICE: recommend use Session::current() instead of the method.
*
* @param string $sid
*
* @return SessionInterface|Connection
*/
public static function mustGet(string $sid = ''): SessionInterface
{
$sid = $sid ?: self::getBoundedSid();

if (isset(self::$sessions[$sid])) {
return self::$sessions[$sid];
}

throw new SessionException('session information has been lost of the SID: ' . $sid);
return self::getManager()->mustGet($sid);
}

/**
Expand All @@ -150,34 +141,43 @@ public static function mustGet(string $sid = ''): SessionInterface
*/
public static function set(string $sid, SessionInterface $session): void
{
self::$sessions[$sid] = $session;
// self::$sessions[$sid] = $session;
self::getManager()->set($sid, $session);

// Bind cid => sid(fd)
self::bindCo($sid);
}

/**
* Destroy session
* Destroy session by sessionId
*
* @param string $sid If empty, destroy current CID relationship session
*
* @return bool
*/
public static function destroy(string $sid = ''): void
public static function destroy(string $sid): bool
{
$sid = $sid ?: self::getBoundedSid();

if (isset(self::$sessions[$sid])) {
// Clear self data.
self::$sessions[$sid]->clear();
unset(self::$sessions[$sid]);
}
return self::getManager()->destroy($sid);
}

/**
* Clear all
*/
public static function clear(): void
{
self::$idMap = self::$sessions = [];
self::$idMap = [];

self::getManager()->clear();
}

/**
* Clear all caches
*/
public static function clearCaches(): void
{
self::$idMap = [];

self::getManager()->clearCaches();
}

/**
Expand All @@ -189,10 +189,12 @@ public static function getIdMap(): array
}

/**
* Only get all sessions in current worker memory.
*
* @return SessionInterface[]
*/
public static function getSessions(): array
{
return self::$sessions;
return self::getManager()->getCaches();
}
}
Loading

0 comments on commit 0cf1062

Please sign in to comment.