-
Notifications
You must be signed in to change notification settings - Fork 253
impr(CLDSRV-852): Refactor cache and helpers #6107
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
Open
tmacro
wants to merge
2
commits into
improvement/CLDSRV-869/account_limiting
Choose a base branch
from
improvement/CLDSRV-852/refactor_cache
base: improvement/CLDSRV-869/account_limiting
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−335
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,63 +1,66 @@ | ||
| const configCache = new Map(); | ||
|
|
||
| // Load tracking for adaptive burst capacity | ||
| // Map<bucketKey, Array<timestamp>> - rolling 1-second window | ||
| const requestTimestamps = new Map(); | ||
| const namespace = { | ||
| bucket: 'bkt', | ||
| }; | ||
tmacro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function setCachedConfig(key, limitConfig, ttl) { | ||
| function cacheSet(cache, key, value, ttl) { | ||
| const expiry = Date.now() + ttl; | ||
| configCache.set(key, { expiry, config: limitConfig }); | ||
| cache.set(key, { expiry, value }); | ||
| } | ||
|
|
||
| function getCachedConfig(key) { | ||
| const value = configCache.get(key); | ||
| if (value === undefined) { | ||
| function cacheGet(cache, key) { | ||
| const cachedValue = cache.get(key); | ||
| if (cachedValue === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const { expiry, config } = value; | ||
| const { expiry, value } = cachedValue; | ||
| if (expiry <= Date.now()) { | ||
| configCache.delete(key); | ||
| cache.delete(key); | ||
| return undefined; | ||
| } | ||
|
|
||
| return config; | ||
| return value; | ||
| } | ||
|
|
||
| function cacheDelete(cache, key) { | ||
| cache.delete(key); | ||
| } | ||
|
|
||
| function expireCachedConfigs(now) { | ||
| function cacheExpire(cache) { | ||
| const now = Date.now(); | ||
|
|
||
| const toRemove = []; | ||
| for (const [key, { expiry }] of configCache.entries()) { | ||
| for (const [key, { expiry }] of cache.entries()) { | ||
| if (expiry <= now) { | ||
| toRemove.push(key); | ||
| } | ||
| } | ||
|
|
||
| for (const key of toRemove) { | ||
| configCache.delete(key); | ||
| cache.delete(key); | ||
| } | ||
|
|
||
| return toRemove.length; | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate cached config for a specific bucket | ||
| * | ||
| * @param {string} bucketName - Bucket name | ||
| * @returns {boolean} True if entry was found and removed | ||
| */ | ||
| function invalidateCachedConfig(bucketName) { | ||
| const cacheKey = `bucket:${bucketName}`; | ||
| return configCache.delete(cacheKey); | ||
| function formatKeyDecorator(fn) { | ||
| return (resourceClass, resourceId, ...args) => fn(`${resourceClass}:${resourceId}`, ...args); | ||
| } | ||
|
|
||
| const getCachedConfig = formatKeyDecorator(cacheGet.bind(null, configCache)); | ||
| const setCachedConfig = formatKeyDecorator(cacheSet.bind(null, configCache)); | ||
| const deleteCachedConfig = formatKeyDecorator(cacheDelete.bind(null, configCache)); | ||
| const expireCachedConfigs = cacheExpire.bind(null, configCache); | ||
|
|
||
| module.exports = { | ||
| namespace, | ||
| setCachedConfig, | ||
| getCachedConfig, | ||
| expireCachedConfigs, | ||
| invalidateCachedConfig, | ||
|
|
||
| deleteCachedConfig, | ||
| // Do not access directly | ||
| // Used only for tests | ||
| configCache, | ||
| requestTimestamps, | ||
| }; | ||
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
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.