-
-
Notifications
You must be signed in to change notification settings - Fork 138
feat(session): add redis session manager #1790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
08ddd5b
WIP
thoresuenert aaa1a4c
Add tests
thoresuenert 1ea4eac
Replace del with command unlink
thoresuenert 6011609
Implement cleanup with unblocking scan command
thoresuenert 1312548
Cleanup for qa
thoresuenert 5432a1a
WIP
thoresuenert 946c5bb
Fix test setup for prefix
thoresuenert 7954049
Update RedisSessionManager.php
thoresuenert bc5e2ab
Fix namespace of test class
thoresuenert e9e7b0a
Merge branch 'main' into feature/redis-session
thoresuenert e6c589e
Merge branch 'main' into feature/redis-session
thoresuenert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Http\Session\Config; | ||
|
|
||
| use Tempest\Container\Container; | ||
| use Tempest\DateTime\Duration; | ||
| use Tempest\Http\Session\Managers\RedisSessionManager; | ||
| use Tempest\Http\Session\SessionConfig; | ||
|
|
||
| final class RedisSessionConfig implements SessionConfig | ||
| { | ||
| /** | ||
| * @param Duration $expiration Time required for a session to expire. | ||
| */ | ||
| public function __construct( | ||
| private(set) Duration $expiration, | ||
| private(set) string $prefix = 'session:', | ||
| ) {} | ||
|
|
||
| public function createManager(Container $container): RedisSessionManager | ||
| { | ||
| return $container->get(RedisSessionManager::class); | ||
| } | ||
| } |
154 changes: 154 additions & 0 deletions
154
packages/http/src/Session/Managers/RedisSessionManager.php
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\Http\Session\Managers; | ||
|
|
||
| use Tempest\Clock\Clock; | ||
| use Tempest\Http\Session\Session; | ||
| use Tempest\Http\Session\SessionConfig; | ||
| use Tempest\Http\Session\SessionDestroyed; | ||
| use Tempest\Http\Session\SessionId; | ||
| use Tempest\Http\Session\SessionManager; | ||
| use Tempest\KeyValue\Redis\Redis; | ||
| use Tempest\Support\Str\ImmutableString; | ||
| use Throwable; | ||
|
|
||
| use function Tempest\event; | ||
|
|
||
| final readonly class RedisSessionManager implements SessionManager | ||
| { | ||
| public function __construct( | ||
| private Clock $clock, | ||
| private Redis $redis, | ||
| private SessionConfig $sessionConfig, | ||
| ) {} | ||
|
|
||
| public function create(SessionId $id): Session | ||
| { | ||
| return $this->persist($id); | ||
| } | ||
|
|
||
| public function set(SessionId $id, string $key, mixed $value): void | ||
| { | ||
| $this->persist($id, [...$this->getData($id), ...[$key => $value]]); | ||
| } | ||
|
|
||
| public function get(SessionId $id, string $key, mixed $default = null): mixed | ||
| { | ||
| return $this->getData($id)[$key] ?? $default; | ||
| } | ||
|
|
||
| public function remove(SessionId $id, string $key): void | ||
| { | ||
| $data = $this->getData($id); | ||
|
|
||
| unset($data[$key]); | ||
|
|
||
| $this->persist($id, $data); | ||
| } | ||
|
|
||
| public function destroy(SessionId $id): void | ||
| { | ||
| $this->redis->command('UNLINK', $this->getKey($id)); | ||
|
|
||
| event(new SessionDestroyed($id)); | ||
| } | ||
|
|
||
| public function isValid(SessionId $id): bool | ||
| { | ||
| $session = $this->resolve($id); | ||
|
|
||
| if ($session === null) { | ||
| return false; | ||
| } | ||
|
|
||
| if (! ($session->lastActiveAt ?? null)) { | ||
| return false; | ||
| } | ||
|
|
||
| return $this->clock->now()->before( | ||
| other: $session->lastActiveAt->plus($this->sessionConfig->expiration), | ||
| ); | ||
| } | ||
|
|
||
| private function resolve(SessionId $id): ?Session | ||
| { | ||
| try { | ||
| $content = $this->redis->get($this->getKey($id)); | ||
| return unserialize($content, ['allowed_classes' => true]); | ||
| } catch (Throwable $e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public function all(SessionId $id): array | ||
| { | ||
| return $this->getData($id); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<mixed> | ||
| */ | ||
| private function getData(SessionId $id): array | ||
| { | ||
| return $this->resolve($id)->data ?? []; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<mixed>|null $data | ||
| */ | ||
| private function persist(SessionId $id, ?array $data = null): Session | ||
| { | ||
| $now = $this->clock->now(); | ||
| $session = $this->resolve($id) ?? new Session( | ||
| id: $id, | ||
| createdAt: $now, | ||
| lastActiveAt: $now, | ||
| ); | ||
|
|
||
| $session->lastActiveAt = $now; | ||
|
|
||
| if ($data !== null) { | ||
| $session->data = $data; | ||
| } | ||
|
|
||
| $this->redis->set($this->getKey($id), serialize($session), $this->sessionConfig->expiration); | ||
|
|
||
| return $session; | ||
| } | ||
|
|
||
| private function getKey(SessionId $id): string | ||
thoresuenert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return sprintf('%s%s', $this->sessionConfig->prefix, $id); | ||
| } | ||
|
|
||
| private function getSessionIdFromKey(string $key): SessionId | ||
| { | ||
| return new SessionId( | ||
| new ImmutableString($key) | ||
| ->afterFirst($this->sessionConfig->prefix) | ||
| ->toString(), | ||
| ); | ||
| } | ||
|
|
||
| public function cleanup(): void | ||
thoresuenert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $cursor = '0'; | ||
|
|
||
| do { | ||
| $result = $this->redis->command('SCAN', $cursor, 'MATCH', $this->getKey(new SessionId('*')), 'COUNT', '100'); | ||
| $cursor = $result[0]; | ||
| ld($result); | ||
| foreach ($result[1] as $key) { | ||
| $sessionId = $this->getSessionIdFromKey($key); | ||
|
|
||
| if ($this->isValid($sessionId)) { | ||
| continue; | ||
| } | ||
|
|
||
| $this->destroy($sessionId); | ||
| } | ||
| } while ($cursor !== '0'); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.