-
Notifications
You must be signed in to change notification settings - Fork 1
feat(): lru memoization for js worker #466
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 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7c3fede
feat(): lru memoization for js worker
e11sy 712c106
chore(): add crypto hash params
e11sy ed72270
chore(): cover with tests
e11sy c51fa74
chore(): cover with memoize entire beautifyBacktrace method
e11sy 53fd9e2
imp(): tests and types
e11sy c96b935
chore(): lint fix
e11sy 4b8b529
Update workers/javascript/package.json
e11sy 02a12e8
Update workers/javascript/src/index.ts
e11sy 4e67af6
chore(): update test
e11sy 7b97367
chore(): lint fix
e11sy 1baf710
chore(): remove test duplicate
e11sy 67cd19f
test(): cover with different arguments case
e11sy 639e632
chore(): test memoize util
e11sy e9c0f14
chore(): lint fix
e11sy 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,98 @@ | ||
| import LRUCache from 'lru-cache'; | ||
| import Crypto from '../utils/crypto'; | ||
|
|
||
| /** | ||
| * Pick the strategy of cache key form | ||
| * It could be concatenated list of arguments like 'projectId:eventId' | ||
| * Or it could be hashed json object — blake2b512 algorithm | ||
| */ | ||
| export type MemoizeKeyStrategy = 'concat' | 'hash'; | ||
|
|
||
| /** | ||
| * Options of the memoize decorator | ||
| */ | ||
| export interface MemoizeOptions { | ||
| /** | ||
| * Max number of values stored in LRU cache at the same time | ||
| */ | ||
| max?: number; | ||
|
|
||
| /** | ||
| * TTL in milliseconds | ||
| */ | ||
| ttl?: number; | ||
|
|
||
| /** | ||
| * Strategy for key generation | ||
| */ | ||
| strategy?: MemoizeKeyStrategy; | ||
| } | ||
|
|
||
| /** | ||
| * Async-only, per-method LRU-backed memoization decorator. | ||
| * Cache persists for the lifetime of the class instance (e.g. worker). | ||
| * | ||
| * @param options | ||
| */ | ||
| export function memoize(options: MemoizeOptions = {}): MethodDecorator { | ||
| /* eslint-disable @typescript-eslint/no-magic-numbers */ | ||
| const { | ||
| max = 50, | ||
| ttl = 1000 * 60 * 30, | ||
| strategy = 'concat', | ||
| } = options; | ||
| /* eslint-enable */ | ||
|
|
||
| return function ( | ||
| _target, | ||
| propertyKey, | ||
| descriptor: PropertyDescriptor | ||
| ): PropertyDescriptor { | ||
| const originalMethod = descriptor.value; | ||
|
|
||
| if (typeof originalMethod !== 'function') { | ||
| throw new Error('@Memoize can only decorate methods'); | ||
| } | ||
|
|
||
| descriptor.value = async function (...args: unknown[]): Promise<unknown> { | ||
| /** | ||
| * Create a cache key for each decorated method | ||
| */ | ||
| const cacheKey = `memoizeCache:${String(propertyKey)}`; | ||
|
|
||
| /** | ||
| * Create a new cache if it does not exists yet (for certain function) | ||
| */ | ||
| const cache: LRUCache<string, any> = this[cacheKey] ??= new LRUCache<string, any>({ | ||
| max, | ||
| ttl, | ||
| }); | ||
|
|
||
| const key = strategy === 'hash' | ||
| ? Crypto.hash(args, 'blake2b512', 'base64url') | ||
| : args.map(String).join('__ARG_JOIN__'); | ||
|
|
||
| /** | ||
| * Check if we have a cached result | ||
| */ | ||
| const cachedResult = cache.get(key); | ||
|
|
||
| if (cachedResult !== undefined) { | ||
| return cachedResult; | ||
| } | ||
|
|
||
| try { | ||
| const result = await originalMethod.apply(this, args); | ||
|
|
||
| cache.set(key, result); | ||
|
|
||
| return result; | ||
| } catch (err) { | ||
| cache.delete(key); | ||
| throw err; | ||
| } | ||
| }; | ||
|
|
||
| return descriptor; | ||
| }; | ||
| } | ||
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
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
|
neSpecc marked this conversation as resolved.
|
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
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
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.