-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: Port Laravel's once() helper with coroutine safety
#348
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
Conversation
Implements Once using Hyperf Context for per-coroutine isolation, ensuring concurrent requests don't interfere with cached values.
Onceable captures caller context (file, line, class, object) to generate unique hashes for memoization. Supports HasOnceHash interface for custom hash implementations.
Memoizes callable results per-coroutine using the Once class.
Tests verify: - Caching within coroutine - Differentiation based on closure uses - Coroutine isolation (parallel calls get independent results)
Tests verify: - Caller object capture from trace - HasOnceHash interface support for custom hash implementations
Allows objects to provide custom hash values for once() memoization by implementing the HasOnceHash interface.
a887181 to
ed6904b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR ports Laravel's once() memoization helper to Hypervel, adapted for coroutine safety by using Context instead of static properties for storage isolation.
Changes:
- Adds the
once()helper function that caches callable results based on call location and context - Implements coroutine-safe memoization using Hypervel's Context system instead of static properties
- Includes comprehensive tests for caching behavior, coroutine isolation, and hash generation
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/support/src/Once.php |
Main memoization class storing cached values in WeakMap within Context for coroutine isolation |
src/support/src/Onceable.php |
Generates unique hashes from call traces and closure variables to identify memoization targets |
src/support/src/Contracts/HasOnceHash.php |
Interface allowing objects to provide custom hash values for memoization |
src/support/src/helpers.php |
Adds the once() helper function with Laravel-compatible API |
tests/Support/OnceTest.php |
Tests caching behavior, closure differentiation, and coroutine-scoped isolation |
tests/Support/OnceableTest.php |
Tests trace-based object capture and HasOnceHash implementation |
src/horizon/src/ProcessInspector.php |
Removes redundant PHPDoc type annotation |
src/console/src/Scheduling/ManagesFrequencies.php |
Fixes incorrect duplicate type in docblock parameter |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This PR ports Laravel's
once()memoization helper to Hypervel, adapted for coroutine safety.What's included
Onceclass - Stores memoized values usingContextinstead of static properties, ensuring per-coroutine isolationOnceableclass - Generates unique hashes from call traces to identify memoization targetsonce()helper - Same as Laravel's API:once(fn () => expensive())HasOnceHashcontract - Allows objects to provide custom hash values for memoizationCoroutine safety
Laravel's implementation uses a static
WeakMapwhich would leak state between concurrent requests in Swoole. This implementation stores theWeakMapinContext, so each coroutine gets its own isolated memoization cache.Tests
OnceTest- Verifies caching behavior and coroutine isolationOnceableTest- Verifies trace-based hash generation andHasOnceHashsupport