-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(redis): Support mget
command in caching functionality
#12348
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0d9856e
feat(redis): Support `mget` command in caching functionality
s1gr1d 320855b
fix tests
s1gr1d e8f2d99
Merge branch 'develop' into sig/ioredis-statements
s1gr1d 3ded60f
review comments
s1gr1d e4618cb
review comments
s1gr1d 7fcfa91
Merge branch 'develop' into sig/ioredis-statements
s1gr1d 7814781
fix single arg commands
s1gr1d d0f8f84
change if condition
s1gr1d d9e29ee
set cache hit for all get commands
s1gr1d 25766e7
Merge branch 'develop' into sig/ioredis-statements
s1gr1d 144cbfc
accept arrays in item size calculation
s1gr1d 38c3ec9
test something
s1gr1d f61a939
add flatten array util
s1gr1d 2007358
create array key
s1gr1d 1707d3e
Merge branch 'develop' into sig/ioredis-statements
s1gr1d 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type { CommandArgs as IORedisCommandArgs } from '@opentelemetry/instrumentation-ioredis'; | ||
import { flatten } from '@sentry/utils'; | ||
|
||
const SINGLE_ARG_COMMANDS = ['get', 'set', 'setex']; | ||
|
||
export const GET_COMMANDS = ['get', 'mget']; | ||
export const SET_COMMANDS = ['set' /* todo: 'setex' */]; | ||
// todo: del, expire | ||
|
||
/** Determine cache operation based on redis statement */ | ||
export function getCacheOperation( | ||
command: string, | ||
): 'cache.get' | 'cache.put' | 'cache.remove' | 'cache.flush' | undefined { | ||
const lowercaseStatement = command.toLowerCase(); | ||
|
||
if (GET_COMMANDS.includes(lowercaseStatement)) { | ||
return 'cache.get'; | ||
} else if (SET_COMMANDS.includes(lowercaseStatement)) { | ||
return 'cache.put'; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
function keyHasPrefix(key: string, prefixes: string[]): boolean { | ||
return prefixes.some(prefix => key.startsWith(prefix)); | ||
} | ||
|
||
/** Safely converts a redis key to a string (comma-separated if there are multiple keys) */ | ||
export function getCacheKeySafely(redisCommand: string, cmdArgs: IORedisCommandArgs): string[] | undefined { | ||
try { | ||
if (cmdArgs.length === 0) { | ||
return undefined; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const processArg = (arg: string | Buffer | number | any[]): string[] => { | ||
if (typeof arg === 'string' || typeof arg === 'number' || Buffer.isBuffer(arg)) { | ||
return [arg.toString()]; | ||
} else if (Array.isArray(arg)) { | ||
return flatten(arg.map(arg => processArg(arg))); | ||
} else { | ||
return ['<unknown>']; | ||
} | ||
}; | ||
|
||
if (SINGLE_ARG_COMMANDS.includes(redisCommand) && cmdArgs.length > 0) { | ||
return processArg(cmdArgs[0]); | ||
} | ||
|
||
return flatten(cmdArgs.map(arg => processArg(arg))); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** Determines whether a redis operation should be considered as "cache operation" by checking if a key is prefixed. | ||
* We only support certain commands (such as 'set', 'get', 'mget'). */ | ||
export function shouldConsiderForCache(redisCommand: string, key: string[], prefixes: string[]): boolean { | ||
if (!getCacheOperation(redisCommand)) { | ||
return false; | ||
} | ||
|
||
return key.reduce((prev, key) => { | ||
return prev || keyHasPrefix(key, prefixes); | ||
}, false); | ||
} | ||
|
||
/** Calculates size based on the cache response value */ | ||
export function calculateCacheItemSize(response: unknown): number | undefined { | ||
const getSize = (value: unknown): number | undefined => { | ||
try { | ||
if (Buffer.isBuffer(value)) return value.byteLength; | ||
else if (typeof value === 'string') return value.length; | ||
else if (typeof value === 'number') return value.toString().length; | ||
else if (value === null || value === undefined) return 0; | ||
return JSON.stringify(value).length; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
return Array.isArray(response) | ||
? response.reduce((acc: number | undefined, curr) => { | ||
const size = getSize(curr); | ||
return typeof size === 'number' ? (acc !== undefined ? acc + size : size) : acc; | ||
}, 0) | ||
: getSize(response); | ||
} |
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.
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.
This seems a bit excessive use of reduce :D I'd avoid it where not absolutely necessary, here an early return is easier to follow IMHO:
or something along this line?